http 406,弹簧不可解析的媒体但是我有mvc注释,而jackson在我的类路径上

时间:2013-10-14 13:32:50

标签: json spring spring-mvc jackson

我有:

<context:annotation-config/>
<mvc:annotation-driven/>

在我的appContext中,我的pom包括jackson:

<dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>${org.codehaus.jackson-version}</version>
        </dependency>
      <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>${org.codehaus.jackson-version}</version>
        </dependency>

并且我的控制器在@ResponseBody的返回类型上有List<Long>,但我仍然收到带有get请求的nonparseablemedia 406错误,我缺少什么?

@RequestMapping(value = "/myUrl/{customerId}/{productId}/", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public @ResponseBody List<Long> myMethod(@PathVariable Long customerId, @PathVariable Double productId) {
        List<Long> result = fieldService.identifyField(customerId, productId);
        return result;
    }

并且它由单元测试调用(其中id是适当的变量):

MockHttpServletRequestBuilder mockHttpServletRequestBuilder
                = get("/myUrl/"+customerId+"/"+productId);

        MvcResult mvcResult = this.mockMvc.perform(mockHttpServletRequestBuilder)
                .andDo(print())
                .andExpect(status().isOk())
                .andReturn();

请求的打印显示正在找到方法但是406,argh!

Handler:
  Type = com.mycompany.MyController
  Method = public java.util.List<java.lang.Long>   uk.co.axiomtechsolutions.ipf.webapp.web.MyController.MyMethod(java.lang.Long,java.lang.Double)

Resolved Exception:
  Type = org.springframework.web.HttpMediaTypeNotAcceptableException

1 个答案:

答案 0 :(得分:1)

对于其他任何人来说,它似乎是由使用Double数据类型作为Url变量引起的...完全停止使得spring认为它是文件扩展名?将数据类型转换为整数意味着测试通过。