Spring mvc参数类型转换
1,需求
有时候我们接收到的参数为String类型的,但是我们需要将它们转化为其他类型的如:date类型,枚举类型等等,spring mvc为我们提供了这样的功能。
2,配置文件
在springmvc.xml配置文件中添加如下代码:
<mvc:annotation-driven conversion-service="conversionService" />
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.cmc.core.converters.StringToESportConverter" />
</list>
</property>
</bean>
别忘记上面那句,是注册转换器的。
3,添加StringToESportConverter类
package com.cmc.core.converters;
import org.springframework.core.convert.converter.Converter;
import com.gionee.xo.healthy.enums.ESport;
/**
* 配置spring mvc自动接收ESport
*
* @author chenmc
*/
public class StringToESportConverter implements Converter<String, ESport> {
@Override
public ESport convert(String source) {
String value = source.trim();
if ("".equals(value)) {
return null;
}
return ESport.get(Integer.parseInt(source));
}
}
4,添加ESport枚举类
package com.cmc.xo.healthy.enums;
import java.util.Locale;
import com.cmc.core.base.utils.I18N;
/**
* 运动枚举
*
* @author chenmc
* @date 2017年4月18日 下午8:32:33
*/
public enum ESport {
run("0"),//跑步
cycling("1");//骑行
private final String value;
private ESport(String v) {
this.value = v;
}
public String toString() {
return this.value;
}
public static ESport get(int v) {
String str = String.valueOf(v);
return get(str);
}
public static ESport get(String value) {
for (ESport e : values()) {
if (e.toString().equals(value)) {
return e;
}
}
return null;
}
public String getName() {
return I18N.getEnumName(this, Locale.CHINA);
}
}
转换主要用到了get(String value)这个方法
5,controller中代码
@ApiOperation(value="获取某用户单种运动的总信息", notes="返回某用户的运动总次数和总耗时总消耗")
@RequestMapping( value = {"/sports/{useruid:.{32}}/{type:\\d{1}}/sum"}, method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
@ResponseBody
public String get_count(HttpServletRequest request, @PathVariable String useruid, @PathVariable ESport type) {
return BaseResultHP.jsonResultSuccess(so.getSum(useruid, type));
}
url中传入的type为String类型的数字,而我接收参数@PathVariable ESport type
Spring mvc参数类型转换的更多相关文章
- spring mvc 参数类型转换
实现方式以字符串转Date为例说明: 全局配置 第一种:实现 Converter 接口 实现类: public class StringToDateConveter implements Conver ...
- spring mvc: 参数方法名称解析器(用参数来解析控制器下的方法)MultiActionController/ParameterMethodNameResolver/ControllerClassNameHandlerMapping
spring mvc: 参数方法名称解析器(用参数来解析控制器下的方法)MultiActionController/ParameterMethodNameResolver/ControllerClas ...
- spring mvc 参数
Struts(表示层)+Spring(业务层)+Hibernate(持久层) Struts: Struts是一个表示层框架,主要作用是界面展示,接收请求,分发请求. 在MVC框架中,Struts属于V ...
- spring mvc参数绑定
spring绑定参数的过程 从客户端请求key/value数据,经过参数绑定,将key/value数据绑定到controller方法的形参上.springmvc中,接收页面提交的数据是通过方法形参来接 ...
- Spring MVC参数封装传递
在Spring MVC中,前端JSP页面可以传递 基本类型(int,String).实体类型.包装类型.数组类型.集合类型(List.map )等. 假如在传递的类型中有 Date类型的字段,需要在 ...
- spring mvc 参数绑定
基础类型 原始类型:id必须要传,否则报错. @RequestMapping("/test") @ResponseBody public ResponseData test(int ...
- Spring MVC 参数必填项导致客户端报 HTTP 400 并且无法进入断点的问题
1.问题 Spring MVC 在参数上设置了必填项,post 请求时报 HTTP 400 并且未进入断点,如将“年龄”设置为了必填项: @RequestParam( value="age& ...
- Spring MVC 参数的绑定方法
在Spring MVC中,常见的应用场景就是给请求的Url绑定参数.本篇就介绍两种最最基本的绑定参数的方式: 基于@RequestParam 这种方法一般用于在URL后使用?添加参数,比如: @Req ...
- Spring MVC参数处理
使用Servlet API作为参数 HttpServletRequest HttpServletResponse HttpSession 使用流作为参数 总结 Spring MVC通过分析处理处理方法 ...
随机推荐
- Xpath Helper的使用
xPath Helper插件 xPath helper是一款Chrome浏览器的开发者插件,安装了xPath helper后就能轻松获取HTML元素的xPath,程序员就再也不需要通过搜索html源代 ...
- tensorflow卷积神经网络-【老鱼学tensorflow】
前面我们曾有篇文章中提到过关于用tensorflow训练手写2828像素点的数字的识别,在那篇文章中我们把手写数字图像直接碾压成了一个784列的数据进行识别,但实际上,这个图像是2828长宽结构的,我 ...
- C++11 带来的新特性 (4)—— 匿名函数(Lambdas)
1 语法 Lambdas并不是新概念,在其它语言中已经烂大街了.直接进入主题,先看语法: [ captures ] ( params ) specifiers exception attr -> ...
- C&C++ Calling Convention
tkorays(tkorays@hotmail.com) 调用约定(Calling Convention) 是计算机编程中一个比较底层的设计,它主要涉及: 函数参数通过寄存器传递还是栈? 函数参数从左 ...
- day19其他模块
collections模块 详细内容 http://www.cnblogs.com/Eva-J/articles/7291842.html 1.namedtuple: 生成可以使用名字来访问元素内容的 ...
- zookeeper(1)-简单介绍
参考: https://www.cnblogs.com/wuxl360/p/5817471.html zookeeper集群搭建 zookeeper集群原理和搭建 zookeeper集群搭建3 ...
- DICOM图像转出为bmp格式图像方法(matlab程序实现)
在matlab中用dicomread读取dicom文件后,生成一个MxN矩阵(对应图像像素个数),每个像素灰度数据是int16格式 但是bmp图像灰度是int8格式的(灰度范围0~255),所以若想把 ...
- Dapper 封装oracle底层访问数据库
如下代码,修改成只支持oracle: using System; using System.Collections.Generic; using System.Data; using System.L ...
- 【转】 微软在.NET官网上线.NET 架构指南
原文地址:http://www.cnblogs.com/shanyou/p/6676357.html. 微软在Visual Studio 2017 正式发布的时候也上线了一个参考应用https://g ...
- 用gulp-imageisux智图api压缩图片
➣ 智图平台是什么? 智图是腾讯ISUX前端团队开发的一个专门用于图片压缩和图片格式转换的平台,其功能包括针对png,jpeg,gif等各类格式图片的压缩,以及为上传图片自动选择最优的图片格式.同时, ...