春季 XML 406 错误
Posted
技术标签:
【中文标题】春季 XML 406 错误【英文标题】:Spring XML 406 error 【发布时间】:2014-03-18 21:07:01 【问题描述】:我正在尝试使用 Java 制作一个 RESTful 服务,使用几个教程和许多很多 *** 条目。不幸的是,我似乎无法让我的代码正常工作,当我尝试点击端点时,我一直在获取 Http 406。任何帮助表示赞赏。
GreetingController.java:
import java.util.Random;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/greeting")
public class GreetingController
protected final Logger log = LoggerFactory.getLogger(GreetingController.class);
private static final String template = "Hello, %s!";
private static Random rand = new Random();
@RequestMapping(method = RequestMethod.GET, headers="Accept=*/*")//, value="/name")
@ResponseBody
public Greeting greeting()
log.debug("Entered greeting()");
return new Greeting(rand.nextInt(99999999),
String.format(template, "Stephen"));
@RequestMapping(method = RequestMethod.GET, value="/name")
@ResponseBody
public Greeting greetingName(@PathVariable String name)
log.debug("Entered greetingName()");
return new Greeting(rand.nextInt(99999999),
String.format(template, name));
问候.java:
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="greeting")
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
public class Greeting
private Integer id;
private String content;
public Greeting()
public Greeting(Integer id, String content)
this.id = id;
this.content = content;
@XmlElement
public void setId(Integer id)
this.id = id;
public Integer getId()
return id;
@XmlElement
public void setContent(String content)
this.content = content;
public String getContent()
return content;
x-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xmlns:mvc="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.package.name" />
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
Http 标头(通过萤火虫):
Response Headers
Content-Language en
Content-Length 1067
Content-Type text/html;charset=utf-8
Date Tue, 18 Feb 2014 20:24:13 GMT
Server Apache-Coyote/1.1
Request Headers
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Connection keep-alive
Cookie JSESSIONID=9064B3E9F1C3259B73C65B022C8BDC75
Host localhost:8080
User-Agent Mozilla/5.0 (Windows NT 6.0; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0
在启动 tomcat 并访问所述服务器上的不同页面时记录:
DEBUG o.s.security.web.FilterChainProxy - Converted URL to lowercase, from: '/greeting'; to: '/greeting'
DEBUG o.s.security.web.FilterChainProxy - Candidate is: '/greeting'; pattern is /**; matched=true
DEBUG o.s.security.web.FilterChainProxy - /greeting at position 1 of 12 in additional filter chain; firing Filter: 'org.springframework.security.web.access.channel.ChannelProcessingFilter@9a6bbb'
DEBUG o.s.s.w.a.i.DefaultFilterInvocationSecurityMetadataSource - Converted URL to lowercase, from: '/greeting'; to: '/greeting'
DEBUG o.s.s.w.a.i.DefaultFilterInvocationSecurityMetadataSource - Candidate is: '/greeting'; pattern is /**; matched=true
DEBUG o.s.s.w.a.c.ChannelProcessingFilter - Request: FilterInvocation: URL: /greeting; ConfigAttributes: [ANY_CHANNEL]
DEBUG o.s.security.web.FilterChainProxy - /greeting at position 2 of 12 in additional filter chain; firing Filter: 'org.springframework.security.web.context.SecurityContextPersistenceFilter@16fcc4'
DEBUG o.s.s.w.c.HttpSessionSecurityContextRepository - No HttpSession currently exists
DEBUG o.s.s.w.c.HttpSessionSecurityContextRepository - No SecurityContext was available from the HttpSession: null. A new one will be created.
DEBUG o.s.security.web.FilterChainProxy - /greeting at position 3 of 12 in additional filter chain; firing Filter: 'org.springframework.security.web.authentication.logout.LogoutFilter@1db52c8'
DEBUG o.s.security.web.FilterChainProxy - /greeting at position 4 of 12 in additional filter chain; firing Filter: 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@17d6c1'
DEBUG o.s.security.web.FilterChainProxy - /greeting at position 5 of 12 in additional filter chain; firing Filter: 'org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter@1144823'
DEBUG o.s.security.web.FilterChainProxy - /greeting at position 6 of 12 in additional filter chain; firing Filter: 'org.springframework.security.web.authentication.www.BasicAuthenticationFilter@8c02cc'
DEBUG o.s.security.web.FilterChainProxy - /greeting at position 7 of 12 in additional filter chain; firing Filter: 'org.springframework.security.web.savedrequest.RequestCacheAwareFilter@bbd1b'
DEBUG o.s.security.web.FilterChainProxy - /greeting at position 8 of 12 in additional filter chain; firing Filter: 'org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@ac576f'
DEBUG o.s.security.web.FilterChainProxy - /greeting at position 9 of 12 in additional filter chain; firing Filter: 'org.springframework.security.web.authentication.AnonymousAuthenticationFilter@15daa9e'
DEBUG o.s.s.w.a.AnonymousAuthenticationFilter - Populated SecurityContextHolder with anonymous token: 'org.springframework.security.authentication.AnonymousAuthenticationToken@9055c2bc: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@b364: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: null; Granted Authorities: ROLE_ANONYMOUS'
DEBUG o.s.security.web.FilterChainProxy - /greeting at position 10 of 12 in additional filter chain; firing Filter: 'org.springframework.security.web.session.SessionManagementFilter@6c9f0f'
DEBUG o.s.security.web.FilterChainProxy - /greeting at position 11 of 12 in additional filter chain; firing Filter: 'org.springframework.security.web.access.ExceptionTranslationFilter@13ec758'
DEBUG o.s.security.web.FilterChainProxy - /greeting at position 12 of 12 in additional filter chain; firing Filter: 'org.springframework.security.web.access.intercept.FilterSecurityInterceptor@917cb0'
DEBUG o.s.s.w.a.i.DefaultFilterInvocationSecurityMetadataSource - Converted URL to lowercase, from: '/greeting'; to: '/greeting'
DEBUG o.s.s.w.a.i.DefaultFilterInvocationSecurityMetadataSource - Candidate is: '/greeting'; pattern is /welcome*; matched=false
DEBUG o.s.s.w.a.i.DefaultFilterInvocationSecurityMetadataSource - Candidate is: '/greeting'; pattern is /**; matched=true
DEBUG o.s.s.w.a.i.FilterSecurityInterceptor - Secure object: FilterInvocation: URL: /greeting; Attributes: [IS_AUTHENTICATED_ANONYMOUSLY]
DEBUG o.s.s.w.a.i.FilterSecurityInterceptor - Previously Authenticated: org.springframework.security.authentication.AnonymousAuthenticationToken@9055c2bc: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@b364: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: null; Granted Authorities: ROLE_ANONYMOUS
DEBUG o.s.s.access.vote.AffirmativeBased - Voter: org.springframework.security.access.vote.RoleVoter@1913751, returned: 0
DEBUG o.s.s.access.vote.AffirmativeBased - Voter: org.springframework.security.access.vote.AuthenticatedVoter@c6b80e, returned: 1
DEBUG o.s.s.w.a.i.FilterSecurityInterceptor - Authorization successful
DEBUG o.s.s.w.a.i.FilterSecurityInterceptor - RunAsManager did not change Authentication object
DEBUG o.s.security.web.FilterChainProxy - /greeting reached end of additional filter chain; proceeding with original chain
DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'spring-tutorial' determining Last-Modified value for [/SpringMVC/greeting]
DEBUG o.s.w.s.m.a.DefaultAnnotationHandlerMapping - Mapping [/greeting] to handler 'com.example.controller.GreetingController@1eb1db2'
DEBUG o.s.web.servlet.DispatcherServlet - Last-Modified value for [/SpringMVC/greeting] is: -1
DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'spring-tutorial' processing GET request for [/SpringMVC/greeting]
DEBUG o.s.w.b.a.s.HandlerMethodInvoker - Invoking request handler method: public com.example.form.Greeting com.example.controller.GreetingController.greeting()
INFO c.s.controller.GreetingController - Entered greeting()
DEBUG o.s.w.s.m.a.AnnotationMethodHandlerExceptionResolver - Resolving exception from handler [com.example.controller.GreetingController@1eb1db2]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
DEBUG o.s.w.s.m.a.ResponseStatusExceptionResolver - Resolving exception from handler [com.example.controller.GreetingController@1eb1db2]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
DEBUG o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolving exception from handler [com.example.controller.GreetingController@1eb1db2]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
DEBUG o.s.s.w.c.HttpSessionSecurityContextRepository - SecurityContext contents are anonymous - context will not be stored in HttpSession.
DEBUG o.s.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'spring-tutorial': assuming HandlerAdapter completed request handling
DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request
DEBUG o.s.s.w.a.ExceptionTranslationFilter - Chain processed normally
DEBUG o.s.s.w.c.SecurityContextPersistenceFilter - SecurityContextHolder now cleared, as request processing completed
【问题讨论】:
将您的记录器转为 DEBUG 并重试。它应该会告诉您问题出在哪里。 是的,我在 Spring 4.0.0 上工作得很好。我们需要查看这些日志。 添加了日志和日志语句。 您的日志似乎完全不相关。将您的日志级别设置为 DEBUG 并发送得到 406 响应的请求。 我的日志记录被搞砸了,更新了与页面被点击时间相关的正确日志记录。我认为 Java 没有正确地编组为 XML。我一直在寻找像 JSON 的 jackson 插件这样的 XML mashaller,但还没有找到。 【参考方案1】:406 表示请求内容被服务器视为不可接受。尝试删除Accept
标头或将headers="Accept=*/*"
添加到@RequestMapping
注释中。
【讨论】:
我觉得你说错了。它与请求的内容无关。该请求指定它在其Accept
标头中接受的内容类型。如果服务器无法生成该内容,它会以 406 响应。
我尝试添加 [code]header="Accept=*/*"[/code] 但它仍然不起作用。见编辑。我知道问题是响应,问题是我不知道如何修复响应。【参考方案2】:
只是花了几个小时来研究这个,所以我要离开这里,什么对我有用(我正在使用 Spring 4.0.6)以及我认为对你有用的东西(Spring 3.0.x )。
春季 4.0.x
由于MappingJackson2HttpMessageConverter
自动选择Jackson 数据绑定库并启用带有@ResponseBody
注释的控制器方法以自动返回JSON,因此通常假设XML 也存在这样的工具,但事实并非如此。
为了启用 XML,我必须手动创建一个带有两个消息转换器的 RequestMappingHandlerAdapter
,一个 MarshallingHttpMessageConverter
,后跟一个 MappingJackson2HttpMessageConverter
。
这也允许使用 Spring 提供的任何编组器,只需将其设置为 MarshallingHttpMessageConverter
的属性即可。
结果spring-servlet.xml
:
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- ... -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller">
<bean class="org.springframework.oxm.xstream.XStreamMarshaller"></bean>
</property>
</bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
</list>
</property>
</bean>
<!-- ... -->
</beans>
春季 3.0.x
注意:我假设此版本来自您的 x-servlet.xml
中的架构位置。
根据http://spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc和http://spring.io/blog/2013/06/03/content-negotiation-using-views/,有两种方法可以在Spring 3中实现你想要的。第一种是使用ContentNegotiationManager
,它仅从3.2.x版本开始可用,因此不适用于你的版本。第二个是使用ContentNegotiatingViewResolver
,它根据请求的内容类型将视图的解析委托给其他解析器(无论是由Accept
标头定义,通过扩展,还是默认情况下是可配置的)。
基本上,您需要为ContentNegotiatingViewResolver
配置两个解析器,一个用于 XML,另一个用于您的 JSP 视图。
spring-servlet.xml
:
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="location" value="spreadsheet-views.xml"/>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="WEB-INF/views"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
</bean>
例如,您可以为 JSON 添加视图解析器,但您必须实现它,因为 Spring 没有提供。第二篇博文展示了如何实现一个简单的。
【讨论】:
【参考方案3】:我在尝试解决同一问题时遇到了这个问题。在查看 pfac 的答案后,我发现只需将 jackson-dataformat-xml 库添加到我的项目中即可解决我的问题。
如果使用 maven:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.3.2</version>
</dependency>
【讨论】:
【参考方案4】:我正在努力解决类似的问题。尽管我在注释中设置了“produces=application/xml”参数,但由于某种原因,我的 Web 应用程序似乎没有默认提供 xml 数据类型。
我只是通过为我的 applicationContext.xml 添加额外的 bean 配置来解决它:
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="defaultContentType" value="application/xml" />
</bean>
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />
或通过 Java Config 类:
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer)
configurer.defaultContentType(MediaType.APPLICATION_XML);
【讨论】:
以上是关于春季 XML 406 错误的主要内容,如果未能解决你的问题,请参考以下文章
春季启动,XML 在 POM.xml 上有“未知”错误 [重复]