springboot解决传递json字符串时获取某个参数为null

当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){


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
@DateTimeFormat是Spring Boot中的注解,用于将字符串转换成Date类型。它通常在前台向后台传递使用。通过在属性上添加@DateTimeFormat注解,并指定日期格式,可以自动将前台传递字符串封装成Date类型的对象。 该注解在前台向后台传递起作用,而@JsonFormat注解则在后台向前台传递起作用。@JsonFormat注解将Date类型转换成字符串,一般在后台将值传递给前台使用。通过在属性上添加@JsonFormat注解,并指定日期格式和区,可以自动将Date类型的对象转换成字符串。 需要注意的是,@JsonFormat注解不仅可以完成后台到前台参数传递的类型转换,还可以实现前台到后台类型转换。当content-type为application/json,优先使用@JsonFormat的pattern进行类型转换,而不会使用@DateTimeFormat进行类型转换。@JsonFormat注解的作用是完成json字符串到java对象的转换工作,与参数传递的方向无关。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [springBoot注解之@JsonFormat和@DateTimeFormat的作用](https://blog.csdn.net/weixin_43825761/article/details/127286337)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值