springboot解决传递json字符串时获取某个参数为null
2018-01-26 16:28
537 查看
当postman传递数据时,如此有多个解决方案,如:
1.以自动转换实体
@RequestMapping(value="/getAjax",method = RequestMethod.POST) @ResponseBody public void getAjax(@RequestBody News news){ System.out.println("ok"); }2.以map或json接收
@RequestMapping(value="/login2",method = RequestMethod.POST) @ResponseBody public void login2(@RequestBody JSONObject jsonObject){ System.out.println("ok"); }3.新建一个自定义接收方式
@RequestMapping(value="/login3",method = RequestMethod.POST) public void login3(@RequestJson(value = "name") String name,@RequestJson(value = "pwd") String pwd){ System.out.println("ok"); }
以下是代码:
创建以上两个文件:
1.RequestJson
package com.pb.news.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(); }2.RequestJsonHandlerMethodArgumentResolver
package com.pb.news.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 { Reques 4000 tJson 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); } }
最后,新建一个webConfig添加该方法
package com.pb.news; import com.pb.news.annotation.RequestJsonHandlerMethodArgumentResolver; import org.springframework.context.annotation.Configuration; import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import java.util.List; @Configuration public class webConfig extends WebMvcConfigurerAdapter {
/*配置自定义接受参数的方法*/@Override public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers){ argumentResolvers.add(new RequestJsonHandlerMethodArgumentResolver()); }}
相关文章推荐
- 关于SpringBoot自定义注解(解决post接收String参数 null(前台传递json格式))
- springboot中put请求获取不到参数解决
- 解决SpringBoot获取不到PUT方式提交的参数的问题
- json传递中文乱码解决方法以及解决request.getParameter()获取参数为乱码的问题
- Spring Boot @PathVariable出现点号"."时导致路径参数截断获取不全的解决办法
- 解决spring boot接收前端传递过来的json数据时,接收到的时间与实际传递时间不一致的问题
- 关于怎么解决从ajax传入的json参数注入到Controller的接收对象 以及如何在Spring Boot项目使用参数校验
- jquery通过ajax-json访问java后台传递参数,通过request.getParameter获取不到参数的说明
- springboot 关于controller层传递单个参数的校验
- easyui-combotree传递参数到后台获取json值来绑定combotree控件
- spring-boot获取form-data数据及json数据
- SpringBank 开发日志 重新设计Action调用Service的参数传递 使用泛型解决类型转换问题
- spring boot 返回json字符串 null值转空字符串
- spring boot中返回json字符串中文乱码
- spring boot 返回的json中去掉值为null的属性
- spring-boot上传文件MultiPartFile获取不到文件问题解决
- spring boot 解决后台返回 json 到前台中文乱码之后出现返回json数据报错 500
- js传递给NPAPI插件的字符串参数在Firefox下正常,在chrome下面会有乱码现象的问题解决
- 异常解决篇:Spring Boot默认错误返回格式变成数组@RequestBody无法解析Json格式
- 微信公众号平台网页授权接口中获取到的授权code传递给(即一个微信公众号网页授权给)任何其他多个回调域名下的url,解决了只能设置一个网页授权回调域名的问题,解决了redirect_uri参数错误的问