springboot解决传递json字符串时获取某个参数为null
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json
免费下载资源
·
当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 {
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);
}
}
最后,新建一个webConfig添加该方法
@Override public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers){ argumentResolvers.add(new RequestJsonHandlerMethodArgumentResolver()); }}
补充:
如果只想传递某一个参数时:这种情况,因为是RequestParam,所以采用表单的方式传递
@RequestMapping(value="/userLogin",method = RequestMethod.POST)
@ResponseBody
//@ApiImplicitParam(paramType = "query",name= "username" ,value = "用户名",dataType = "string")
public void userLogin(@RequestParam(value = "username" , required = false) String username,
@RequestParam(value = "password" , required = false) String password){
GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e
4 个月前
8c391e04
6 个月前
更多推荐
已为社区贡献2条内容
所有评论(0)