SpringBoot 与 Postman 实现REST模拟请求,无法接收json数据

   

     前言、介绍postman,当我们使用登入功能的项目时,除了postman、还需要Postman Interceptor这个插件配合使用,能够正常使用postman正常测试。Postman是一款Http请求模拟工具.它可以模拟各种Http Request,使用起来十分的方便.

  1、 在测试SpringBoot接收数据过程中,遇到这样的问题:controller层接收单个Json数据,属性接收到的null;当加上@RequestBody,接收到为json字符串。

 

   2、在网上找到资料,使用自定义注解的方式。接收数据

(1)引入相关注解


    com.alibaba
    fastjson
    1.2.51

(2)创建两个自定义注解相关的类

SpringBoot 与 Postman 实现REST模拟请求,无法接收json数据_第1张图片

 

a、
package com.blog.annotion;

import java.lang.annotation.*;

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestJson {
    String value();
}

b、

package com.blog.annotion;

import com.alibaba.fastjson.JSONObject;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;

/**
 * @author admin
 * @date 2018/10/27
 */

public class RequestJsonHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.hasParameterAnnotation(RequestJson.class);
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
                                  NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        RequestJson requestJson = parameter.getParameterAnnotation(RequestJson.class);
        HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
        BufferedReader reader = request.getReader();
        StringBuilder sb = new StringBuilder();
        char[] buf = new char[1024];
        int rd;
        while ((rd = reader.read(buf)) != -1) {
            sb.append(buf, 0, rd);
        }
        JSONObject jsonObject = JSONObject.parseObject(sb.toString());
        String value = requestJson.value();
        return jsonObject.get(value);
    }

}

(2)在启动类中添加下面的代码

package com.blog;

import com.blog.annotion.RequestJsonHandlerMethodArgumentResolver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * @author admin
 */
@SpringBootApplication
//另他继承WebMvcConfigurationSupport 
public class DemoApplication extends WebMvcConfigurationSupport {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
//自定义注解添加下面的方法
    @Override
    public void addArgumentResolvers(List argumentResolvers) {
        argumentResolvers.add(new RequestJsonHandlerMethodArgumentResolver());
        super.addArgumentResolvers(argumentResolvers);
    }
}

SpringBoot 与 Postman 实现REST模拟请求,无法接收json数据_第2张图片

SpringBoot 与 Postman 实现REST模拟请求,无法接收json数据_第3张图片

你可能感兴趣的:(Java后端开发)