使用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()
{
.......
}
当我在请求标头中指定“accept:application / vnd.myresource.v1 + json”时,Spring会正确映射到第一个方法。并且,当我在请求标头中指定“accept:application / vnd.myresource.v2 + json”时,Spring会正确映射到第二个方法。但是,当我在请求标头中没有指定“accept:”时(请求默认为“accept:* / *”),Spring会映射到第一个方法。
如何指定第二种方法作为Spring的默认映射?
答案 0 :(得分:0)
我发现我可以将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()
{
.......
}