为什么restTemplate.put()抛出“HttpClientErrorException:404 Not Found”

时间:2014-11-26 09:20:40

标签: rest spring-mvc resttemplate rest-client

我发出了一个放置请求

RestTemplate restTemplate = new RestTemplate();
restTemplate.put(new URI("http://localhost:8080/test"), dto);

成功点击其余端点

@RequestMapping(value = "/test", method=RequestMethod.PUT)
public void test123(@RequestBody DTO dto) {
    System.out.println("phone:"+dto.getPhone()); 
}

但是" put"客户端上的方法抛出异常,即使服务器被成功命中,我也不知道 期待回报价值。

Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 404 Not Found
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
    at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:589)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:547)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:518)
    at org.springframework.web.client.RestTemplate.put(RestTemplate.java:394)
    at com.my.Main.main(Main.java:45)

1 个答案:

答案 0 :(得分:1)

我知道这是超级旧的,但另一个解决方法是在控制器上添加@ResponseStatus注释并指定HttpStatus.NoContent作为值。通过这种方式,您可以准确地返回状态,并且不必说任何事情(无效)都是响应主体。

相关问题