Spring 的 RestTemplate:查询参数的复杂对象 [英] Spring's RestTemplate: complex object to query params

查看:78
本文介绍了Spring 的 RestTemplate:查询参数的复杂对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的复杂对象:

I have a complex object like this:

public class ComplexObject {

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

    //getters and setters
}

我想调用一个接收所有复杂对象字段的网络服务:http://localhost:8080/api/some_service?a=something&b=something&...&z=something

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

有没有办法将 ComplexObject 传递给 RestTemplate 并自动完成工作,或者我必须自己进行手动映射?

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?

谢谢!

推荐答案

YES!有一种方法可以传递完整的复杂对象来进行服务调用,然后肯定可以自动实现.为此,您必须更改发送此 complexObject 的方式,并且必须使用 HTTP POST(强烈推荐),如下所示:

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;

}

如果没有并且 GET 是唯一的选择,那么不幸的是,您必须按原样发送.因为在一天结束时,您要么使用接收参数映射的 rest 模板的函数,要么使用参数创建自己的 URI,它是相同的 HTTP GET,您必须以编程方式实现.

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.

例如&插图您可以访问这里,最佳参考将是 spring resttemplate doc

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

这篇关于Spring 的 RestTemplate:查询参数的复杂对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