Spring's RestTemplate: complex object to query params

末鹿安然 提交于 2019-12-11 07:55:02

问题


I have a complex object like this:

public class ComplexObject {

    private String a;
    private String b; 
    ...
    private String z;

    //getters and setters
}

I want to call a web service that receives all the complex object fields: http://localhost:8080/api/some_service?a=something&b=something&...&z=something

Is there any way to pass a ComplexObject to RestTemplate and have the work done automatically or I have to do the manual mapping by myself?

Thanks!


回答1:


YES! there is a way to pass complete complex object to make the service call and then for sure it can be achieved automatically. And for this you have to alter the way you send this complexObject and have to use HTTP POST (highly recommended ), as:

public HttpStatus send() 
{
ComplexObject complexObj = getYourFilledObject();


ResponseEntity<HttpStatus> response = restTemplate.postForEntity(ROOT_URI, complexObj, HttpStatus.class);

return response;

}

And if not and GET is the only option then unfortunately you have to send as you’re. Because at the end of the day either you use rest templates ‘s function which intake params map or you create your own URI with params, it is the same HTTP GET and you have to achieve programmatically.

For examples & illustration you can visit here and best reference will be spring resttemplate doc



来源:https://stackoverflow.com/questions/55052637/springs-resttemplate-complex-object-to-query-params

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!