springweb 详解。
spring web架构图

从图中可以看出,
如果要对输出的内容进行重构,不需要视图的话,在handlerMethodReturnValueHandler里进行操作,可以重构这个对象,以达到自定义输出的结果。
需要视图的话,在viewResolver 进行处理,
servletInvocableHandlerMethod 对请求参数进行封装。
如果参数封装有问题,就去 servletInvocableHandlerMethod 打断点寻找。
问题1.想让输出 为json,xml,protobuf 怎么处理?
根据上图 当然是在HandlerMethodReturnValueHandler 中查找,
第一种,理所当然找到RequestResponseBodyMethodProcessor 它里面有一个messageConvert 所以增加一个 对应处理json,xml,protobuf的类即可。
第二种,可以自定义注解,自定义一个HandlerMethodReturnValueHandler 的实现类,来处理对应的注解,直接进行参数转换,或是用messageConvert进行转换即可。当然第一种有现成的方法,没必要用第二种
问题2.@RequestParam 注解什么意思
这个注解,默认是true,必须需要这个参数,如果你不传递,就报错给你看, 可以把参数改为false。
问题3,怎么确定一个输出使用什么MediaType
答:第一根据请求参数里的.accept(mediaTypes) 和 produces 里的mediatype 来求并集。.contentType(ProtobufHttpMessageConverter.PROTOBUF) 请求参数类型是这个,需要用对应的mediaType来解析参数。
一、filter 详解
1.HiddenHttpMethodFilter 将 post请求,转化为,请求参数里指定的方法
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException { HttpServletRequest requestToUse = request; if ("POST".equals(request.getMethod()) && request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE) == null) {
String paramValue = request.getParameter(this.methodParam);
if (StringUtils.hasLength(paramValue)) {
requestToUse = new HttpMethodRequestWrapper(request, paramValue);
}
} filterChain.doFilter(requestToUse, response);
}
界面用法 th:method将post 转换为delete方法。
<td><form class="form-inline" th:action="@{'/' + ${message.id}}" th:method="delete" method="delete">
<input type="submit" value="Delete1"/></form></td>

