首页 文章

Spring JSON请求体未映射到Java POJO

提问于
浏览
14

我正在使用Spring来实现RESTful Web服务 . 其中一个 endpoints 将JSON字符串作为请求体,我希望将其映射到POJO . 但是,现在似乎传入的JSON字符串不是映射到POJO的属性 .

这是@RestController接口

@RequestMapping(value="/send", headers="Accept=application/json", method=RequestMethod.POST)
public void sendEmails(@RequestBody CustomerInfo customerInfo);

数据模型

public class CustomerInfo {
    private String firstname;
    private String lastname; 
    public CustomerInfo() {
        this.firstname = "first";
        this.lastname = "last";
    }

    public CustomerInfo(String firstname, String lastname)
    {
        this.firstname = firstname;
        this.lastname = lastname;
    }

    public String getFirstname(){
        return firstname;
    }

    public void setFirstname(String firstname){
        this.firstname = firstname;
    }

    public String getLastname(){
        return lastname;
    }

    public void getLastname(String lastname){
        this.lastname = lastname;
    }
}

最后我的POST请求:

{"CustomerInfo":{"firstname":"xyz","lastname":"XYZ"}}

Content-Type指定为application / json

但是,当我打印出对象值时,默认值(“first”和“last”)被打印出来而不是我传入的值(“xyz”和“XYZ”)

有谁知道为什么我没有得到我预期的结果?

FIX

事实证明,请求体的值没有传入,因为我不仅需要在我的界面中使用@RequestBody注释,而且还需要实际的方法实现 . 有了这个,问题就解决了 .

5 回答

  • 0

    事实证明,请求体的值没有传入,因为我不仅需要在我的界面中使用@RequestBody注释,而且还需要实际的方法实现 . 有了这个,问题就解决了 .

  • 2

    你可以通过多种方式实现这一目标,我将以不同的方式实现这一目标 -

    NOTE: 请求数据为{"customerInfo":{"firstname":"xyz","lastname":"XYZ"}}

    1st way 我们可以将以上数据绑定到 Map 上,如下所示

    @RequestMapping(value = "/send", headers = "Accept=application/json", method = RequestMethod.POST)
    public void sendEmails(@RequestBody HashMap<String, HashMap<String, String>> requestData) {
    
        HashMap<String, String> customerInfo = requestData.get("customerInfo");
        String firstname = customerInfo.get("firstname");
        String lastname = customerInfo.get("lastname");
        //TODO now do whatever you want to do.
    }
    

    2nd way 我们可以将它直接绑定到pojo

    step 1 创建dto类 UserInfo.java

    public class UserInfo {
        private CustomerInfo customerInfo1;
    
        public CustomerInfo getCustomerInfo1() {
            return customerInfo1;
        }
    
        public void setCustomerInfo1(CustomerInfo customerInfo1) {
            this.customerInfo1 = customerInfo1;
        }
    }
    

    step 1. 创建另一个dto类 CustomerInfo.java

    class CustomerInfo {
            private String firstname;
            private String lastname;
    
            public String getFirstname() {
                return firstname;
            }
    
            public void setFirstname(String firstname) {
                this.firstname = firstname;
            }
    
            public String getLastname() {
                return lastname;
            }
    
            public void setLastname(String lastname) {
                this.lastname = lastname;
            }
        }
    

    step 3 将请求正文数据绑定到pojo

    @RequestMapping(value = "/send", headers = "Accept=application/json", method = RequestMethod.POST)
        public void sendEmails(@RequestBody UserInfo userInfo) {
    
            //TODO now do whatever want to do with dto object
        }
    

    我希望它会帮助你 . 谢谢

  • 1

    格式化很糟糕,但这应该适用于jackson配置 .

    <!-- Use Jackson for JSON conversion (POJO to JSON outbound). -->
    <bean id="jsonMessageConverter"
                class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> 
    
    <!-- Use JSON conversion for messages -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jsonMessageConverter"/>
            </list>
        </property>
    </bean>
    

    另外,如评论中所述,您的JSON对您的对象是错误的 .

    {"firstname":"xyz",‌​"lastname":"XYZ"}
    

    看起来确实是您对象的正确JSON .

  • -1

    样本数据 :

    [  
    {  
      "targetObj":{  
         "userId":1,
         "userName":"Devendra"
      }
    },
    {  
      "targetObj":{  
         "userId":2,
         "userName":"Ibrahim"
      }
    },
    {  
      "targetObj":{  
         "userId":3,
         "userName":"Suraj"
      }
    }
    ]
    

    对于以上数据,这个pring控制器方法为我工作:

    @RequestMapping(value="/saveWorkflowUser", method = RequestMethod.POST)
    public void saveWorkflowUser (@RequestBody List<HashMap<String ,HashMap<String , 
      String>>> userList )  {
        System.out.println(" in saveWorkflowUser : "+userList);
     //TODO now do whatever you want to do.
    }
    
  • 12

    从默认构造函数中删除这两个语句并尝试

相关问题