406 Spring MVC Json, not acceptable according to the request “accept” headers

后端 未结 6 693
攒了一身酷
攒了一身酷 2020-12-05 19:39

have following details in my pom.xml

    
        org.codehaus.jackson
        jackson-cor         


        
相关标签:
6条回答
  • Maybe all fields of your POJO need Getter and Setter.

    I fixed it according to this issue: reference:Spring MVC - HttpMediaTypeNotAcceptableException

    And 406 is not a useful message to fix the bug. You should debug into the codes and see what the Exception is on earth.

    0 讨论(0)
  • 2020-12-05 20:18

    Add following jar to your pom.xml file which is required for Spring 4.1.*

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.4.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.4.1.1</version>
    </dependency>
    
    0 讨论(0)
  • 2020-12-05 20:18

    It worked for me with the below dependencies and @EnableWebMvc in my RestController, Please see that I just added jackson-databind dependency.

      <properties>
        <spring-version>4.2.1.RELEASE</spring-version>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>${spring-version}</version>  
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>${spring-version}</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>${spring-version}</version>
        </dependency>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-databind</artifactId>
          <version>2.5.3</version>
        </dependency>
      </dependencies>
    
    0 讨论(0)
  • 2020-12-05 20:22

    Use below dependency in your pom working with spring 4.

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.5.3</version>
    </dependency>
    
    0 讨论(0)
  • 2020-12-05 20:25

    Thanks it solved my issue. I finally ended up with a pom.xml like this:

    <!-- Json dependency -->
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.4.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.4.1.1</version>
        </dependency>
        <!-- Json dependency -->
    
    0 讨论(0)
  • 2020-12-05 20:26

    Get rid of this issues by adding @EnableWebMvc in Controller class.

    @Controller
    @RequestMapping("/API/course")
    @EnableWebMvc
    public class CourseController {
     @Autowired
     private com.item.DAO.CourseRepository courseRepository;
    
     @ResponseStatus(value=HttpStatus.OK)
     @RequestMapping(method=RequestMethod.GET)
     public @ResponseBody List<Course> getListOfCourse(){
        List<Course> courses = courseRepository.getListOfCourse();
        return courses ;
    }
    

    or add following line in xml configuration file if XML configuration is being used in your project.

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <mvc:annotation-driven />
    <bean>
    

    and add following dependencies in your pom.xml file:-

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.4.1.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.4.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.4.1</version>
        </dependency>
    
    0 讨论(0)
提交回复
热议问题