Java HttpMediaTypeNotAcceptableException:找不到可接受的表示 spring mvc 4.1.5 with com.fasterxml 2.5.1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28912462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:01:18  来源:igfitidea点击:

HttpMediaTypeNotAcceptableException: Could not find acceptable representation spring mvc 4.1.5 with com.fasterxml 2.5.1

javajsonspringspring-mvcHymanson

提问by 000

Help - what else can I do to fix this error? org.springframework.web.HttpMediaTypeNotAcceptableException:: Could not find acceptable representation.

帮助 - 我还能做些什么来修复这个错误?org.springframework.web.HttpMediaTypeNotAcceptableException:: 找不到可接受的表示。

I think my project is configured properly to handle json restful requests. In the past several days, I have read and applied various suggestions to no avail. Are there any more ideas on what else I should be doing differently?

我认为我的项目配置正确,可以处理 json restful 请求。在过去的几天里,我阅读并应用了各种建议,但无济于事。还有什么我应该做的不同的想法吗?

I am using Spring MVC 4.1.5 and com.fasterxml 2.5.1.

我正在使用 Spring MVC 4.1.5 和 com.fasterxml 2.5.1。

A portion of my pom.xml

我的 pom.xml 的一部分

 <dependency>
    <groupId>com.fasterxml.Hymanson.core</groupId>
    <artifactId>Hymanson-annotations</artifactId>
    <version>2.5.1</version>
    <type>bundle</type>
 </dependency>
 <dependency>
    <groupId>com.fasterxml.Hymanson.core</groupId>
    <artifactId>Hymanson-core</artifactId>
    <version>2.5.1</version>
    <type>bundle</type>
 </dependency>
 <dependency>
    <groupId>com.fasterxml.Hymanson.core</groupId>
    <artifactId>Hymanson-databind</artifactId>
    <version>2.5.1</version>
    <type>bundle</type>
 </dependency>

Here is my part of my web.xml

这是我的 web.xml 的一部分

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/appServlet/servlet-context.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

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

Here is part of my servlet-context.xml

这是我的 servlet-context.xml 的一部分

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

<context:component-scan base-package="com.losgatos">
    <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>   

Here is my controller

这是我的控制器

package com.losgatos.controllers;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.losgatos.User;

@RestController
@RequestMapping( "user" )
public class UserController { 

    @RequestMapping( value = "data", produces="application/json" )
    public User getUser(){
        return new User();
    }   
}

Here is the User User POJO

这是用户用户 POJO

package com.losgatos;

import java.util.EnumSet;

public class User {

    public User(){
        id = 0;
        age = 22;
        name = "Titus Feng"; 
        alias = "tornado tie";
        roles = EnumSet.of( Role.NORMAL, Role.NEW );
    }

    private int id, age;
    private String name, alias;
    private EnumSet<Role> roles;

    //added getters and setters here

    public enum Role{
        NEW, NORMAL, ADMIN, MEMBER, DORMANT 
    }   
}

采纳答案by 000

I have gotten it to work finally by:
removing <type>bundle</type>from my fasterxml.Hymanson pom.xml entries
rerunning my pom.xml file
cleaning the project build (project -> clean)
cleaning and restarting the server

我终于通过以下方式让它工作了:从我的fasterxml.Hymanson pom.xml条目中
删除 重新运行我的pom.xml文件 清理项目构建(项目->清理) 清理并重新启动服务器<type>bundle</type>



回答by Abhishek Nayak

In your spring context configuration file(dispatcher-servlet.xml) you have:

在您的 spring 上下文配置文件(dispatcher-servlet.xml)中,您有:

<context:component-scan base-package="com.losgatos">
    <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>

and your controller is annotated with:

并且您的控制器注释为:

@RestController
@RequestMapping( "user" )
public class UserController { 
...
}

your controller class will not be loaded to container at all. BTW as per your exception message:

您的控制器类根本不会加载到容器中。顺便说一句,根据您的异常消息:

I am getting the error

org.springframework.web.HttpMediaTypeNotAcceptableException:: Could not find acceptable representation.

我收到错误

org.springframework.web.HttpMediaTypeNotAcceptableException:: 找不到可接受的表示。

This exception raises when you have not registered the following in context file:

当您尚未在上下文文件中注册以下内容时,会引发此异常:

  • <mvc:annotation-driven />(you have not registered on your context file)
  • If required JAR's are not present on classpath, (you already have it)
  • When ENUM is used, annotate your ENUM class with @JsonFormat(shape= JsonFormat.Shape.OBJECT)
  • <mvc:annotation-driven />(您尚未在上下文文件中注册)
  • 如果所需的 JAR 不在类路径中,(您已经有了)
  • 使用 ENUM 时,用 @JsonFormat(shape= JsonFormat.Shape.OBJECT)

EDIT: tested working fine for me, with Spring 4.0.1.RELEASE and below single JSON dependency:

编辑:测试对我来说工作正常,使用 Spring 4.0.1.RELEASE 和以下单个 JSON 依赖项:

com.fasterxml.Hymanson.core Hymanson-databind 2.4.3

com.fasterxml.Hymanson.core Hyman逊数据绑定 2.4.3

and controller class:

和控制器类:

@RestController
@RequestMapping("/u")
public class UserRestController {

    @RequestMapping( value = "/data", produces="application/json" )
    public RestUser getUser(){
        return new RestUser();
    } 
}

got:

得到了:

{"id":0,"age":22,"name":"Titus Feng","alias":"tornado tie","roles":["NEW","NORMAL"]}

Suggestion, change this:

建议,改成这样:

<context:component-scan base-package="com.losgatos">
    <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>

to:

到:

<context:component-scan base-package="com.losgatos">
    <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.RestController" />
</context:component-scan>

回答by Brian Clozel

Spring Framework 4.1 raised some dependencies requirements; you need to upgrade Hymanson to version 2.3 or later. Note that the artifact groupId changed to com.fasterxml.

Spring Framework 4.1 提出了一些依赖需求;您需要将 Hymanson 升级到 2.3 或更高版本。请注意,工件 groupId 更改为com.fasterxml.