0

I'm trying to output Json response using spring mvc and hibernate. When i am trying to hit the url as per my controller design. I'm getting this error

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

I have added the jackson-mapper-asl in my pom.xml.

My Web.xml

        <?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>restApp</display-name>

         <servlet>
         <servlet-name>spring</servlet-name>
         <servlet-class>
                org.springframework.web.servlet.DispatcherServlet
          </servlet-class>
          <load-on-startup>1</load-on-startup>
         </servlet>

        <servlet-mapping>
         <servlet-name>spring</servlet-name>
         <url-pattern>/</url-pattern>
         </servlet-mapping>
        </web-app>

My dispatcher-servlet.xml(spring-servlet.xml)

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
   http://www.springframework.org/schema/beans     
   http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.1.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

    <context:component-scan base-package="model,controller,dao,service" />

    <tx:annotation-driven transaction-manager="hibernateTransactionManager" />
    <!--  
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        id="jspViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    -->
    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="mediaTypes">
          <map>
              <entry key="html" value="text/html"/>
              <entry key="json" value="application/json"/>
              <entry key="xml"  value="application/xml"/>
          </map>
        </property>
         <property name="viewResolvers">
            <list>
              <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
                <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
                <property name="prefix" value="/WEB-INF/jsp/"/>
                <property name="suffix" value=".jsp"/>
              </bean>
            </list>
        </property>
    </bean>

    <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
        id="dataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/world"></property>
        <property name="username" value="root"></property>
        <property name="password" value="Tpg@1234"></property>
    </bean>

    <bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
        id="sessionFactory">
        <property name="dataSource" ref="dataSource"></property>
        <property name="annotatedClasses">
            <list>
                <value>model.Book</value>   
                <value>model.Chapter</value>
                <value>model.Status</value>
            </list>
        </property>

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>            
        </property>
    </bean>

    <bean class="org.springframework.orm.hibernate4.HibernateTransactionManager"
        id="hibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>


    <bean id="persistenceExceptionTranslationPostProcessor"
        class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

    <bean id="BookDao" class="dao.BookDaoImpl"></bean>
    <bean id="ChapterDao" class="dao.ChapterDaoImpl"></bean>
    <bean id="Bookservice" class="service.BookServiceImpl"></bean>
    <bean id="ChapterService" class="service.ChapterServiceImpl"></bean>

</beans>

This is my Controller.

@Controller
@RequestMapping(value="/book")
public class BookController {

@Autowired
BookService bookService;

@Autowired
BookDao bookDao;

static final Logger logger = Logger.getLogger(BookController.class);

@RequestMapping(value="/add", method = RequestMethod.POST,headers="Accept=*/*")
public @ResponseBody
Status addBook(@RequestBody Book book){
    try{
        bookService.addBook(book);
        return new Status(1,"book added successfully");
    }catch(Exception e){
        return new Status(0, e.toString());
    }
}


@RequestMapping(value="/{bookId}",method = RequestMethod.GET,headers="Accept=*/*")
public @ResponseBody
Book getBook(@PathVariable("bookId")Integer bookId){
    Book book = null;
    try{
        book = bookService.getBookById(bookId);
    }catch(Exception e){
        e.printStackTrace();
    }
    System.out.println("book returned");
    System.out.println(book);
    return book;
}

@RequestMapping(value="/list",method=RequestMethod.GET,headers="Accept=*/*")
public @ResponseBody
List<Book> getBookList(){
    List<Book> bookList = null;
    try{
        bookList = bookService.getBookList();
    }catch(Exception e){
        e.printStackTrace();
    }
    System.out.println("bookList returned");
    System.out.println(bookList);
    return bookList;
}

@RequestMapping(value = "/delete/{bookId}", method = RequestMethod.GET,headers="Accept=*/*")
public @ResponseBody
Status deleteEmployee(@PathVariable("bookId") long bookId) {

    try {
        bookService.deleteBook(bookId);;
        return new Status(1, "Employee deleted Successfully !");
    } catch (Exception e) {
        return new Status(0, e.toString());
    }

}

Now when i'm trying to hit the url it is giving the error "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 ().

The Hibernate queries are running properly in the controller after hitting the url. It's only that I'm not getting the JSON response.

My POJO class(Book.java)

@Entity
@Table(name="book")
@JsonIgnoreProperties({"hibernateLazyInitializer","handler"})
public class Book implements Serializable{

@Id
@Column(name="bookId")
@GeneratedValue
private Integer bookId;

@Column(name="bookName")
private String bookName;

@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinTable(
        name="BookChapter",
        joinColumns = @JoinColumn(name="BOOK_ID"),
        inverseJoinColumns = @JoinColumn(name="CHAPTER_ID")
)
public Set<Chapter> chapter;

public Integer getBookId() {
    return bookId;
}

public void setBookId(Integer bookId) {
    this.bookId = bookId;
}

public String getBookName() {
    return bookName;
}

public void setBookName(String bookName) {
    this.bookName = bookName;
}

public Set<Chapter> getChapter() {
    return chapter;
}

public void setChapter(Set<Chapter> chapter) {
    this.chapter = chapter;
}

}
bablu
  • 601
  • 1
  • 9
  • 21
  • Try to remove the Asterix expression in your Accept-Header. It is better to define the supported media type, like: headers = "Accept=text/html" headers = "Accept=application/json" etc... – sven.kwiotek Jan 30 '15 at 11:18
  • When the accept headers seem ok, yet you see this issue it usually means that either the dependencies are wrong or missing, or there's something in the bean e.g. bad setters/getters that are wrong, I commented it here, http://stackoverflow.com/questions/26825276/spring-4-restcontroller-json-characteristics-not-acceptable-according-to-the-re/26826136#26826136. In your case, the jackson version is fine, so I suspect an issue with your Book pojo, can you paste it in the question – Master Slave Jan 30 '15 at 11:31
  • Okay. i have posted my POJO class here. – bablu Jan 30 '15 at 12:07
  • hard to tell, if I were you, i would add *chapter* property to JsonIgnoreProperties list, if it still doesn't work, than the problem is not in the conversion, if it does work, than you at least know where to look – Master Slave Jan 30 '15 at 12:27

0 Answers0