ContentNegotiatingViewResolver 电子商务平台多终端视图适配案例

113 篇文章 0 订阅
77 篇文章 0 订阅

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

肖哥弹架构 跟大家“弹弹” SpringBoot源码,需要代码关注

欢迎 点赞,点赞,点赞。

关注公号Solomon肖哥弹架构获取更多精彩内容

类结构设计

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,可以轻松地扩展应用程序以支持新的响应格式。
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Solomon_肖哥弹架构

你的欣赏就是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值