我正在创建一个 Spring RESTful 服务,并且它正在工作。我的客户端将一个对象发送到 put 请求中,我的服务完美地接收该对象,但在此之后,我的客户端收到此异常:“org.springframework.web.client.HttpClientErrorException:404 Not Found”
这是我的客户端代码:
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
Greeting greeting = new Greeting(21l, FileUtils.toByteArray("src/main/resources/test.png"));
String url = "http://localhost:8080/DolphinRestServer/bulletin/put";
restTemplate.put(url,greeting);
这是我的服务器代码:
@Service
@RequestMapping("/bulletin")
public class BulletinService {
@RequestMapping(method = RequestMethod.PUT, value = "/put")
public void put(@RequestBody Greeting greeting){
System.out.println("it's work fine and my greeting is here --------->"+greeting);
}
}
测试时,我收到这些消息:
-服务器端:
-客户端:
最佳答案
您的 put
方法具有 void
返回类型。此外,它不将 HttpServletResponse
作为参数。根据Spring MVC documentation ,当出现这种情况时,Spring MVC 使用 URL 作为 View 名称并尝试渲染它。在您的例子中,Spring MVC 尝试查找并加载名为 /bulletin/put
的 View ,但它无法找到该 View ,因此会出现 404 消息。
关于java - spring RESTful 服务工作正常,但抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28755607/