繁体   English   中英

通过Spring纠正JSON REST控制器

[英]Correct JSON REST Controller by Spring

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

在我的代码中,有一个RestTemplate,它将一个简单的POJO发送到REST URL,然后REST控制器发送回另一个POJO。

我发现很多例子都是以JSON的形式发送和接收对象,但有些例子已经有几年了。 我找到的最多的是他们通过为Configure bean添加MappingJackson2HttpMessageConverter配置调度程序bean XML, 以便将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格式,并将其作为文本而不是实例发送。

我还可以找到2种发送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;
    }   
}

根据文档 ,如果您在xml配置中指定<mvc:annotation-driven/> (在您的情况下只是<annotation-driven> ),将自动添加RequestMappingHandlerMappingRequestMappingHandlerAdapter 相同的文档包含HttpMessageConverters列表, HttpMessageConverters<mvc:annotation-driven>' 和:

MappingJackson2HttpMessageConverter转换为/从JSON转换 - 如果类路径中存在Jackson 2,则添加。

这意味着,您不需要手动添加json消息转换器,只需在配置文件中指定<mvc:annotation-driven>并在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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM