jamesdeath123
jamesdeath123

Reputation: 4608

return 406 error on spring mvc project - serializing List of object into xml and json

The following 406 is displayed:

The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers. 

From what I understand the problem, I lack the conversion for displaying data in xml or json format (Maybe I am wrong on this too..) However I don't know how can I do it... Any helps are appreciated!

The codes come as followed:

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
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("/userType")
public class UserTypeController {

    private static SessionFactory factory = new Configuration().configure().buildSessionFactory(); 

    /* Method to  READ all the employees */
    @RequestMapping(value = "/list.xml", method = RequestMethod.GET)
    public @ResponseBody List<UserType> listUserTypes( ) {
        Session session = factory.openSession();
        Transaction tx = null;
        try{
            tx = session.beginTransaction();
            List<UserType> userTypes = (List<UserType>)session.createQuery("FROM UserType").list();
            return userTypes;
        }catch (HibernateException e) {
            if (tx!=null) tx.rollback();
            e.printStackTrace(); 
            return new ArrayList<UserType>();
        }finally {
            session.close(); 
        }
    }
}

The hibernate mapping class is:

import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlAccessType;

@XmlRootElement(name = "UserType")
@XmlAccessorType(XmlAccessType.NONE)
public class UserType {
    @XmlElement(name = "UserTypeID")
    private int id;
    @XmlElement(name = "UserTypeName")
    private String name;

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

The spring configuration includes:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 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
        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">
    <context:component-scan base-package="com.pretech" />
    <mvc:annotation-driven />
  </beans>

and the web.xml is:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
    <display-name>SpringRestFulExample</display-name>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

With the above app, I am calling the following urls and they all return the same 406:

http://localhost:8080/SpringRestFulExample/userType/list.xml

http://localhost:8080/SpringRestFulExample/userType/list.json

http://localhost:8080/SpringRestFulExample/userType/list

EDIT:

I think actually the issue is about browser unable to accept List as input; however this should also be serialized into either xml or json, how can I do it?

Upvotes: 0

Views: 463

Answers (1)

Joshua Dickerson
Joshua Dickerson

Reputation: 36

You need to serialize your response using something like Jackson or GSON (for json) and then add an @Produces annotation to set the content-type headers in the response. Jersey and Jax-RS are also nice.

Upvotes: 1

Related Questions