天天看點

@RequestBody 接收不到JSON格式請求體資料

@RequestBody 接收不到請求體數的原因可能有很多,今天分享其中一個例子。

控制層:
@RestController
@RequestMapping("/test")
public class TestController {
    protected Log log = LogFactory.getLog(TestController.class);
    @PostMapping(value = "/APPIM/{channel}/{brand}/{skillGroup}")
    public void testPostRequest( @PathVariable("channel") String channel,   @RequestBody AppMsg msg) {
        log.info("--------------------------------------msg:"+msg.toString());
    }
}      

實體類:

public class AppMsg implements Serializable{

    private static final long serialVersionUID = -1753511326596692309L;

    private String ToUserName;

    private String FromUserName;

    ......

    }

postman請求:

@RequestBody 接收不到JSON格式請求體資料

但是背景擷取資料實體的屬性值 ,全部為null 。

原因:

實體類的屬性字段首字母為大寫!! !映射不了

解決辦法倆種:

1.首字母改為預設的小寫方式

2.使用 @JsonProperty 注解在字段屬性上,如下:

    @JsonProperty(value = "ToUserName")

    private String ToUserName;

    @JsonProperty(value = "FromUserName")

    private String FromUserName;