掘金 后端 ( ) • 2024-06-28 00:46

ContentNegotiatingViewResolver用于内容协商的视图解析器。它可以根据客户端的 Accept 请求头来决定使用哪种视图(例如 JSON、XML 或 HTML)来响应请求。以下是使用 ContentNegotiatingViewResolver 的一个真实案例,包括业务场景、核心代码以及配置。

类结构设计

image.png

场景案例:

电子商务平台的产品API,该 API 需要支持多种格式的响应,以满足不同客户端的需求,如移动应用、桌面应用和第三方服务。

1. 定义视图控制器:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProductController {

    @GetMapping("/products")
    public Object getProducts() {
        // 假设这是获取产品列表的业务逻辑
        List<Product> products = productService.listAll();
        
        // 返回产品列表,让视图解析器决定响应格式
        return products;
    }
}

2. 配置 ContentNegotiatingViewResolver

Java 配置:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.accept.ContentNegotiationManager;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Bean
    public ContentNegotiatingViewResolver viewResolver(ContentNegotiationManager manager) {
        ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
        resolver.setContentNegotiationManager(manager);
        return resolver;
    }
    
    @Bean
    public ContentNegotiationManager contentNegotiationManager() {
        // 配置内容协商管理器
        // ...
    }
}

XML 配置:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <mvc:annotation-driven/>

    <mvc:view-resolvers>
        <mvc:content-negotiation>
            <!-- 配置视图解析器 -->
        </mvc:content-negotiation>
    </mvc:view-resolvers>

    <!-- 其他Spring MVC配置 -->
</beans>

3. 配置消息转换器:

为了支持不同的响应格式,你需要配置相应的 HttpMessageConverter

import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.xml.MarshallingHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.add(new MappingJackson2HttpMessageConverter()); // JSON
    converters.add(new MarshallingHttpMessageConverter()); // XML
    // 可以添加更多的消息转换器
}

4. 客户端请求:

客户端通过发送带有 Accept 头的请求来指定期望的响应格式。

GET /products
Accept: application/json

或者

GET /products
Accept: application/xml

5. 视图解析器解析视图:

ContentNegotiatingViewResolver 将根据客户端的 Accept 头选择合适的 HttpMessageConverter 来转换响应体。

总结:

  • ContentNegotiatingViewResolver 允许开发者根据客户端的 Accept 请求头动态地返回不同格式的响应。
  • 它提供了一种灵活的方式来支持多种数据格式,如 JSON、XML 和 HTML。
  • 结合 HttpMessageConverter,可以轻松地扩展应用程序以支持新的响应格式。