SpringMVC Controller receives the JSON string submitted by the page POST. Because the JSON string contains all lowercase letters with "_", it is too ugly to write the getters and setters of the properties in Java, so I thought of using JSONField to parse it
//User类
import com.alibaba.fastjson.annotation.JSONField;
public class User{
@JSONField(name = "user_name")
private String userName;
public String getUserName(){
return this.userName;
}
public void setUserName(String userName){
this.userName=userName;
}
}
//Controller类
@RequestMapping(value = "/insert_user",method = RequestMethod.POST)
public String insertUser(@RequestBody User user){
System.out.println(user.getUserName());
return "ok";
}
But it was not received after the POST was submitted, all were null. Later, using @SerializeName("user_name") still didn't work. Is there any solution to this? Or is my request method wrong? . . .
The default json converter of spring mvc is jackson, and you are using @JSONField in fastjson, so it does not work. The next thing you have to do is to replace the default json converter. The specific method is Baidu
@RequestBody
Function:
Time to use:
A) GET and POST methods are used to determine the timing based on the value of the request header Content-Type:
B) When submitting in PUT mode, judge based on the value of request header Content-Type:
Note: The data encoding format of the body part of the request is specified by the Content-Type of the header part;