java - spring RESTful 服务工作正常,但抛出异常

标签 java spring rest spring-mvc

我正在创建一个 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);
    }
}

测试时,我收到这些消息:

-服务器端: enter image description here

-客户端:

Client side image

最佳答案

您的 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/

相关文章:

java - 如何在java中创建一个接受回调函数的方法?

java - 在 Intellij "create project from archetype"向导中显示自定义原型(prototype)属性

java - 添加 mvc 命名空间时 Spring-MVC 不起作用

android - 如何在android中调用Restful web服务

java - 在 REST 服务 (JAX-RS) 中保留和更新数据

matlab中bat算法的java版

java - NFC 权限错误

java - 在 Java 中向 Time 对象添加分钟

java - Spring boot - 记录每个 4xx 和 5xx,包括请求

web-services - 在没有任何专用 IP 地址的 EC2 服务器上安装 SSL 证书