rss spring 接口_Spring 接口支持返回多种格式

本文详细介绍了如何使用SpringMVC配置接口以支持返回XML、JSON、HTML等多种格式。内容包括HttpHeader的Content-Type和Accept的区别、ContentNegotiationStrategy的判定方式、RequestMappings的工作原理以及HttpMessageConverter的默认转换器和自定义配置,同时提到了视图解析中的Freemarker页面渲染。
摘要由CSDN通过智能技术生成

1. 简介

本文介绍使用SpringMVC的后端服务如何通过配置来支持多种返回值类型(xml,json,html,excel)

2. 基础概念

2.1 HttpHeader中Content-Type和Accept设置的区别

Accept:接口要返回给客户端的数据格式

1curl --header 'Accept:application/json' http://localhost:8080/todo

Content-Type:客户端发送给服务器端的数据格式

1curl -X PUT --header 'Content-Type:application/json' -d '{"title":"周末日程","content":"睡觉"}' http://localhost:8080/todo

2.2 SpringMVC生成输出的两种方式

1) 当服务端使用Restful的方式,只为客户端的ajax或其他服务端请求提供数据时,通常会使用@ResponseBody来标识你的返回,这时候Spring使用HttpMessageConverter来把返回的对象格式化成所需的格式。

2) 当你需要提供表现层(比如:HTML),这时候SpringMVC使用ViewResolver来将处理你的返回。

有时候你的应用程序这两者都要提供

2.3 SpringMVC输出格式判定

很多时候为了支持多个系统或多个终端,你需要让相同的数据已不同的表现形式输出。

SpringMVC使用ContentNegotationStrategy来判定用户请求希望得到什么格式的数据。

ContentNegotationStrategy通过三种方式来识别用户想要返回什么样的数据通过HTTP Header的Accept:Accept:application/xml 优先级由上至下

请看如下配置

@Override

public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {

configurer.favorPathExtension(false)

.favorParameter(true)

.parameterName("mediaType")

.defaultContentType(MediaType.APPLICATION_JSON)

.mediaType("xml", MediaType.APPLICATION_XML)

.mediaType("html", MediaType.TEXT_HTML)

.mediaType("json", MediaType.APPLICATION_JSON);

}

在你工程的WebMvcConfig中加入以上配置,表示关闭URL后缀的规则,打开请求参数规则并设置请求参数为’mediaType’,默认的返回格式是json,还支持返回xml,html。

这三个组件是用来处理返回不同格式输出的关键Request Mappings: 决定不同的请求到不同的方法并返回不同的格式.

View Resolution: 根据类型返回合适的表示层.

HttpMessageConverters: 将request中的参数转换成java对象,将java对象转换成相应的输出格式到response.

2.4 RequestMappings

2.4.1 RequestMappingHandlerMapping

我们在spring中通常使用的就是RequestMappingHandlerMapping,根据RequestMappingInfo,细化匹配条件,整体的查找过程如下:

AbstractHandlerMethodMapping实现接口getHandlerInternal

1. 使用UrlPathHelper查找request对应的path

2. 查找path对应的HandlerMethod

2.1 从urlMap中直接等值匹配查找匹配条件RequestMappingInfo

2.2 如果等值查找到匹配条件,将其添加到match条件中

2.3 如果没有找到匹配条件,使用所有的handlerMethod的RequestMappingInfo进行匹配

2.4 对匹配到的Match进行排序,取出最高优先级的Match,并核对是否是唯一的最高优先级

2.5 对匹配到条件,没有匹配到条件的两种情况,分别进行封装

3. 封装HandlerMethod,确保bean中存的是实例    ContentNegotiationManager其中提供了针对miniType的match条件比较,使框架可以匹配到最合适的处理方法。

2.5 HttpMessageConverter

2.5.1 The Default Message Converters

SpringMvc默认会加载下列HttpMessageConverters:

ByteArrayHttpMessageConverter – converts byte arrays

StringHttpMessageConverter – converts Strings

ResourceHttpMessageConverter – converts org.springframework.core.io.Resource for any type of octet stream

SourceHttpMessageConverter – converts javax.xml.transform.Source

FormHttpMessageConverter – converts form data to/from a MultiValueMap.

Jaxb2RootElementHttpMessageConverter – converts Java objects to/from XML (added only if JAXB2 is present on the classpath)

MappingJackson2HttpMessageConverter – converts JSON (added only if Jackson 2 is present on the classpath)

MappingJacksonHttpMessageConverter – converts JSON (added only if Jackson is present on the classpath)

AtomFeedHttpMessageConverter – converts Atom feeds (added only if Rome is present on the classpath)

RssChannelHttpMessageConverter – converts RSS feeds (added only if Rome is present on the classpath)

我们如果返回的是使用@ResponseBody来标识的,那么框架会使用HttpMessageConverter来处理返回值,默认的xmlCoverter不是特别好用,依赖返回实体对象上的@XmlRootElement注解,不是很方便所以引入辅助类库,并自定义MessageConverter这样可以直接将返回的对象处理成xml格式。

Gradle import library

compile group: 'org.springframework', name: 'spring-oxm', version: '4.3.9.RELEASE'

compile group: 'com.thoughtworks.xstream', name: 'xstream', version: '1.4.10'

configuration

@Override

public void configureMessageConverters(List> converters) {

converters.add(createXmlHttpMessageConverter());

super.configureMessageConverters(converters);

}

private HttpMessageConverter createXmlHttpMessageConverter() {

MarshallingHttpMessageConverter xmlConverter =

new MarshallingHttpMessageConverter();

XStreamMarshaller xstreamMarshaller = new XStreamMarshaller();

xmlConverter.setMarshaller(xstreamMarshaller);

xmlConverter.setUnmarshaller(xstreamMarshaller);

return xmlConverter;

}

2.6 View Resolution

2.6.1 页面render(freemarker)

当需要返回页面时就需要由合适的viewResolver来绘制画面,这里采用freemarker作为页面引擎。

Gradle import library

1compile("org.springframework.boot:spring-boot-starter-freemarker")

欢迎工作一到五年的Java工程师朋友们加入Java程序员开发: 854393687

群内提供免费的Java架构学习资料(里面有高可用、高并发、高性能及分布式、Jvm性能调优、Spring源码,MyBatis,Netty,Redis,Kafka,Mysql,Zookeeper,Tomcat,Docker,Dubbo,Nginx等多个知识点的架构资料)合理利用自己每一分每一秒的时间来学习提升自己,不要再用"没有时间“来掩饰自己思想上的懒惰!趁年轻,使劲拼,给未来的自己一个交代!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值