简体   繁体   English

春天,最equest数据(发布)未绑定到POJO

[英]Requst data(post) not bind to a POJO in spring

I need to map the form data into a pojo, so ii set the POJO as a method parameter. 我需要将表单数据映射到pojo中,因此ii将POJO设置为方法参数。 So then request data should bind to the pojo. 因此,请求数据应绑定到pojo。 But it not working. 但它不起作用。 I am using spring 4.2.3.RELEASE . 我正在使用spring 4.2.3.RELEASE This is the my code 这是我的代码

PersonDto class PersonDto类

public class PersonDto {

private int userId;
private String userName;

public int getUserId() {
    return userId;
}

public void setUserId(int userId) {
    this.userId = userId;
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}
}

UserController class UserController类

@Controller()
@RequestMapping("/u")
public class UserController {

@ResponseBody
@RequestMapping(value = "/add",method = RequestMethod.POST)
public String addUser(PersonDto dto){
    System.out.println("Name : "+dto.getUserName());
    return ""+dto.getUserName();
}

@RequestMapping("/home")
public String userHome(){
    System.out.println("UserDetailsController Index");
    return "user-home";
}
}

in here home request mapping is working. 在这里家庭请求映射正在工作。 But when i send post request to /add then PersonDto is empty. 但是当我向/add发送发帖请求时,PersonDto为空。 This is how i send request 这是我发送请求的方式 请求 What is the reason for this? 这是什么原因?

您需要在方法参数中使用@RequestBody注释PersonDto dto,以指示它应绑定到请求主体:

public String addUser(@RequestBody PersonDto dto){

try this options: 试试这个选项:

  1. Use @RequestBody in POST method. 在POST方法中使用@RequestBody。

  2. include maven dependency for jackson 包括杰克逊的Maven依赖

    com.fasterxml.jackson.databind.ObjectMapper com.fasterxml.jackson.databind.ObjectMapper

Try to mark your method parameter with @ModelAttribute annotation: 尝试使用@ModelAttribute批注标记方法参数:

@ResponseBody
@RequestMapping(value = "/add",method = RequestMethod.POST)
public String addUser(@ModelAttribute("dto") PersonDto dto){
    System.out.println("Name : "+dto.getUserName());
    return ""+dto.getUserName();
}

Note, that you can specify name of model attribute. 注意,您可以指定模型属性的名称。 If you use <spring:form> tag check you form modelAttribute attribute: 如果使用<spring:form>标记,请检查是否具有modelAttribute属性:

<form:form method="POST" action="/someaction" modelAttribute="dto">
   ...
</form:form>

I might be pretty late to the party, but if this issue is not yet resolved (since I don't see any accepted answer), this answer may help. 我参加聚会可能已经很晚了,但是如果这个问题尚未解决(因为我没有看到任何可接受的答案),那么这个答案可能会有所帮助。 I had a similar issue and realised that I had not added a constructor wherein all the parameters passed in request body could be set. 我有一个类似的问题,并且意识到我没有添加可以设置在请求正文中传递的所有参数的构造函数。 So perhaps adding the following constructor: 因此,也许添加以下构造函数:

public PersonDto(int userId, String userName){
     this.userId=userId;
     this.userName=userName;
}

Please let me know if this works for you. 请让我知道这是否适合您。 If something else already did, please share your answer since it was quite a task for me to realise my silly error. 如果已经做了其他事情,请分享您的答案,因为实现我的愚蠢错误对我来说是一项艰巨的任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM