简体   繁体   中英

Spring MVC Returning HTTP 406 Response

I want to Return a JSON Response from the controller. The Flow goes like this.

<dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.2-1003-jdbc4</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.25</version>
        </dependency>     
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
                <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-webmvc</artifactId>
                        <version>4.1.0.RELEASE</version>
                </dependency>
                <dependency>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-core</artifactId>
                        <version>3.6.0.CR1</version>
               </dependency>
               <dependency>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-entitymanager</artifactId>
                        <version>4.3.1.Final</version>
               </dependency>
               <dependency>
                        <groupId>junit</groupId>
                        <artifactId>junit</artifactId>
                        <version>4.4</version>
              </dependency>
              <dependency>
                        <groupId>org.codehaus.jackson</groupId>
                        <artifactId>jackson-mapper-asl</artifactId>
                        <version>1.8.5</version>
                        <scope>compile</scope>
              </dependency>
              <dependency>
                        <groupId>org.codehaus.jackson</groupId>
                        <artifactId>jackson-core-asl</artifactId>
                        <version>1.8.5</version>
                        <scope>compile</scope>
              </dependency>

Is my dependency tree.

@XmlRootElement
public class ResponseUser {
    private String contact_id;
    private String user_key;

    public String getContact_id() {
        return contact_id;
    }

    public void setContact_id(String contact_id) {
        this.contact_id = contact_id;
    }

    public String getUser_key() {
        return user_key;
    }

    public void setUser_key(String user_key) {
        this.user_key = user_key;
    }

    public String toString()
    {
      return "Contact_id"+":"+this.contact_id +","+"User_key"+":"+this.user_key;
    }

}

Is my pojo class.

@RequestMapping(value="/user",method=RequestMethod.POST,produces="application/json")
    public  @ResponseBody ResponseUser getUser(HttpServletRequest request,HttpServletResponse response)
    {
       ResponseUser user=new ResponseUser();
       user.setContact_id("sjlfjsld");
       user.setUser_key("skdjflsjdfl");
       System.out.println(user);
       return user;
    }

Is my controller.

<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"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="org.rwind.router" />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/Views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
        <context:annotation-config />
        <mvc:annotation-driven>
  <mvc:message-converters>
    <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
  </mvc:message-converters>
</mvc:annotation-driven>

</beans>

Is my Dispactcher-servlet.xml

<web-app version="3.0"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
         metadata-complete="false">

    <display-name>Softforge-api</display-name>

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

    <servlet-mapping>
        <servlet-name>api</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/api-servlet.xml</param-value>
    </context-param>

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


</web-app>

Is my web.xml file.

I don't know where i am doing wrong. But i am getting 406 Error. Guys please tell were i have to change.

A couple of things.

  1. You are mixing versions of hibernate
  2. You have Jackson 1 (codehaus) as a dependency but explicitly configure Jackson 2
  3. You are loading all of your beans twice.
  4. Configuration improvements

Mixing Hibernate Versions

<dependency>
     <groupId>org.hibernate</groupId>
     <artifactId>hibernate-core</artifactId>
     <version>3.6.0.CR1</version>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.3.1.Final</version>
</dependency>

In your dependency list you are mixing versions 3.6.0.CR1 (which isn't even a final release) and 4.3.1.Final. mixing jars of a framework is never a wise thing to do. You should be able to remove the dependency for hibernate-core as the hibernate-entitymanager has a dependency on it.

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.3.1.Final</version>
</dependency>

Jackson1 as a dependency

  <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.8.5</version>
        <scope>compile</scope>
  </dependency>
  <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>1.8.5</version>
        <scope>compile</scope>
  </dependency>

The dependencies you have in your dependency list are for Jackson 1, however you explicitly configure the MappingJackson2HttpMessageConverter which uses Jackson 2 not Jackson 1. Fix your dependencies for this

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.3.5</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.3.5</version>
</dependency>

You can also remove the explicit configuration of the MappingJackson2HttpMessageConverter as that will be automatically added when you are using <mvc:annotation-driven /> , this will save you a couple of lines of configuration.

<mvc:annotation-driven />

Loading beans twice

In your web.xml you have defined a DispatcherServlet named api which will, by default, will load the /WEB-INF/api-servlet.xml . You are again loading this configuration with the ContextLoaderListener . This duplicates all your beans and consumes additional resources. For now you can remove the ContextLoaderListener .

<?xml version="1.0" encoding="UTF-8"?> 
<web-app
    version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <display-name>Softforge-api</display-name>

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

    <servlet-mapping>
        <servlet-name>api</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

Configuration enhancements

It is recommended to use the version less XSD files instead of the versioned ones. So use spring-beans.xsd instead of spring-beans-3.0.xsd , this will ensure you are always using the most recent available version.

<context:annotation-config /> is already implied by <context:component-scan /> you can remove it.

The MappingJackson2HttpMessageConverter is automatically configured by <mvc:annotation-driven /> when it detects Jackson2 on the classpath, you can remove the explicit configuration.

<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"
    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="org.rwind.router" />
    <mvc:annotation-driven />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/Views/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM