// Controller
@RequestMapping(value = "/test", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
public String test(User user) {
return user.toString();
}
//
@NoArgsConstructor
@Data
public class User {
private String username;
private String password;
private Detail detail;
@Data
public class Detail {
private String gender;
private String location;
public Detail() {
}
}
}
//
$.post("http://localhost:8083/rrt/test", {
"username": "aaaa",
"password": "bbbb",
"detail": {
"gender": "male",
"location": "Beijing"
}
})
when data is sent in this way, the backend will report an error:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.beans.NullValueInNestedPathException: Invalid property "detail" of bean class [com.example.demo.param.User]: Could not instantiate property type [com.example.demo.param.User$Detail] to auto-grow nested property path; nested exception is java.lang.NoSuchMethodException: com.example.demo.param.User$Detail.<init>()] with root cause
when sent by application/json, you can bind nested parameters correctly with @ ResponseBody; what should you do in the request mode of form-data?