简体   繁体   English

消息转换器在春季无法正常工作

[英]Message converter not working in spring

Hi i am making a web application using spring jpa and hibernate. 嗨,我正在使用Spring JPA和Hibernate制作Web应用程序。 In my servlet.xml file i have placed the following lines of code 在我的servlet.xml文件中,我放置了以下代码行

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">


  <mvc:annotation-driven>
    <mvc:message-converters register-defaults="true">
        <beans:bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <beans:property name="prefixJson" value="true"/>
            <beans:property name="supportedMediaTypes" value="application/json"/>
            <beans:property name="objectMapper">
                <beans:ref bean="JacksonObjectMapper" />
            </beans:property>
        </beans:bean>   
    </mvc:message-converters>
</mvc:annotation-driven>

<context:component-scan base-package="com.abc.*.controller" />


<util:properties id="portalProperties" location="classpath:portal.abc.properties"></util:properties>
<util:properties id="imageProperties" location="classpath:image.properties"></util:properties>
<tx:annotation-driven />


<task:executor id="asyncExecutor" pool-size="25"/>
<task:annotation-driven executor="asyncExecutor" /> 


<annotation-driven  conversion-service="applicationConversionService" />    

<resources mapping="/resources/**" location="/resources/" />



<beans:bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
  <beans:property name="mediaTypes">
    <beans:map>
      <beans:entry key="html" value="text/html"/>
      <beans:entry key="json" value="application/json"/>
    </beans:map>
  </beans:property>
  <beans:property name="viewResolvers">
    <beans:list>
      <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>
    </beans:list>
  </beans:property>
  <beans:property name="defaultViews">
    <beans:list>
      <beans:bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
        <beans:property name="prefixJson" value="true"/>
      </beans:bean>
    </beans:list>
  </beans:property>
</beans:bean>


<beans:bean id="JacksonObjectMapper" class="com.fasterxml.jackson.databind.ObjectMapper" />

<beans:bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
    <mvc:resources mapping="/images/**" location="/images/" />
    <mvc:resources mapping="/css/**" location="/css/" />
    <mvc:resources mapping="/style.css" location="/style.css" />
    <mvc:resources mapping="/**" location="/" />

<beans:bean id="validator"
    class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />

<beans:bean id="applicationConversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
    <beans:property name="converters">
    <beans:list>
        <beans:bean class="com.abc.util.StringTrimmingConverter"/>
    </beans:list>
    </beans:property>
</beans:bean>



<mvc:interceptors>
    <beans:bean id="webContentInterceptor" class="org.springframework.web.servlet.mvc.WebContentInterceptor">
            <beans:property name="cacheSeconds" value="0"/>
            <beans:property name="useExpiresHeader" value="false"/>
            <beans:property name="useCacheControlHeader" value="true"/>
            <beans:property name="useCacheControlNoStore" value="true"/>
        </beans:bean>     
</mvc:interceptors>

In the above file i have mentioned the message converter.But when i call for a POST service request putting some values it gives me 在上面的文件中,我提到了消息转换器。但是当我调用POST服务请求时,需要输入一些值给我

The server refused this request because the request entity is in a format not supported by the requested resource for the requested method. 服务器拒绝此请求,因为请求实体的格式不受请求的方法所请求的资源支持。

My pom.xml code snippet 我的pom.xml代码段

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

I am able to return json but unable to accept json in the request body. 我能够返回json,但无法在请求正文中接受json。 I have kept the 我保留了

Content-Type: application/json 内容类型:application / json

in my header while calling for the web service. 在调用Web服务时在我的标题中。 Can anyone please help me in understanding why my application is unable to accept the json data. 谁能帮我理解为什么我的应用程序无法接受json数据。 My controller code snippets 我的控制器代码段

@RequestMapping(value = "/saveTimings", method = RequestMethod.POST)
public @ResponseBody
Outlet saveTimings(@RequestBody Timing timing) throws Exception {

    return timing;
}

You've registered your Jackson converter to a wrong media type it should be 您已将Jackson转换器注册为错误的媒体类型

<beans:property name="supportedMediaTypes" value="application/json" />

instead, and add <beans:property name="prefixJson" value="false"/> 而是添加<beans:property name="prefixJson" value="false"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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