使用 HttpMessageConverters 处理控制器中的 HTML 和非 HTML 表示 [英] Handle HTML and non-HTML representations in controller with HttpMessageConverters
问题描述
我有一个用于 RESTful 资源 (ServiceDirectoryController
) 的 Spring MVC 控制器,我想为 HTML(网页)和非 HTML(网络服务)表示提供服务.我想使用视图名称和 JSP 来生成 HTML 表示,但 HttpMessageConverter
用于其他表示.我该怎么做?
I have a Spring MVC controller for a RESTful resource (ServiceDirectoryController
), which I want to serve both HTML (web page) and non HTML (web service) representations. I want to use view names and JSP to generate the HTML representation, but HttpMessageConverter
s for the other representations. How do I do that?
我已经为非 HTML 表示实现了请求处理方法.我的内部(域模型)表示是 ThingNames
,为此我有合适的消息转换器,因此请求处理方法很简单:
I've implemented the request handling method for the non-HTML representations. My internal (domain-model) representation is ThingNames
, for which I have suitable message converters, so the request handling method is simply:
@RequestMapping(value = { "/directory/" }, method = RequestMethod.GET)
@ResponseBody
public ThingNames retrieveThings() {
...
}
现在我想为JSP页面添加请求处理方法.所以我需要一个方法来指示视图名称并提供合适的模型.所以,显而易见的事情是:
Now I want to add the request handling method for the JSP page. So I need a method that indicates the view name and provides a suitable model. So, the obvious thing to do is this:
@RequestMapping(value = { "/directory/" }, method = RequestMethod.GET)
public String retrieveThings(Map< String, Object > model) {
...
}
以视图名称作为返回值.
with the view name as the return value.
但这行得通吗?Spring 是否足够聪明,可以确定 HTML 表示应该由第二种方法处理,而对所有其他表示使用第一种方法?如果没有,我该如何处理 JSP/HTML 和非 JSP/HTML 表示?
But will that work? Is Spring clever enough to work out that the HTML representation should be handled by the second method, and use the first method for all other representations? And if not, how do I go about handling both JSP/HTML and non JSP/HTML representations?
推荐答案
根据 Paul Chapman 的 Spring 博客文章,如果我设置了 PPA 内容协商策略,我的控制器将按所写的那样工作 .也就是说,如果我将 Spring 配置为按以下偏好的降序考虑有关所需内容类型的线索:
According to a Spring blog post by Paul Chapman, my controller will work as written if I set up a PPA Content Negotiation Strategy. That is, if I configure Spring to consider clues about the wanted content type in the following descending order of preference:
- 路径扩展名(
.htm
、.tsv
等) - 请求参数(
?format=htm
、?format=xls
) - 接受标头(正确的 HTTP 内容协商,遗憾的是在 HTML 浏览器中效果不佳).
此外,Spring 提供的 ContentNegotiationManagerFactoryBean
就是按照这个顺序运行的.
What is more, the Spring provided ContentNegotiationManagerFactoryBean
operates in just that order.
此外,您必须在控制器中注释处理程序方法,以便 Spring 知道这些方法应该使用 consumes
和 produces
值来处理 HTML 内容>@RequestMapping 注释.也就是说,而不是简单地写
In addition, you must annotate the handler methods in the controller so Spring knows that those methods should handle HTML content, using the consumes
and produces
values of the @RequestMapping
annotation. That is, instead of simply writing
@RequestMapping(value = { "/directory/" }, method = RequestMethod.GET)
@ResponseBody
public String retrieveThings(Map< String, Object > model) {
...
}
写
@RequestMapping(value = { "/directory/" }, method = RequestMethod.GET,
produces = MediaType.TEXT_HTML_VALUE)
@ResponseBody
public String retrieveThings(Map< String, Object > model) {
...
}
这篇关于使用 HttpMessageConverters 处理控制器中的 HTML 和非 HTML 表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!