2017-03-15 46 views
1

雖然發送從郵遞員請求它顯示我 HTTP狀態406 由該請求所標識的資源僅能夠用根據不可接受的特徵產生響應的到請求「接受」標題。 同樣在控制檯將其打印org.springframework.web.HttpMediaTypeNotAcceptableException異常HTTP狀態406 - org.springframework.web.HttpMediaTypeNotAcceptableException:無法找到可接受表示

錯誤--------- >>

13:30:24.143 [http-nio-8080-exec-4] DEBUG org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver - Resolving exception from handler [[email protected]]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation 
    13:30:24.145 [http-nio-8080-exec-4] DEBUG org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver - Resolving exception from handler [[email protected]]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation 
    13:30:24.145 [http-nio-8080-exec-4] DEBUG org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolving exception from handler [[email protected]]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation 
    13:30:24.146 [http-nio-8080-exec-4] DEBUG org.springframework.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling 
14:39:11.951 [http-nio-8080-exec-10] DEBUG org.springframework.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling 

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>xptraining</groupId> 
    <artifactId>xptraining</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>war</packaging> 
    <build> 
     <sourceDirectory>src</sourceDirectory> 
     <plugins> 
      <plugin> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.5.1</version> 
       <configuration> 
        <source>1.8</source> 
        <target>1.8</target> 
       </configuration> 
      </plugin> 
      <plugin> 
       <artifactId>maven-war-plugin</artifactId> 
       <version>3.0.0</version> 
       <configuration> 
        <warSourceDirectory>WebContent</warSourceDirectory> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
    <dependencyManagement> 
     <dependencies> 
      <dependency> 
       <groupId>org.glassfish.jersey</groupId> 
       <artifactId>jersey-bom</artifactId> 
       <version>${jersey.version}</version> 
       <type>pom</type> 
       <scope>import</scope> 
      </dependency> 
     </dependencies> 
    </dependencyManagement> 

    <dependencies> 
     <dependency> 
      <groupId>org.glassfish.jersey.containers</groupId> 
      <artifactId>jersey-container-servlet-core</artifactId> 
      <!-- use the following artifactId if you don't need servlet 2.x compatibility --> 
      <!-- artifactId>jersey-container-servlet</artifactId --> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>javax.servlet-api</artifactId> 
      <version>3.0.1</version> 
     </dependency> 
     <!-- uncomment this to get JSON support --> 
     <dependency> 
      <groupId>org.glassfish.jersey.media</groupId> 
      <artifactId>jersey-media-moxy</artifactId> 
     </dependency> 

     <!-- Spring dependencies --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-web</artifactId> 
      <version>4.3.2.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-orm</artifactId> 
      <version>4.3.2.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-core</artifactId> 
      <version>4.3.2.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.data</groupId> 
      <artifactId>spring-data-jpa</artifactId> 
      <version>1.8.2.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context</artifactId> 
      <version>4.3.2.RELEASE</version> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
      <version>4.3.2.RELEASE</version> 
     </dependency> 

     <dependency> 
      <groupId>com.fasterxml.jackson.core</groupId> 
      <artifactId>jackson-annotations</artifactId> 
      <version>2.7.4</version> 
     </dependency> 
     <dependency> 
      <groupId>com.fasterxml.jackson.core</groupId> 
      <artifactId>jackson-core</artifactId> 
      <version>2.7.4</version> 
     </dependency> 
     <dependency> 
      <groupId>com.fasterxml.jackson.core</groupId> 
      <artifactId>jackson-databind</artifactId> 
      <version>2.7.4</version> 
     </dependency> 
     <dependency> 
      <groupId>org.codehaus.jackson</groupId> 
      <artifactId>jackson-mapper-asl</artifactId> 
      <version>1.7.1</version> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-tx</artifactId> 
      <version>4.3.2.RELEASE</version> 
     </dependency> 


     <dependency> 
      <groupId>org.springframework.data</groupId> 
      <artifactId>spring-data-jpa</artifactId> 
      <version>1.0.0.RELEASE</version> 
     </dependency> 




     <!-- Hibernate dependencies --> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-core</artifactId> 
      <version>4.3.11.Final</version> 
     </dependency> 

     <!-- for JPA, use hibernate-entitymanager instead of hibernate-core --> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-entitymanager</artifactId> 
      <version>4.3.11.Final</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-c3p0</artifactId> 
      <version>4.3.11.Final</version> 
     </dependency> 

     <!-- MySql-Connector --> 
     <dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
      <version>5.1.32</version> 
     </dependency> 

     <dependency> 
      <groupId>org.apache.tomcat</groupId> 
      <artifactId>tomcat-dbcp</artifactId> 
      <version>7.0.55</version> 
     </dependency> 



    </dependencies> 
    <properties> 
     <jersey.version>2.24</jersey.version> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    </properties> 
