MappingJacksonHttpMessageConverter不使用@EnableWebMvc

时间:2014-03-07 10:56:41

标签: java json spring

我在Spring 3.2.5中尝试在XML免费配置中设置JSON编组时遇到问题

这是我的配置类:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.pworld.mvc.controller")
@Import(DatabaseBeansConfig.class)
public class MainServletConfig extends WebMvcConfigurerAdapter   {
    private static Logger logger = LoggerFactory.getLogger("MainServletConfig");
    private List<HttpMessageConverter<?>> messageConverters; // Cached: this 

        @Override
        void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
          // Note: this overwrites the default message converters.
              converters.addAll(getMessageConverters());
        }

     /**
     * The message converters for the content types we support.
     *
     * @return the message converters; returns the same list on subsequent calls
     */
         private List<HttpMessageConverter<?>> getMessageConverters() {

              if (messageConverters == null) {
                 messageConverters = new ArrayList<HttpMessageConverter<?>>();
                 messageConverters.add(new MappingJacksonHttpMessageConverter());
              }
              return messageConverters;
         }
}

如果我调试此代码,我可以看到默认转换器中已有MappingJackson2HttpMessageConverter,因此不需要添加另一个吗?

我的控制器:

@RequestMapping(value = CARS_URL + HTML_SUFFIX, 
            method = RequestMethod.POST,
            consumes = {MediaType.APPLICATION_JSON},
            produces = {MediaType.APPLICATION_JSON})
@ResponseBody
public Car handlePagination(Model model, @RequestParam(value = PAG_NUM, required = true) int pagNum,
                                         @RequestParam(value = ITEMS_PER_PAGE, required = true) int itemsPerPage)
    {
        return new Car();
    }

因此,每当我使用该方法时,我都会得到一个

  

HTTP状态406 - 不可接受的响应

我尝试返回一个普通的字符串并且它有效,它只是JSON解析而不是踢... ...

我在我的课程中:

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.1.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.1.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.1.2</version>
        </dependency>   

我还缺少什么?我从来没有遇到过在XML <mvc:annotation-driven />配置中工作的问题....

请求正文

Request Url: http://localhost:9080/xxx.html?pagNum=1&itemsPerPage=8
Request Method: POST
Status Code: 406
Params: {}

请求标题

Accept: application/json
Content-Type: application/json
Connection: keep-alive
Origin: chrome-extension: //rest-console-id
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36

我从服务器获得406

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>GlassFish Server Open Source Edition  4.0  - Error report</title><style type="text/css"><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 406 - Not Acceptable</h1><hr/><p><b>type</b> Status report</p><p><b>message</b>Not Acceptable</p><p><b>description</b>The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.</p><hr/><h3>GlassFish Server Open Source Edition  4.0 </h3></body></html>

春季日志:

2014-03-18T15:42:48.790+0000|INFO: TRACE DispatcherServlet - Bound request context to thread: org.apache.catalina.connector.RequestFacade@39b08627
2014-03-18T15:42:48.790+0000|INFO: DEBUG DispatcherServlet - DispatcherServlet with name 'main' processing POST request for [/cars.html]
2014-03-18T15:42:48.790+0000|INFO: TRACE DispatcherServlet - Testing handler map [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping@6e078e68] in DispatcherServlet with name 'main'
2014-03-18T15:42:48.790+0000|INFO: DEBUG RequestMappingHandlerMapping - Looking up handler method for path /cars.html
2014-03-18T15:42:48.790+0000|INFO: DEBUG ExceptionHandlerExceptionResolver - Resolving exception from handler [null]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
2014-03-18T15:42:48.791+0000|INFO: DEBUG ResponseStatusExceptionResolver - Resolving exception from handler [null]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
2014-03-18T15:42:48.791+0000|INFO: DEBUG DefaultHandlerExceptionResolver - Resolving exception from handler [null]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
2014-03-18T15:42:48.791+0000|INFO: DEBUG DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'main': assuming HandlerAdapter completed request handling
2014-03-18T15:42:48.791+0000|INFO: TRACE DispatcherServlet - Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@39b08627
2014-03-18T15:42:48.791+0000|INFO: DEBUG DispatcherServlet - Successfully completed request
2014-03-18T15:42:48.791+0000|INFO: TRACE AnnotationConfigWebApplicationContext - Publishing event in WebApplicationContext for namespace 'main-servlet': ServletRequestHandledEvent: url=[/cars.html]; client=[0:0:0:0:0:0:0:1]; method=[POST]; servlet=[main]; session=[null]; user=[null]; time=[1ms]; status=[OK]
2014-03-18T15:42:48.791+0000|INFO: TRACE AnnotationConfigWebApplicationContext - Publishing event in Root WebApplicationContext: ServletRequestHandledEvent: url=[/cars.html]; client=[0:0:0:0:0:0:0:1]; method=[POST]; servlet=[main]; session=[null]; user=[null]; time=[1ms]; status=[OK]

已解决,我将此作为参考:https://spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc#annotated-account-class

我需要在@Configuration类中配置内容协商bean ....

感谢每个人!!

1 个答案:

答案 0 :(得分:0)

首先在应用程序服务器上启用spring的日志并检查更多详细信息,但由于缺少依赖性,此错误多次发生,尝试更改Jackson的版本。

相关问题