后台处理类。
@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
public String delete(@PathVariable("id") Message message, RedirectAttributes redirect) {
messageRepository.delete(message);
redirect.addFlashAttribute("globalMessage", "Message removed successfully");
return "redirect:/";
}
2.HttpPutFormContentFilter 如果请求是PUT或pach,将form里的参数提取到request的parameter里面。
protected void doFilterInternal(final HttpServletRequest request, HttpServletResponse response,
FilterChain filterChain) throws ServletException, IOException { if (("PUT".equals(request.getMethod()) || "PATCH".equals(request.getMethod())) && isFormContentType(request)) {
HttpInputMessage inputMessage = new ServletServerHttpRequest(request) {
@Override
public InputStream getBody() throws IOException {
return request.getInputStream();
}
};
MultiValueMap<String, String> formParameters = formConverter.read(null, inputMessage);
HttpServletRequest wrapper = new HttpPutFormContentRequestWrapper(request, formParameters);
filterChain.doFilter(wrapper, response);
}
else {
filterChain.doFilter(request, response);
}
}
会把form里的参数放入到request的parameters里面
<form action="/springmvc/testRest/3" method="post">
<input type="hidden" name="_method" value="put"/>
<input type="submit"value="Test Rest PUT"/>
</form>
现在一般的spring web应用里都会添加这两个过滤器。 OrderedHiddenHttpMethodFilter ,OrderedHttpPutFormContentFilter
springweb 详解。的更多相关文章
- (十)Maven依赖详解
1.何为依赖? 比如你是个男的,你要生孩子,呸呸呸...男的怎么生孩子,所以你得依赖你老婆,不过也不一定咯,你也可以依赖其她妹子. 我们在平时的项目开发中也是同理,你需要依赖一些东西才能实现相应的功能 ...
- @RequestMapping 用法详解之地址映射
@RequestMapping 用法详解之地址映射 引言: 前段时间项目中用到了RESTful模式来开发程序,但是当用POST.PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没 ...
- Spring jar包详解
Spring jar包详解 org.springframework.aop ——Spring的面向切面编程,提供AOP(面向切面编程)的实现 org.springframework.asm——spri ...
- Spring——jar包详解(转)
Spring——jar包详解 org.springframework.aop ——Spring的面向切面编程,提供AOP(面向切面编程)的实现 org.springframework.asm——spr ...
- 常见 jar包详解
常见 jar包详解 jar包 用途 axis.jar SOAP引擎包 commons-discovery-0.2.jar 用来发现.查找和实现可插入式接口,提供一些一般类实例化.单件的生命周期 ...
- Dubbo配置方式详解
Dubbo 是一个分布式服务框架,致力于提供高性能和透明化的 RPC 远程服务调用方案,是阿里巴巴 SOA 服务化治理方案的核心框架,每天为 2,000+ 个服务提供 3,000,000,000+ 次 ...
- SpringMVC RequestMapping 详解
SpringMVC RequestMapping 详解 RequestMapping这个注解在SpringMVC扮演着非常重要的角色,可以说是随处可见.它的知识点很简单.今天我们就一起学习Spring ...
- Spring MVC 详解之废话少说
<陈翔六点半之废话少说>.... Spring WEB MVC 的请求流程: Spring WEB MVC架构: 开始创建.配置gradle项目 1.在gralde项目中,选择SDK 和框 ...
- maven生命周期和插件详解
生命周期 什么是生命周期? maven的生命周期就是对所有的构建过程进行抽象和统一.maven从大量项目和构建工具中总结了一套高度完善的.易扩展的生命周期.这个生命周期包含项目的清理.初始化.编译.测 ...
随机推荐
- python之路之线程,进程,协程
一.线程和进程概述 1.python线程的Event 2.python线程其他和队列以及生产者消费者 3. 使用multprocessing创建进程 4.进程间数据共享方式——sharedmeory( ...
- Books Exchange (hard version)
The only difference between easy and hard versions is constraints. There are nn kids, each of them i ...
- AC3 Rematrix
当L R channel highly correlated时,AC3 encoder 使用rematrix技术压缩L/R的和和差. 原始信号为left,right,使用rematrix压缩信号为le ...
- (转载)Docker的boot2docker.iso镜像使用
原文路径:https://blog.csdn.net/jiangjingxuan/article/details/54908272#commentsedit 在Docker首次启动时需要下载的一个bo ...
- Java下载文件时文件名中的中文变成下划线,其他正常
将 utf-8 转换成 ISO8859-1 编码 response.addHeader("Content-Disposition", "attachment;filena ...
- 解决async 运行多线程时报错RuntimeError: There is no current event loop in thread 'Thread-2'
原来使用: loop = asyncio.get_event_loop()task = asyncio.ensure_future(do_work(checker))loop.run_until_co ...
- php一些实用的自制方法
时间戳转多久之前 function mdate($time = NULL) { $text = ''; $time = $time === NULL || $time > time() ? ti ...
- SqlDataReader阅读器关闭时尝试调用 HasRows 无效
SqlDataReader阅读器关闭时尝试调用 HasRows 无效 原创长白山上放羊娃 发布于2018-07-25 00:29:27 阅读数 538 收藏 展开 在SqlHelper中封装好的Sq ...
- 计算几何-UVa10652
This article is made by Jason-Cow.Welcome to reprint.But please post the article's address. 题意见白书,P2 ...
- 大数据的特征(4V+1O)
数据量大(Volume):第一个特征是数据量大,包括采集.存储和计算的量都非常大.大数据的起始计量单位至少是P(1000个T).E(100万个T)或Z(10亿个T). 类型繁多(Variety):第二 ...