</project> 

控制器 - ------ >>>

package com.xptraining.controller; 

import java.util.List; 
import org.springframework.http.MediaType; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestBody; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.ResponseBody; 

import com.xptraining.model.Cart; 
import com.xptraining.model.Status; 
import com.xptraining.services.CartService; 

/** 
* The Cart Controller Class 
* 
* @author Harshad Kenjale 
* 
*/ 

@Controller 
@RequestMapping("/cart") 
public class CartController { 

    @Autowired 
    CartService cartServices; 

    /** 
    * Get All records in Cart 
    * 
    * @return All Cart List 
    */ 

    @RequestMapping(value = "/list", method = RequestMethod.GET) 
    public @ResponseBody List<Cart> getCart() { 

System.out.println("@@@@@@@@@@@@hello i m from list [email protected]@@@@@@@@@@"); 

      List<Cart> cartList = null; 
     try { 
      cartList = cartServices.getCartList(); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     /*String joined = Joiner.on("\t").join(cartList);*/ 
     return cartList ; 
    } 

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> 
    <display-name>xptraining</display-name> 

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

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

    <servlet> 
     <servlet-name>dispatcherServlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/rest.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

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



</web-app> 

rest.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
    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/tx 
     http://www.springframework.org/schema/tx/spring-tx.xsd 
     http://www.springframework.org/schema/data/jpa 
     http://www.springframework.org/schema/data/jpa/spring-jpa.xsd 
     "> 
<context:component-scan base-package="com.xptraining" /> 

<!-- Enable annotation driven transaction management --> 

<jpa:repositories base-package="com.xptraining.repository" /> 

     <context:annotation-config /> 

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

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

<tx:annotation-driven transaction-manager="transactionManager" /> 
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory" /> 
    </bean> 

</beans> 
+0

您對jackson-databind有依賴關係,在您的pom中定義了兩次,並且您正在使用與jackson核心版本不同的版本。刪除其中一個jackson-databind依賴關係,並將版本更改爲2.7.4。 – alfcope

+0

@alfcope我改變了,但仍然顯示我同樣的錯誤。謝謝 – y2k

+0

請顯示配置。你有沒有註冊傑克遜消息轉換器?並審查你的POM,你的JPA依賴關係也是重複的。 – alfcope

回答

1

您沒有註冊驅動你的背景註釋。將<mvc:annotation-driven/>添加到您的rest.xml文件。

Enabling the MVC Java Config or the MVC XML Namespace

不要忘記你的XML頭添加了MVC命名空間:

xmlns:mvc="http://www.springframework.org/schema/mvc" 
xsi:schemaLocation="... 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 

順便問一下,你正在使用Spring 4.3,但您的命名空間指向3.0。

+0

其工作很好,非常感謝。 – y2k

+0

您正在使用Spring 4.3,但您的名稱空間指向3.0。 ??? – y2k

+0

是的,當定義模式位置時,你的定義就像'http:// www.springframework.org/schema/context/spring-context-3.0.xsd',但是你使用的是spring 4.3,所以爲了防萬一,它應該是'http:// www.springframework.org/schema/context/spring-context-4.3.xsd' – alfcope

相關問題