Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 406不可接受的弹簧座WS_Spring_Maven_Spring Mvc - Fatal编程技术网

Spring 406不可接受的弹簧座WS

Spring 406不可接受的弹簧座WS,spring,maven,spring-mvc,Spring,Maven,Spring Mvc,我正在编写一个SpringRESTWS以XML形式返回响应。下面是我的课程和pom文件 我在执行服务时遇到Http 406错误,如下所示: http://localhost:8080/SpringRestExample/rest/emp/dummy.xml 但是,我在执行时得到了JSON响应 http://localhost:8080/SpringRestExample/rest/emp/dummy.json 请告知 pom.xml <dependency>

我正在编写一个SpringRESTWS以XML形式返回响应。下面是我的课程和pom文件

我在执行服务时遇到Http 406错误,如下所示:

 http://localhost:8080/SpringRestExample/rest/emp/dummy.xml
但是,我在执行时得到了JSON响应

http://localhost:8080/SpringRestExample/rest/emp/dummy.json 
请告知

pom.xml

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>${jackson.version}</version>
    </dependency>   
app-context.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<context:component-scan base-package="com.skanda.spring.controller" />

  <beans:bean id="contentNegotiationManager"
         class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<beans:property name="favorPathExtension" value="true" />
<beans:property name="favorParameter" value="true" />
<beans:property name="parameterName" value="mediaType" />
<beans:property name="ignoreAcceptHeader" value="true"/>
<beans:property name="useJaf" value="false"/>
<beans:property name="defaultContentType" value="application/json" />

<beans:property name="mediaTypes">
    <beans:map>
        <beans:entry key="json" value="application/json" />
        <beans:entry key="xml" value="application/xml" />
   </beans:map>
</beans:property>
</beans:bean>

406表示服务器无法以客户端请求的内容类型生成响应。检查HTTP请求的Accept标头,并确保它包含application/xml


可能它包含text/xml,这就是您应该在
@RequestMapping
注释的
生成属性中使用的内容。顺便说一句,您应该使用
org.springframework.http.MediaType
中的常量,而不是像现在这样硬编码字符串。

对于任何类型的xml转换,您都需要在模型类的顶部添加@XmlRootElement注释。我已经做了更改,运行了上面的程序并得到了结果。只需在Employee类上面添加@XmlRootElement,您就完成了。

谢谢您。我添加了accept头,当再次执行时,如果我需要在请求中传递除accept之外的任何其他头,我将收到400 Http错误(客户端发送的请求在语法上不正确())。请告知。这应该是评论
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<context:component-scan base-package="com.skanda.spring.controller" />

  <beans:bean id="contentNegotiationManager"
         class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<beans:property name="favorPathExtension" value="true" />
<beans:property name="favorParameter" value="true" />
<beans:property name="parameterName" value="mediaType" />
<beans:property name="ignoreAcceptHeader" value="true"/>
<beans:property name="useJaf" value="false"/>
<beans:property name="defaultContentType" value="application/json" />

<beans:property name="mediaTypes">
    <beans:map>
        <beans:entry key="json" value="application/json" />
        <beans:entry key="xml" value="application/xml" />
   </beans:map>
</beans:property>
</beans:bean>
public class Employee implements Serializable{

private static final long serialVersionUID = -7788619177798333712L;

private int id;
private String name;
private Date createdDate;

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

@JsonSerialize(using=DateSerializer.class)
public Date getCreatedDate() {
    return createdDate;
}
public void setCreatedDate(Date createdDate) {
    this.createdDate = createdDate;
}


}