簡體   English   中英

Spring JSON請求體未映射到Java POJO

[英]Spring JSON request body not mapped to Java POJO

我正在使用Spring來實現RESTful Web服務。 其中一個端點將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”)

有誰知道為什么我沒有得到我預期的結果?

固定

事實證明,請求體的值沒有傳入,因為我不僅需要在我的界面中使用@RequestBody注釋,而且還需要實際的方法實現。 有了這個,問題就解決了。

事實證明,請求體的值沒有傳入,因為我不僅需要在我的界面中使用@RequestBody注釋,而且還需要實際的方法實現。 有了這個,問題就解決了。

你可以通過多種方式實現這一目標,我將以不同的方式實現這一目標 -

NOTE:請求數據為{“customerInfo”:{“firstname”:“xyz”,“lastname”:“XYZ”}}

1st way我們可以將以上數據綁定到地圖上,如下所示

@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
    }

我希望它會幫助你。 謝謝

格式化很糟糕,但這應該適用於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。

從默認構造函數中刪除這兩個語句並嘗試

樣本數據 :

[  
{  
  "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.
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM