java - Spring REST 3 支持 XML 和 JSON

标签 java rest spring-mvc xstream jackson

如果我们使用Spring MVC开发REST,它将支持XML和JSON数据。我在我的 spring 配置 bean app-servlet.xml

中写了 ContentNegotiationViewResorver
<bean
        class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"
        p:order="1">
        <property name="mediaTypes">
            <map>
                <entry key="xml" value="application/xml" />
                <entry key="json" value="application/json" />
            </map>
        </property>
        <property name="defaultViews">
            <list>
                <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
                    <property name="marshaller">
                        <bean class="org.springframework.oxm.xstream.XStreamMarshaller"
                            p:autodetectAnnotations="true" />
                    </property>
                </bean>
                <bean
                    class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
            </list>
        </property>
    </bean>

我的 spring REST Controller 是:

@Controller
@RequestMapping("/rest/customers")
class CustomerRestController {

protected Log log = LogFactory.getLog(CustomerRestController.class);

@RequestMapping(method = POST)
@ResponseStatus(CREATED)
public void createCustomer(@RequestBody Customer customer,
        HttpServletResponse response) {

    log.info(">>>" + customer.getName());
    response.setHeader("Location", String.format("/rest/customers/%s",
            customer.getNumber()));
}


@RequestMapping(value = "/{id}", method = GET)
@ResponseBody
public Customer showCustomer(@PathVariable String id) {
    Customer c = new Customer("0001", "teddy", "bean");
    return c;
}


@RequestMapping(value = "/{id}", method = PUT)
@ResponseStatus(OK)
public void updateCustomer(@RequestBody Customer customer) {
    log.info("customer: " + customer.getName());
}

我在我的客户域类中设置了 @XStreamAlias("customer") 注释。 但是当我尝试访问 http://localhost:8080/rest/customers/teddy.xml 时,它总是响应 JSON 数据。

我在我的客户域类中设置了 @XmlRootElement(name="customer") 注释。 但是当我尝试访问 http://localhost:8080/rest/customers/teddy.json 时,它总是响应 XML 数据。

有什么问题吗?

最佳答案

我认为“xml”内容类型应该映射到“text/xml”而不是“application/xml”。此外,要强制基于扩展的内容类型解析器,您可以尝试将“ContentNegotiatingViewResolver”的“favorPathExtension”属性设置为 true(尽管默认情况下它应该是 true!)

编辑:我现在已经在这个 GIT 位置添加了一个工作示例 - git://github.com/bijukunjummen/mvc-samples.git,如果你打开端点,使用 mvn tomcat :run,json 在 http://localhost:8080/mvc-samples/rest/customers/teddy.json 和 xml 在 http://localhost:8080/mvc-示例/rest/customers/teddy.xml。这使用 JAXB2 而不是 XStream,因为我熟悉 JAXB。我注意到的一件事是,当我的 JAXB 注释在 Customer 类中不正确时,Spring 正在提供 JSON 而不是您看到的 XML(您可以通过从 Customer 类中删除 XMLRootElement 注释来复制它),一旦我修复了我的注释,我按预期取回了 XML。 所以可能是您的 XStream 配置有问题。

编辑 2:你是对的!!我没有注意到,一旦我返回 xml,我就认为 json 现在可以工作了。我看到了问题,在 AnnotationMethodHandlerAdapter 中,对 @ResponseBody 的处理有点奇怪,它完全忽略了 ViewResolvers,并使用注册的 MessageConverters 而不是完全绕过 ContentNegotiatingViewResolver,目前的一种解决方法是使用 @ModelAttribute 注释进行响应,而不是 @ResponseBody,这样可以调用 View 解析器。现在尝试使用位于 git@github.com:bijukunjummen/mvc-samples.git 的项目,看看它是否适合您。这可能是一个 Spring 错误,您可以尝试在 Spring 论坛中提出它,看看他们有什么建议。

关于java - Spring REST 3 支持 XML 和 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4703412/

相关文章:

java - JSP 中的 GWT AutoSuggest

java - 如何使用拆分函数将字符串拆分为 '\'?

java - 无法将 Java RESTful Web 服务教程中所示的库导入到我的 NetBeans 项目中

java - 响应未根据 Spring 4.3.0 中我的自定义响应类进行转换

java - 从动态编译的源代码中实例化一个对象

java - 根据 boolean 条件使用星号强制必填字段

java - 如何在 Spring MVC 中使用 model.addAttributes 添加对象

java - org.springframework.beans.factory.annotation.InjectionMetadata.needsRefresh

c# - RestSharp 在等待响应时无法转换对象

java - URI 中的 REST 父 ID