本文介绍了HttpMediaTypeNotAcceptableException:找不到可接受的表示spring mvc 4.1.5 with com.fasterxml 2.5.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

帮助 - 我还能做些什么来解决此错误? org.springframework.web.HttpMediaTypeNotAcceptableException ::无法找到可接受的表示。

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

我认为我的项目已正确配置以处理json restful请求。在过去的几天里,我已阅读并应用各种建议无济于事。关于我应该采取哪些不同的做法还有其他想法吗?

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?

我使用的是Spring MVC 4.1.5和com.fasterxml 2.5.1。

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

我的pom.xml的一部分

A portion of my pom.xml

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

这是我的web.xml的一部分

Here is my part of my 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>

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

Here is part of my 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>

这是我的控制器

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();
    }
}

这是用户用户POJO

Here is the User User 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
    }
}


推荐答案

我最终通过以下方式让它工作:

从我的fasterxml.jackson pom.xml条目中删除< type> bundle< / type>

重新运行我的pom。 xml文件

清理项目构建(项目 - >清理)

清理并重新启动服务器

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

这篇关于HttpMediaTypeNotAcceptableException:找不到可接受的表示spring mvc 4.1.5 with com.fasterxml 2.5.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-07 05:15