Java Spring RestTemplate-在GET中传入对象参数
如何使用RestTemplate将对象作为参数传入?例如,假设我使用Spring Boot设置了以下服务:Java Spring RestTemplate-在GET中传入对象参数,java,spring,spring-boot,get,resttemplate,Java,Spring,Spring Boot,Get,Resttemplate,如何使用RestTemplate将对象作为参数传入?例如,假设我使用Spring Boot设置了以下服务: @RequestMapping(value = "/get1", method = RequestMethod.GET) public ResponseEntity<String> get1(@RequestParam(value = "parm") String parm) { String response = "You entered " + parm;
@RequestMapping(value = "/get1", method = RequestMethod.GET)
public ResponseEntity<String> get1(@RequestParam(value = "parm") String parm) {
String response = "You entered " + parm;
return new ResponseEntity<String>(response, HttpStatus.OK);
}
@RequestMapping(value = "/get2", method = RequestMethod.GET)
public ResponseEntity<String> get2(@RequestParam(value = "parm") MyObj parm) {
String response = "You entered " + parm.getValue();
return new ResponseEntity<String>(response, HttpStatus.OK);
}
但是,如果客户机想要调用第二个服务,则会通过以下方式得到500个错误:
//This works fine
RestTemplate restTemplate = new RestTemplate();
String response = restTemplate.getForObject("http://localhost:8080/get1?parm={parm}", String.class, "Test input 1");
//This doesn't work
MyObj myObj = new MyObj("Test input 2");
RestTemplate restTemplate = new RestTemplate();
String response = restTemplate.getForObject("http://localhost:8080/get2?parm={parm}", String.class, myObj);
MyObj类如下所示:
@JsonSerialize
public class MyObj {
private String inputValue;
public MyObj() {
}
public MyObj(String inputValue) {
this.inputValue = inputValue;
}
public String getInputValue() {
return inputValue;
}
public void setInputValue(String inputValue) {
this.inputValue = inputValue;
}
}
@RequestMapping(value = "/get2", method = RequestMethod.POST)
public ResponseEntity<String> get2(@RequestBody MyObj parm) {
String response = "You entered " + parm.getInputValue();
return new ResponseEntity<>(response, HttpStatus.OK);
}
我假设问题在于myObj没有正确地设置为参数。我该怎么做呢
提前谢谢。我猜
这:
应改为
String response = restTemplate.getForObject("http://localhost:8080/get2?parm={parm}", MyObj.class, myObj)
您必须在
responseType
之后传递每个URI参数的值。问题是restemplate
不知道如何将对象映射到URI参数。您必须显式调用适当的myObj
方法来检索实际值:
String response = restTemplate.getForObject(
"http://localhost:8080/get2?parm={parm}", String.class,
myObj.getInputValue());
调用的getForObject
方法的签名是:
public T getForObject(字符串url、类响应类型、对象…url变量)抛出..
其中
urlvaries
是URI变量值的数组,用于扩展URI 当您使用复杂对象(如MyObj
)作为@RequestParam
)时,Spring将尝试将字符串转换为该复杂对象。在这种情况下,由于MyObj
只有一个名为inputValue
的String
字段,因此它将自动使用您提供给查询参数的任何值来填充对象中的属性
例如,如果您调用:http://localhost:8080/get2?parm=foobar
您将得到一个MyObj
,其中inputValue
将是的“foobar”
如果使用RestTemplate
,则不会出现错误,相反,它会尝试使用toString()
方法将new MyObj(“测试输入2”)
转换为字符串,响应为:
您输入了com.example。MyObj@63a815e8
这可能不是你想要的。通常,您不希望将复杂对象作为请求参数传递,可以将
@RequestBody
与RequestMethod.POST
和restemplate.postForEntity()
结合使用,以将MyObj
作为JSON正确传递
按如下方式更改控制器:
@JsonSerialize
public class MyObj {
private String inputValue;
public MyObj() {
}
public MyObj(String inputValue) {
this.inputValue = inputValue;
}
public String getInputValue() {
return inputValue;
}
public void setInputValue(String inputValue) {
this.inputValue = inputValue;
}
}
@RequestMapping(value = "/get2", method = RequestMethod.POST)
public ResponseEntity<String> get2(@RequestBody MyObj parm) {
String response = "You entered " + parm.getInputValue();
return new ResponseEntity<>(response, HttpStatus.OK);
}
这将正确地将对象作为JSON传递。否,第二个参数(
String.class
)用于描述响应的返回类型。/get1
和/get2
中的响应都是一个字符串
,是请求参数发生了变化。工作正常。谢谢回答得好!实际上,很多popele(如果不是所有人:)总是将对象封送/解封为JSON。我总是想知道为什么不在客户端和服务器端同时实现一个接口?只要你不向世界开放你的服务,我相信这是完美的…@hublo这是我在项目中经常做的事情,消费者端的REST客户端可以实现与另一端的REST服务相同的接口。