web-dev-qa-db-ja.com

Spring JSONリクエスト本文がJava POJOにマッピングされていない

Springを使用してRESTful Webサービスを実装しています。エンドポイントの1つが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に指定されている

ただし、オブジェクト値を出力すると、渡された値(「xyz」および「XYZ」)の代わりにデフォルト値(「first」および「last」)が出力されました

期待した結果が得られない理由を誰もが知っていますか?

[〜#〜] fix [〜#〜]

私のインターフェースだけでなく、実際のメソッド実装にも@RequestBodyアノテーションが必要なため、リクエスト本文の値は渡されませんでした。それができたら、問題は解決しました。

20
Y. Chen

私のインターフェースだけでなく、実際のメソッド実装にも@RequestBodyアノテーションが必要なため、リクエスト本文の値は渡されませんでした。それができたら、問題は解決しました。

13
Y. Chen

あなたは多くの方法でそれを行うことができます、ここで私は以下の異なる方法でそれをするつもりです-

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
    }

私はそれがあなたを助けることを願っています。ありがとう

4
sanjeevjha

このフォーマットはひどいですが、これはジャクソンの設定で機能するはずです。

<!-- 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
DwB

サンプルデータ :

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