0

使用 Spring MVC 3.2,我在两个不同的控制器中定义了两个方法,并带有以下产生注释:

@RequestMapping(
    method = RequestMethod.GET, 
    produces={
        "application/vnd.myresource.v1+json", 
        "application/vnd.myresource.v1+xml"
    })
public ResponseEntity getResourceMethod_v1()
{
.......
}

@RequestMapping(
    method = RequestMethod.GET, 
    produces={
        "application/vnd.myresource.v2+json", 
        "application/vnd.myresource.v2+xml"
    })
public ResponseEntity getResourceMethod_v2()
{
.......
}

当我在请求标头中指定“接受:application/vnd.myresource.v1+json”时,Spring 正确映射到第一种方法。而且,当我在请求标头中指定“接受:application/vnd.myresource.v2+json”时,Spring 会正确映射到第二种方法。但是,当我没有在请求头中指定“accept:”时(请求默认为“accept:*/*”),Spring 映射到第一个方法。

如何将第二种方法指定为 Spring 的默认映射?

4

1 回答 1

1

我发现我可以将 MediaType.ALL_VALUE 添加到我想成为默认方法的生产块中。

@RequestMapping(
    method = RequestMethod.GET, 
    produces={
        "application/vnd.myresource.v1+json", 
        "application/vnd.myresource.v1+xml"
    })
public ResponseEntity getResourceMethod_v1()
{
.......
}

@RequestMapping(
    method = RequestMethod.GET, 
    produces={
        MediaType.ALL_VALUE,
        "application/vnd.myresource.v2+json", 
        "application/vnd.myresource.v2+xml"
    })
public ResponseEntity getResourceMethod_v2()
{
.......
}
于 2013-10-09T17:18:41.510 回答