Spring Rest Service不支持XML响应 [英] Spring Rest Service not working for XML response
问题描述
我正在尝试创建spring restful webservice应用程序。
我需要设计服务,它将同时返回json和xml。目前默认情况下,json作为响应返回,但我也想返回xml。
I am trying to create spring restful webservice application. I need to design service which will return both json as well as xml. Currently by default json is returned as a response but i want to return xml as well.
如果我点击: http:// localhost:8087 / RestServices / rest / profile / 233 或233 .json
>> 我按预期得到json回复
>> i get json response as expected
但如果点击: http:// localhost:8087 / RestServices / rest / profile / 233.xml
>> 我收到错误消息 HTTP状态 - 406 此请求标识的资源只能根据请求接受标题生成具有不可接受特征的响应。
>> i get error saying HTTP Status - 406 The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.
技术详细信息:
Spring 4.0
Hibernate 4.0
Spring MVC。
RestController。
Technology Details : Spring 4.0 Hibernate 4.0 Spring MVC. RestController.
依赖/罐子详细信息:
antlr-2.7.7.jar
antlr-2.7.7.jar
aopalliance-1.0.jar
aopalliance-1.0.jar
com.mysql.jdbc_5.1.5.jar
com.mysql.jdbc_5.1.5.jar
commons-logging-1.2.jar
commons-logging-1.2.jar
dom4j-1.6.1.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.5.Final.jar
hibernate-commons-annotations-4.0.5.Final.jar
hibernate-core-4.3.6.Final.jar
hibernate-core-4.3.6.Final.jar
hibernate-entitymanager-4.3.6.Final.jar
hibernate-entitymanager-4.3.6.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
hibernate-validator-4.2.0.Final.jar
hibernate-validator-4.2.0.Final.jar
jackson-annotations-2.4.1.jar
jackson-core-2.4.4.jar
jackson-core-asl-1.9.0.jar
jackson-databind-2.4.4.jar
jackson-dataformat-xml-2.5.0.jar
jackson-jaxrs-1.9.2.jar
jackson-mapper-asl-1.9.0.jar
ja ckson-xc-1.9.2.jar
jandex-1.1.0.Final.jar
jandex-1.1.0.Final.jar
javassist-3.18.1-GA.jar
javassist-3.18.1-GA.jar
jaxb-api-2.2.jar
jboss-logging-3.1.3.GA.jar
jboss-logging-3.1.3.GA.jar
jboss-logging-annotations-1.2.0.Beta1.jar
jboss-logging-annotations-1.2.0.Beta1.jar
jboss-transaction-api_1.2_spec-1.0.0.Final.jar
jboss-transaction-api_1.2_spec-1.0.0.Final.jar
log4j-1.2.17.jar
log4j-1.2.17.jar
logback-classic-0.9.jar
logback-classic-0.9.jar
logback-core-0.9.6.jar
logback-core-0.9.6.jar
servlet-api.jar
servlet-api.jar
slf4j-api-1.6.1.jar
slf4j-api-1.6.1.jar
slf4j-log4j13-1.0.1.jar
slf4j-log4j13-1.0.1.jar
slf4j.jar
slf4j.jar
spring-aop-4.0.0 .RELEASE.jar
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
spring-beans-4.0 .0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-build-src-4.0.0.RELEASE.jar
spring-build-src-4.0.0.RELEASE.jar
spring -context-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-context-support-4.0.0.RELEASE.jar
spring-context-support-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-data-commons-1.6.3.RELEA SE.jar
spring-data-commons-1.6.3.RELEASE.jar
spring-data-jpa-1.4.1.RELEASE.jar
spring-data-jpa-1.4.1.RELEASE.jar
spring-expression- 4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
spring-framework-bom-4.0.0.RELEASE.jar
spring-framework-bom-4.0.0.RELEASE.jar
spring-instrument-4.0.0.RELEASE.jar
spring-instrument-4.0.0.RELEASE.jar
spring-instrument-tomcat-4.0.0.RELEASE.jar
spring-instrument-tomcat-4.0.0.RELEASE.jar
spring-jdbc-4.0.0.RELEASE.jar
spring-jdbc-4.0.0.RELEASE.jar
spring-jdbc.jar
spring-jdbc.jar
spring-jms-4.0.0.RELEASE.jar
spring-jms-4.0.0.RELEASE.jar
spring-messaging-4.0.0.RELEASE.jar
spring-messaging-4.0.0.RELEASE.jar
spring-orm-4.0.0.RELEASE.jar
spring-orm-4.0.0.RELEASE.jar
spring-oxm-4.0.0.RELEASE.jar
spring-oxm-4.0.0.RELEASE.jar
spring-test-4.0.0.RELEASE.jar
spring-test-4.0.0.RELEASE.jar
spring-tx-4.0.0.RELEASE.jar
spring-tx-4.0.0.RELEASE.jar
spring-web-4.0.0.RELEASE.jar
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar
spring-webmvc-portlet-4.0.0.RELEASE.jar
spring-webmvc-portlet-4.0.0.RELEASE.jar
spring-websocket-4.0.0.RELEASE.jar
spring-websocket-4.0.0.RELEASE.jar
woodstox-core-asl-4.2.0.jar
DateRestController。 java
@RestController
@RequestMapping("/rest")
public class DataRestController
{
@RequestMapping(value = "/profile/{number}", method =
RequestMethod.GET)
public
List<CustomerProfile>getCustomerProfile(@PathVariable("number")
String number)
{
return profileList;
}
}
ProfileModel.java
@XmlRootElement(name="profile")
@Entity
@Table(name = "profile")
public class Profile extends CommonBean
{
@Column(name = "email")
private String email;
@Column(name = "mobile")
private String mobile;
@Column(name = "DOB")
private Date dateOfBirth;
@XmlElement
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public String getMobile()
{
return mobile;
}
public void setMobile(String mobile)
{
this.mobile = mobile;
}
public Date getDateOfBirth()
{
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth)
{
this.dateOfBirth = dateOfBirth;
}
}
controller-servlet.xml
<beans>
<context:component-scan base-package="com.test" />
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>`enter code here`
</bean>
</beans>
到目前为止尝试了多个杰克逊jar如上所示,我试过传递接受application / xml,我试过生产=application / xml
但没有任何效果。
请告诉我上述配置中缺少的内容。它是罐子或一些注释或xml配置???
So far tried multiple jackson jar as highlighted above, i tried passing accept application/xml, i tried produced="application/xml" but nothing worked. Kindly let me know what i missing in above configuration. is it jars or some annotations or xml configuration ???
推荐答案
在您的调度员中添加此配置
add this config in your dispatcher
<mvc:annotation-driven
content-negotiation-manager="contentManager" />
<bean id="contentManager"
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false" />
<property name="ignoreAcceptHeader" value="false" />
<property name="defaultContentType" value="application/json" />
<property name="useJaf" value="false" />
</bean>
这篇关于Spring Rest Service不支持XML响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!