2013-11-25 23 views
0

如果这是我的控制器的方法:Spring MVC的 - 如何映射查找路径产生值

@RequestMapping(method=RequestMethod.GET, produces={"application/json", 
    "application/xml"}, value="/myService") 
@ResponseBody 
public ResponseEntity myMethod(...) { 
... 
} 

在另一个类我查找路径/myService和我想要得到的值在上面的方法中产生列表:application/jsonapplication/xml。是否有可能使用一些Spring MVC的类来访问产品列表中的值?

+1

你可以使用反射,但我不认为这是一个好主意。你的要求和目的是什么? –

+0

如果用户尝试访问错误媒体类型的服务并返回406,我想发回一些可能的建议。建议基于其当前请求和生产列表中定义的可能媒体类型。 – user86834

+0

这应该作为[HTTP选项请求](http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html)来实现。 –

回答

0

这个代码和平可能会做你想要什么(或者至少你点到正确的方向):

@ControllerAdvice 
public class DefaultControllerAdvice extends ResponseEntityExceptionHandler { 

    protected ResponseEntity<Object> handleHttpMediaTypeNotAcceptable(
      HttpMediaTypeNotAcceptableException ex, HttpHeaders headers, HttpStatus status, WebRequest request) { 
     return handleExceptionInternal(ex, "Supported media types: " + ex.getSupportedMediaTypes(), 
       headers, status, request); 
    } 

} 

关键的事实是,HttpMediaTypeNotAcceptableException包含处理程序映射器初始化支持的媒体类型。