当Hibernate4Module在Spring 4.0.8.RELEASE + Hibernate 4.3.7.Final + 2.3.0 com.fasterxml.jackson中使用时,Rest请求给出400个错误请求

时间:2015-03-25 12:15:31

标签: java rest jackson spring-4 hibernate-4.x

我有一个以下控制器

@RequestMapping(value = "/getOptimizeLoggerByCP/{cpId}", method = RequestMethod.POST)
    public @ResponseBody DataTablesResponse<MessageLogger> getOptimizeLoggerByCP(HttpServletRequest request, @PathVariable String cpId,@RequestBody String json) {
        }
}

我还在下面写了消息转换器,因为它没有加载hibernate惰性对象

<mvc:annotation-driven>
        <mvc:message-converters>
            <bean
                class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper">
                    <bean class="com.gridscape.ocppserver.dao.impl.HibernateAwareObjectMapper" />
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

public class HibernateAwareObjectMapper extends ObjectMapper {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public HibernateAwareObjectMapper() {
        Hibernate4Module hm = new Hibernate4Module();
        registerModule(hm);
    }
}

当我从applicationContext.xml中删除消息转换器时,Controller工作但是当我添加消息转换器时,控制器停止工作,因为当我请求控制器时,我收到了400个错误的请求。我从其余部分调用以下详细信息客户端

url:- getOptimizeLoggerByCP/REE001
method:- POST
Content-Type:- application/json
Body:-{
    "start": 1,
    "length": 1,
    "draw": 1
}

1 个答案:

答案 0 :(得分:0)

解决方法: - 我将@RequestBody String json替换为与String json对应的实际对象,即@RequestBody DataTablesRequest<MessageLogger> dataTableRequest

@RequestMapping(value = "/getOptimizeLoggerByCP/{cpId}", method = RequestMethod.POST)
        public @ResponseBody DataTablesResponse<MessageLogger> getOptimizeLoggerByCP(HttpServletRequest request, @PathVariable String cpId,@RequestBody DataTablesRequest<MessageLogger> dataTableRequest) {
        }

但我还是需要找出如何使用@RequestBody String json