0

I have 406 error while using spring restful, I have already tried most answers, but for some reason, nothing works for me

Here are my configuration:

web.xml

    <display-name>QndA Hub</display-name>

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

<servlet-mapping>
    <servlet-name>rest</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

rest-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans ....
    <context:component-scan base-package="com.qnahub" />
    <tx:annotation-driven transaction-manager="transactionManager" />
</beans>

application-context.xml

    <context:annotation-config />

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="***" />
    <property name="username" value="***" />
    <property name="password" value="***" />
</bean>

<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="configLocation" value="classpath:hibernate.cfg.xml" />
    <property name="dataSource" ref="dataSource" />
</bean>

<bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
      p:packagesToScan="com.qnahub"
      p:dataSource-ref="dataSource"
>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="generateDdl" value="true" />
            <property name="showSql" value="true" />
        </bean>
    </property>
</bean>

<!-- Transactions -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

Controller

@RestController("rest")
public class AuthenticationRestAPI {

    @RequestMapping(path = "/login", produces = {"application/json"})
    public @ResponseBody LoginResponse login(){
        LoginResponse loginResponse = new LoginResponse();
        return loginResponse;
    }

From answer in other post I tried: - Removing @rResponseBody - 404 - headers="Accept=/" - change RestController to Controller - I have used Advanced rest client with application/json - POST/GET wasn't helpful too

I'm using Advanced REST Client, the response of the error is:

Response header

Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Language: en
Content-Length: 1067
Date: Sun, 30 Oct 2016 07:04:20 GMT

Response Body

<html><head><title>Apache Tomcat/7.0.47 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 406 - </h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/7.0.47</h3></body></html>

using jackson 2.8.4 spring 4.3.3 2 with spring-webmvc With String it works fine, but the output is with Content-Type: text/plain;charset=ISO-8859-1

Please help.

Mark T
  • 773
  • 4
  • 10
  • 22
  • Can you print the loginResponse in the console and provide what is the output ? I would like to see what's is going out (loginResponse) from the controller class ? – Vasu Oct 30 '16 at 06:01
  • if you mean system out, it gives: full name com.qnahub.data_managers.rest_api.response.LoginResponse@7b9796f8 If I replace it with a String it will work fine but the output is Content-Type: text/plain;charset=ISO-8859-1 – Mark T Oct 30 '16 at 06:41
  • I mean print the details of the response elements to console and provide to check that you are able to generate the response data – Vasu Oct 30 '16 at 06:43
  • Currently I haven't implemented the client, So I added to the question the response from Advanced REST Client, Thanks – Mark T Oct 30 '16 at 07:08

1 Answers1

0

Finally I had a break, I was missing

<mvc:annotation-driven />

I was sure I had it but instead I had the one with <tx: ..., and <context:... But the needed one was missing.

Mark T
  • 773
  • 4
  • 10
  • 22