0
votes

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.

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

>> i get json response as expected

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

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

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

commons-logging-1.2.jar

dom4j-1.6.1.jar

hibernate-commons-annotations-4.0.5.Final.jar

hibernate-core-4.3.6.Final.jar

hibernate-entitymanager-4.3.6.Final.jar

hibernate-jpa-2.1-api-1.0.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

jackson-xc-1.9.2.jar

jandex-1.1.0.Final.jar

javassist-3.18.1-GA.jar

jaxb-api-2.2.jar

jboss-logging-3.1.3.GA.jar

jboss-logging-annotations-1.2.0.Beta1.jar

jboss-transaction-api_1.2_spec-1.0.0.Final.jar

log4j-1.2.17.jar

logback-classic-0.9.jar

logback-core-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-aspects-4.0.0.RELEASE.jar

spring-beans-4.0.0.RELEASE.jar

spring-build-src-4.0.0.RELEASE.jar

spring-context-4.0.0.RELEASE.jar

spring-context-support-4.0.0.RELEASE.jar

spring-core-4.0.0.RELEASE.jar

spring-data-commons-1.6.3.RELEASE.jar

spring-data-jpa-1.4.1.RELEASE.jar

spring-expression-4.0.0.RELEASE.jar

spring-framework-bom-4.0.0.RELEASE.jar

spring-instrument-4.0.0.RELEASE.jar

spring-instrument-tomcat-4.0.0.RELEASE.jar

spring-jdbc-4.0.0.RELEASE.jar

spring-jdbc.jar

spring-jms-4.0.0.RELEASE.jar

spring-messaging-4.0.0.RELEASE.jar

spring-orm-4.0.0.RELEASE.jar

spring-oxm-4.0.0.RELEASE.jar

spring-test-4.0.0.RELEASE.jar

spring-tx-4.0.0.RELEASE.jar

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

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

2
Missing some bean config.Roman C
@RomanC Can you be specific ??? What exactly you mean ??user18181818
Why did you use annotation:driven?Roman C
to have explicit support for annotation-driven MVC controllers i.e. "@RequestMapping, @Controller"user18181818
Do you have jaxb libraries on classpath?Roman C

2 Answers

0
votes

You need to tell spring that the controller method can produce JSON as well as XML. This is done using :

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

Than spring will accept a request for XML.

0
votes

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>