【问题标题】:Correct JSON REST Controller by SpringSpring 正确的 JSON REST 控制器
【发布时间】:2016-07-06 20:19:43
【问题描述】:

我是编写弹簧休息控制器的新手。我想创建一个 JSON 格式的简单发送,但我无法处理它,我的代码是正确的代码吗?

在我的代码中,有一个RestTemplate,它向REST url发送一个简单的POJO,REST Controller发回另一个POJO。

我发现了很多以 JSON 格式发送和接收对象的示例,但其中一些已经存在几年了。我发现最多的是他们通过添加 MappingJackson2HttpMessageConverter 来配置调度程序 bean XML 以用于 配置 bean 以将 JSON 转换为 POJO,反之亦然

...
<beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <beans:property name="messageConverters">
        <beans:list>
            <beans:ref bean="jsonMessageConverter"/>
        </beans:list>
    </beans:property>
</beans:bean>
<beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</beans:bean> 
...

他们还将其设置为 RestTemplate java 代码:

List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(new MappingJackson2HttpMessageConverter());
messageConverters.add(new StringHttpMessageConverter());
restTemplate.setMessageConverters(messageConverters);

有时他们也会设置标题:

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Accept", "application/json");
HttpEntity<MyObject> entity = new HttpEntity<MyObject>(inputRequest, headers);

有时他们将对象转换为 JSON 格式,并将其作为文本而不是实例发送。

我还可以找到两种发送 POST 消息的方法:

ResponseEntity<MyObject> responseEntity = restTemplate.exchange(url, HttpMethod.POST, entity, MyObject.class);

MyObject response = restTemplate.postForObject(url, inputRequest, MyObject.class);

这是 REST 控制器:

@RequestMapping(value = "/send", method = RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE, produces=MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public MyObject send(@RequestBody MyObject requestModel) {
    return //whatever;
}

但是:如果我没有在 XML 中设置任何内容,并且没有向 RestTemplate 添加任何消息转换器和标头,它看起来工作正常。我通过 PostMan 对其进行测试,如果我添加 MyClass 示例的 JSON 格式,我会收到 JSON。

所以我的问题是:我的代码以这种方式发送 JSON 真的正确吗?:

mvc-dispatcher.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />
    <!-- to reach resources like css and images -->
    <default-servlet-handler/>
    <!-- the REST controllers are here -->
    <context:component-scan base-package="hu.viktor" />
</beans:beans>

RestTemplate java:

@Controller
public class RequestSender {
    public MyObject send(MyObject inputRequest) {
        RestTemplate restTemplate = new RestTemplate();
        String url = "http://localhost:8080/rest/send";
        MyObject response = restTemplate.postForObject(url, inputRequest, MyObject.class);
        return response;
    }
}

还有 REST 控制器:

@Controller
@RequestMapping("/rest")
public class CalculatorRestController {
    @RequestMapping(value = "/send", method = RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE, produces=MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public MyObject send(@RequestBody MyObject requestModel) {
        return //whatever;
    }   
}

【问题讨论】:

标签: java json spring rest resttemplate


【解决方案1】:

根据documentation RequestMappingHandlerMappingRequestMappingHandlerAdapter 将自动添加,如果您在xml 配置中指定&lt;mvc:annotation-driven/&gt;(在您的情况下只是&lt;annotation-driven&gt;)。同一文档包含HttpMessageConverters 的列表,将由&lt;mvc:annotation-driven&gt;' 设置。并且:

MappingJackson2HttpMessageConverter 与 JSON 相互转换 — 如果添加 Jackson 2 存在于类路径中。

这意味着您不需要手动添加 json 消息转换器,只需在配置文件中指定 &lt;mvc:annotation-driven&gt; 并在 pom.xml 中声明依赖项(如果您使用的是 Maven):

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.4.6</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.4.6.1</version>
</dependency>

【讨论】:

  • 是的,这些依赖项已经添加到我的 pom.xml 中。所以我的最终代码是正确的?不需要向 xml 和 RestTemplate 添加任何内容?
猜你喜欢
  • 1970-01-01
  • 2018-10-04
  • 2017-01-07
  • 1970-01-01
  • 1970-01-01
  • 2020-12-12
  • 2019-06-06
  • 1970-01-01
  • 2015-01-28
相关资源
最近更新 更多