首页 文章

Spring Rest Service不支持XML响应

提问于
浏览
0

我正在尝试创建spring restful webservice应用程序 . 我需要设计服务,它将返回json和xml . 目前默认情况下,json作为响应返回,但我也想返回xml .

If i hit : http://localhost:8087/RestServices/rest/profile/ 233 或233 .json

>> 我按预期得到了json的回应

but if hit : http://localhost:8087/RestServices/rest/profile/ 233.xml

>> 我收到错误说 HTTP Status - 406 此请求标识的资源只能生成具有根据请求"accept"标头不可接受的特征的响应 .

Technology Details : Spring 4.0 Hibernate 4.0 Spring MVC . RestController .

Dependency/Jars Details :

ANTLR-2.7.7.jar

aopalliance-1.0.jar

com.mysql.jdbc_5.1.5.jar

共享记录-1.2.jar

dom4j的-1.6.1.jar

休眠公地的注解,4.0.5.Final.jar

休眠核心,4.3.6.Final.jar

休眠-的EntityManager,4.3.6.Final.jar

冬眠,JPA-2.1-API 1.0.0.Final.jar

休眠 - 验证 - 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

jackson-xc-1.9.2.jar

jandex-1.1.0.Final.jar

Javassist进行-3.18.1-GA.jar

jaxb-api-2.2.jar

JBoss的日志记录,3.1.3.GA.jar

JBoss的日志记录的注解,1.2.0.Beta1.jar

JBoss的事务,api_1.2_spec-1.0.0.Final.jar

log4j的-1.2.17.jar

的logback经典-0.9.jar

的logback核-0.9.6.jar

servlet的api.jar文件

SLF4J-API-1.6.1.jar

SLF4J-log4j13-1.0.1.jar

slf4j.jar

spring AOP-4.0.0.RELEASE.jar

spring 方面,4.0.0.RELEASE.jar

spring beans ,4.0.0.RELEASE.jar

spring 集结SRC-4.0.0.RELEASE.jar

spring 上下文4.0.0.RELEASE.jar

spring 上下文支持,4.0.0.RELEASE.jar

spring 芯4.0.0.RELEASE.jar

spring 数据公地1.6.3.RELEASE.jar

spring 数据JPA-1.4.1.RELEASE.jar

spring 表达-4.0.0.RELEASE.jar

spring 框架-BOM-4.0.0.RELEASE.jar

spring 仪器4.0.0.RELEASE.jar

spring 仪器Tomcat的4.0.0.RELEASE.jar

spring JDBC-4.0.0.RELEASE.jar

Spring 天了jdbc.jar

spring JMS-4.0.0.RELEASE.jar

spring 消息传递4.0.0.RELEASE.jar

spring ORM-4.0.0.RELEASE.jar

spring OXM-4.0.0.RELEASE.jar

spring - 测试 - 4.0.0.RELEASE.jar

spring -TX-4.0.0.RELEASE.jar

spring 网络4.0.0.RELEASE.jar

spring webmvc-4.0.0.RELEASE.jar

spring webmvc的portlet-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>

到目前为止尝试了多个 Jackson jar如上所示,我尝试传递接受application / xml,我试过 生产环境 =“application / xml”但没有任何效果 . 请告诉我上述配置中缺少的内容 . 它是 jar 或一些注释或xml配置???

2 回答

  • 0

    你需要告诉spring控制器方法可以生成JSON和XML . 这是使用:

    @RequestMapping(... produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
    

    比spring更接受XML请求 .

  • 0

    在您的调度程序中添加此配置

    <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>
    

相关问题