关于SpringBoot自定义注解(解决post接收String参数 null(前台传递json格式))
2018-01-05 14:21
1676 查看
今天遇到个问题,接口方面的,请求参数如下图为json格式(测试工具使用google的插件postman)
后台用字符串去接收为null
解决方案有以下几种
1.使用实体接收(一个参数,感觉没必要)
2.使用map接收(参数不清晰,不想用)
3.自定义注解(本文采用)
第一步:
创建两个类代码如下:
package com.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestJson {
String value();
}
package com.annotation;
import java.io.BufferedReader;
import javax.servlet.http.HttpServletRequest;
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 com.alibaba.fastjson.JSONObject;
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);
}
}
第二步:启动类添加如下代码
第三步:后台请求(使用下图方式接受就可以了)
后台用字符串去接收为null
解决方案有以下几种
1.使用实体接收(一个参数,感觉没必要)
2.使用map接收(参数不清晰,不想用)
3.自定义注解(本文采用)
第一步:
创建两个类代码如下:
package com.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestJson {
String value();
}
package com.annotation;
import java.io.BufferedReader;
import javax.servlet.http.HttpServletRequest;
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 com.alibaba.fastjson.JSONObject;
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);
}
}
第二步:启动类添加如下代码
第三步:后台请求(使用下图方式接受就可以了)
相关文章推荐
- 关于java接收前台$.ajax格式为多维json数组数据为null的原因及解决方法
- springboot解决传递json字符串时获取某个参数为null
- 关于怎么解决从ajax传入的json参数注入到Controller的接收对象 以及如何在Spring Boot项目使用参数校验
- 关于将前台向后台传递json格式集合,后台如何将接收并转为集合
- springboot @RequestBody使用JsonSerialize与 JsonDeserialize自定义转参数,处理Date日期格式
- 关于springboot的RestFul的PUT请求,前台参数接收问题
- 解决spring boot接收前端传递过来的json数据时,接收到的时间与实际传递时间不一致的问题
- 接收前台返回的Json格式参数
- Java后台如何接收并处理前台传过来的json格式的数组参数
- php post json参数的传递和接收处理
- 解决java传递参数是String类型,pgsql的numeric字段接收报错问题
- 处理$.ajax传递json格式参数,属性为null,undefined,“”
- 解决spring boot中rest接口404,500等错误返回统一的json格式
- spring接收json格式的多个对象参数
- Java后台如何接收并处理前台传过来的json格式的数组参数
- java服务器端接收json格式传递的参数
- springBoot PUT请求接收不了参数的解决办法
- Java后台如何接收并处理前台传过来的json格式的数组参数
- 05.Java后台如何接收并处理前台传过来的json格式的数组参数
- js前台传递json字符串数组到后台有后台转换成list集合或其他格式的问题解决