0

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?

4
  • I believe that the problem is that your browser does not know how to display a List<UserType>. Commented Sep 9, 2014 at 19:21
  • @DwB you are right! However how can I incorporate that into the code, like make it able to be shown? Commented Sep 9, 2014 at 19:24
  • @DwB I would also want to allow the server to return json, which currently only allows xml. Any tips on doing so? Thank you! Commented Sep 9, 2014 at 19:25
  • Here is a tutorial for writing a REST web service with Spring. spring.io/guides/tutorials/rest Commented Sep 9, 2014 at 20:31

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.