美文网首页
ContentNegotiatingViewResolver 电

ContentNegotiatingViewResolver 电

作者: 肖哥弹架构 | 来源:发表于2024-06-28 07:59 被阅读0次

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,可以轻松地扩展应用程序以支持新的响应格式。

相关文章

  • SpringMVC-ContentNegotiatingView

    内容协商器(ContentNegotiatingViewResolver与contentNegotiationMa...

  • Spring多视图混合输出RestFul

    从结构上来说Spring的 ContentNegotiatingViewResolver是可以很方便的解决这个问题...

  • 20191202停电电电电

    今天下班回家,发现电灯打不开了,捣鼓了半天,发现是停电了。 结果查了下电费没交,电费交了之后,还是没有来电,给电网...

  • 新电电

    电动车老破小了,该换一个新的了。今天上午去爱玛电动车专卖店购买新车。看中一款又漂亮时尚又经济实惠的车,以旧换新13...

  • 《电商、电商、电商》

    我原本想写《必然》的读书笔记的,不过目前只看了三分之一左右,而且有很多似懂非懂的地方,我想了一下还是下一次写吧。 ...

  • 超自然现象与电

    一:电的等级 将电的等级划分为四级: 低位电——无感知电 常位电——感知电、常用电、工业用电等 高位电——普通闪电...

  • 混动&插电式混动&增程式

    插电式混动和增程式的区别: 1、增:正常电行驶,电不够油生电,还是电行驶。插:正常电行驶,电不够油行驶。 性质不同...

  • 回顾篇

    电商玩法 传统电商:淘宝,京东拼团电商:拼多多信任电商:快手兴趣电商:抖音,快手内容电商:小红书品牌电商:小米,华...

  • 又到保电时节

    自从保电成为常态,保电项目层出不穷,高考保电,中考保电,小升初保电,节日保电,重大政治任务保电等等,都可以向世界组...

  • 希望把真正的好东西与大家分享——VV商会对会员的专访系列之一

    关于VV商会:VV商会是一个电商人帮助电商人,电商人学习电商人,电商人成就电商人的平台。以"分享、交流、互助、尊重...

网友评论

      本文标题:ContentNegotiatingViewResolver 电

      本文链接:https://www.haomeiwen.com/subject/znoscjtx.html