1

我有一个服务器和一个客户端。我正在使用 Spring 在服务器上映射 http 请求,并使用 RestTemplate 向服务器发出请求。

服务器代码如下所示:

@RequestMapping (value="/someEndPoint", method = RequestMethod.POST)
@ResponseBody
public String configureSettings(
@RequestParam(required=false) Integer param1,
@RequestParam(required=false) Long param2,
@RequestBody String body)
{

if(param1 != null)
// do something

if(body not empty or null)
//do something

} 

客户端:

String postUrl = "http://myhost:8080/someEndPoint?param1=val1"
restTemplate.postForLocation(postUrl, null);

这样做的原因是从 param1 在服务器端触发了正确的操作,但是,请求的主体还包含:
param1=val1
设置的请求主体将是 json,所以我想要的只是能够设置其他参数没有设置身体。我知道我使用 restTemplate 不正确,所以任何帮助将不胜感激。

4

1 回答 1

1

你正在做一个HTTP POST,但你没有提供一个对象 put POSTed。SpringRestTemplate试图找出你想要什么POST,所以它看起来并看到 url 的查询字符串有一些东西,所以它试图使用它。

不要向 a 添加查询字符串POST,只需提供您想要的对象POST

String postUrl = "http://myhost:8080/someEndPoint"
restTemplate.postForLocation(postUrl, new ParamModel("val1"));

这本书很好地Spring in Action (3rd edition)涵盖了(和一般的 REST)。RestTemplate我建议看看它。

于 2012-01-26T03:26:56.527 回答