gpt4 book ai didi

java - 尝试通过 HTTP 为 Spring Web MVC 服务配置 XML 编码时出现 "406 Not Acceptable"

转载 作者:行者123 更新时间:2023-11-30 09:07:20 25 4
gpt4 key购买 nike

捕获我遇到的问题的玩具示例在 github 上.简而言之,我有一项服务需要通过 HTTP 将 XML 编码回。我正在尝试使用 Spring Web MVC 进行设置。需要编码回来的响应对象是 JAXB2 从模式生成的(因此缺少 @XmlRootElement 标记,但生成它的包有一个 ObjectFactory,它提供生成 JAXBElement-s 的方法,这让 XML 编码器很高兴)。我尝试了基于 Google 搜索的不同 spring 上下文配置,这些配置大多是在堆栈溢出时点击这里的帖子,但无法让它们中的任何一个为我工作。

环境:

  • Spring 4.0.5。
  • Tomcat 7.0.5X。

这是一个显示问题的请求/响应周期示例(一些输出已被省略):

$ curl -v -X GET -H "Accept: application/xml" http://localhost:8080/sotaro/say/boo
* Connected to localhost (::1) port 8080 (#0)
> GET /sotaro/say/boo HTTP/1.1
> User-Agent: curl/7.30.0
> Host: localhost:8080
> Accept: application/xml
>
< HTTP/1.1 406 Not Acceptable
* Server Apache-Coyote/1.1 is not blacklisted
< Server: Apache-Coyote/1.1
< Content-Type: text/html;charset=utf-8
< Content-Language: en
< Content-Length: 1067
< Date: Wed, 04 Jun 2014 12:48:44 GMT
<
<html>
<body>
<h1>HTTP Status 406 -</h1>
<p><b>message</b></p>
<p><b>description</b> <u>The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.</u></p>
</body>
</html>

这是玩具示例的模块。

我尝试过的上下文配置之一:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-4.0.xsd">

<context:component-scan base-package="io.github.gv0tch0.sotaro"/>

<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false" />
<property name="ignoreAcceptHeader" value="false" />
<property name="useJaf" value="false" />
<property name="defaultContentType" value="application/xml" />
<property name="mediaTypes">
<map>
<entry key="xml" value="application/xml" />
</map>
</property>
</bean>

<bean id="xmlConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<constructor-arg ref="jaxbMarshaller" />
<property name="supportedMediaTypes" value="application/xml" />
</bean>

<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="packagesToScan">
<list>
<value>io.github.gv0tch0.sotaro.*</value>
</list>
</property>
</bean>

<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager">
<mvc:message-converters register-defaults="false">
<ref bean="xmlConverter" />
</mvc:message-converters>
</mvc:annotation-driven>

</beans>

Controller :

@Controller
public class Say {
@RequestMapping(value = "/say/{what}",
produces = {MediaType.APPLICATION_XML_VALUE},
method = RequestMethod.GET)
public @ResponseBody SayWhat say(@PathVariable("what") String what) {
return echo(what);
}
private SayWhat echo(String what) {
SayWhat echo = new SayWhat();
echo.setWhat(what);
return echo;
}
}

响应对象(JAXB2 生成):

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "SayWhat", propOrder = {"what"})
public class SayWhat {
@XmlElement(required = true)
protected String what;

public String getWhat() {
return what;
}
public void setWhat(String value) {
this.what = value;
}
}

及其生成的模式:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="urn:io:github:gv0tch0:sotaro"
targetNamespace="urn:io:github:gv0tch0:sotaro"
version="0.0.1">
<xs:element name="say" type="tns:SayWhat" />
<xs:complexType name="SayWhat">
<xs:sequence>
<xs:element name="what" type="xs:string" minOccurs="1" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:schema>

最佳答案

所以罪魁祸首是失踪 JAXBElement -包装响应对象和一个Jaxb2Marshaller配置为支持JAXBElement - 包装响应。

@Controller 变成:

@Controller
public class Say {
private final static ObjectFactory JAXB_FACTORY = new ObjectFactory();

@RequestMapping(value = "/say/{what}",
produces = {MediaType.APPLICATION_XML_VALUE},
method = RequestMethod.GET)
public @ResponseBody JAXBElement<SayWhat> say(@PathVariable("what") String what) {
return echo(what);
}

private JAXBElement<SayWhat> echo(String what) {
SayWhat echo = new SayWhat();
echo.setWhat(what);
return JAXB_FACTORY.createSay(echo);
}
}

还有 Jaxb2Marshaller -部分 Spring 配置变为:

<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="supportJaxbElementClass" value="true"/>
<property name="packagesToScan">
<list>
<value>io.github.gv0tch0.sotaro</value>
</list>
</property>
</bean>

如果 Jaxb2Marshaller 实现发现 JAXB 注释类所在的包包含一个 ObjectFactory 就更好了。能够将响应包装在 JAXBElement 中-s 并使用原始配置开箱即用。

关于java - 尝试通过 HTTP 为 Spring Web MVC 服务配置 XML 编码时出现 "406 Not Acceptable",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24040084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com