SpringMVC(二)
七、Json处理
springMVC默认的Json解决方案选择是 Jackson,所以只需要导入jackson的jar,即可使用。
7.1 导入依赖
<!-- Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
<!-- FastJson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.54</version>
</dependency>
7.2 使用@ResponseBody
@Controller
public class JsonController{
@RequestMapping("/test1")
@ResponseBody //将handler的返回值,转换成json(jackson),并将json响应给客户端。
public User hello1(){
System.out.println("hello world");
User user = new User();
return user;
}
// @ResponseBody还可以用在handler的返回值上
@RequestMapping("/test2")
public @ResponseBody List<User> hello2(){
System.out.println("hello world");
List<User> users = Arrays.asList(new User(),new User());
return users;
}
// 如果返回值已经是字符串,则不需要转json,直接将字符串响应给客户端
@RequestMapping(value="/test3",produces = "text/html;charset=utf-8") //produces 防止中文乱码
@ResponseBody
public String hello2(){
System.out.println("hello world");
return "你好";
}
}
7.3 使用@RestController
Controller类上加了@RestController注解,等价于在类中的每个方法上都加了@ResponseBody
@Controller
@RestController
public class JsonController{
@RequestMapping("/test1")
public User hello1(){
System.out.println("hello world");
User user = new User();
return user;
}
//@ResponseBody还可以用在handler的返回值上
@RequestMapping("/test2")
public List<User> hello2(){
System.out.println("hello world");
List<User> users = Arrays.asList(new User(),new User());
return users;
}
}
7.4 使用@RequestBody
@RequestBody, 接收Json参数
7.4.1 定义Handler
class User{
private Integer id;
private String name;
private Boolean gender;
//set get
}
@RequestMapping("/users")
public String addUser(@RequestBody User user){//@RequestBody将请求体中的json数据转换为java对象
System.out.println("cap2");
System.out.println("Post user :"+user);
return "index";
}
7.4.2 Ajax发送json
var xhr = new XMLHttpRequest();
xhr.open("post","${pageContext.request.contextPath}/users?"+new Date().getTime());
xhr.setRequestHeader("content-type","application/json");//设置请求头
xhr.send('{"id":1,"name":"shine","gender":"true"}');//传递json串
//ajax
var user = {id:1,name:"shine"};
$.ajax({
url:'${pageContext.request.contextPath}/json2/test4',
type:'post',
contentType:"application/json",//声明请求参数类型为 json
data:JSON.stringify(user),// 转换js对象成json
success:function(ret){
console.log(ret);
}
});
7.5 Jackson常用注解
7.5.1 日期格式化
@JsonFormat(pattern=“yyyy-MM-dd HH:mm:ss”)
public class User{
private Integer id;
private String name;
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
private Date birth;
....
get/set
}
7.5.2 属性名修改
@JsonProperty(“new_name”)
public class User{
@JsonProperty("new_id") //不再使用原属性名,而是 "new_id"
private Integer id;
private String name;
....
get/set
}
输出的json:{“new_id”:xx,"name":"xx"}
7.5.3 属性忽略
@JsonIgnore
public class User{
private Integer id;
@JsonIgnore // 生成json时,忽略此属性
private String name;
....
get/set
}
输出json时: {"id":xx}
7.5.4 null和empty属性排除
Jackson 默认会输出null值的属性,如果不需要,可以排除。
@JsonInclude(JsonInclude.Include.NON_NULL) //null值 属性不输出
@JsonInclude(value= JsonInclude.Include.NON_EMPTY) // empty属性不输出( 空串,长度为0的集合,null值)
public class User{
private Integer id;
@JsonInclude(JsonInclude.Include.NON_NULL) // 若"name==null" 忽略此属性
private String name;
@JsonInclude(value= JsonInclude.Include.NON_EMPTY) // 若hobby长度为0或==null 忽略此属性
private List<String> hobby;
....
get/set
}
如果name=null,且 hobby长度为0,则输出json时:{"id":xx}
7.5.5 自定义序列化
@JsonSerialize(using = MySerializer.class) // 使用MySerializer输出某属性
public class User {
private Integer id;
private String name;
@JsonSerialize(using = MySerializer.class)
private Double salary = 10000.126;//在输出此属性时,使用MySerializer输出
....
get/set
}
则输出json时:{"id":xx,"name":"xxx","salary":10000.13}
public class MySerializer extends JsonSerializer<Double> {
// value即 Double salary的值
@Override
public void serialize(Double value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
// 将Double salary的值 四舍五入
String number = BigDecimal.valueOf(value).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
// 输出 四舍五入后的值
gen.writeNumber(number);
}
}
7.6 FastJson
如果不想使用Jackson,则也可以安装其他的 Json处理方案:FastJson
7.6.1 安装FastJson
<mvc:annotation-driven>
<!-- 安装FastJson,转换器 -->
<mvc:message-converters>
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<!-- 声明转换类型:json -->
<property name="supportedMediaTypes">
<list>
<value>application/json</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
7.6.2 使用
@ResponseBody @RequestBody @RestController 使用方法不变
7.6.3 常用注解
日期格式化:@JSONField(format=“yyyy/MM/dd”)
属性名修改:@JSONField(name=“birth”)
忽略属性:@JSONField(serialize = false)
包含null值:@JSONField(serialzeFeatures = SerializerFeature.WriteMapNullValue) 默认会忽略所有null值,有此注解会输出null
@JSONField(serialzeFeatures = SerializerFeature.WriteNullStringAsEmpty) null的String输出为""
自定义序列化:@JSONField(serializeUsing = MySerializer2.class)
public class User implements Serializable{
@JSONField(serialize = false)
private Integer id;
@JSONField(name="NAME",serialzeFeatures = SerializerFeature.WriteNullStringAsEmpty)
private String name;
@JSONField(serialzeFeatures = SerializerFeature.WriteMapNullValue)
private String city;
@JSONField(format="yyyy/MM/dd")
private Date birth;
@JSONField(serializeUsing = MySerializer2.class)
private Double salary;
...
}
public class MySerializer2 implements ObjectSerializer {
@Override
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType,
int features) throws IOException {
Double value = (Double) object; // salary属性值
String text = value + "元";// 在salary后拼接 “元”
serializer.write(text); // 输出拼接后的内容
}
}
FastJson中如果属性为空,则输出的json字符串中没有改属性。
new User(1,null,null,new Date(),100.5) ;输出json:
{NAME:"",city:null,“birth”:“2020/12/12”,“salary”:“100.5元”}
八、异常解析器
8.1 现有方案,分散处理
Controller中的每个Handler自己处理异常
此种处理方案,异常处理逻辑,分散在各个handler中,不利于集中管理
public String xxx(){
try{
...
}catch(Exception1 e){
e.printStackTrace();
return "redirect:/xx/error1";
}catch(Exception2 e){
e.printStackTrace();
return "redirect:/xx/error2";
}
}
8.2 异常解析器,统一处理
Controller中的每个Handler不再自己处理异常,而是直接throws所有异常。
定义一个“异常解析器” 集中捕获处理 所有异常
此种方案,在集中管理异常方面,更有优势!
public class MyExResolver implements HandlerExceptionResolver{
/**
* 异常解析器:主体逻辑
* 执行时刻:当handler中抛出异常时,会执行:捕获异常,并可以跳到错误页面
*/
@Override
public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex) {
ex.printStackTrace();//打印异常栈
//创建一个ModelAndView
ModelAndView mv = new ModelAndView();
//识别异常
if (ex instanceof Exception1) {
mv.setViewName("redirect:/xxx/error1");
}else if(ex instanceof Exception2){
mv.setViewName("redirect:/xxx/error2");
}else{
mv.setViewName("redirect:/xxx/error");
}
return mv;
}
}
<!-- 声明异常解析器 -->
<bean class="com.baizhi.exception.resolver.MyExResolver"></bean>
九、拦截器
重要但不紧急
作用:抽取C中的冗余功能
9.1 定义拦截器
执行顺序: preHandle–postHandle–afterCompletion
public class MyInter1 implements HandlerInterceptor{
//主要逻辑:在handler之前执行:抽取handler中的冗余代码
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
System.out.println("pre~~~");
/*
response.sendRedirect("/springMVC_day2/index.jsp");//响应
return false;//中断请求
*/
return true;//放行,后续的拦截器或handler就会执行
}
//在handler之后执行:进一步的响应定制
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("post~~");
}
//在页面渲染完毕之后,执行:资源回收
@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex)
throws Exception {
System.out.println("after~~");
}
}
9.2 配置拦截路径
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/inter/test1"/>
<mvc:mapping path="/inter/test2"/>
<mvc:mapping path="/inter/test*"/> <!-- test开头 -->
<mvc:mapping path="/inter/**"/> <!-- /** 任意多级任意路径 -->
<mvc:exclude-mapping path="/inter/a/**"/> <!--不拦截此路径-->
<bean class="com.baizhi.interceptor.MyInter1"></bean> <!--拦截器类-->
</mvc:interceptor>
</mvc:interceptors>
十、上传(非重点)
10.1 导入jar
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
10.2 表单
<form action="${pageContext.request.contextPath }/upload/test1" method="post"
enctype="multipart/form-data">
file: <input type="file" name="source"/> <br>
<input type="submit" value="提交"/>
</form>
10.3 上传解析器
<!-- 上传解析器
id必须是:“multipartResolver”
-->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 最大可上传的文件大小 byte 超出后会抛出MaxUploadSizeExceededException异常,可以异常解析器捕获 -->
<property name="maxUploadSize" value="2097152"></property>
</bean>
10.4 Handler
@RequestMapping("/test1")
public String hello1(String username,MultipartFile source,HttpSession session) {
//文件的原始名称
String filename = source.getOriginalFilename();
//定制全局唯一的命名
String unique = UUID.randomUUID().toString();
//获得文件的后缀
String ext = FilenameUtils.getExtension(filename);//abc.txt txt hello.html html
//定制全局唯一的文件名
String uniqueFileName = unique+"."+ext;
System.out.println("唯一的文件名:"+uniqueFileName);
//文件的类型
String type = source.getContentType();
System.out.println("filename:"+filename+" type:"+type);
//获得 upload_file的磁盘路径 ==> 在webapp目录下创建一个目录"upload_file",且此目录初始不要为空,否则编译时被忽略
String real_path = session.getServletContext().getRealPath("/upload_file");
System.out.println("real_path:"+real_path);
//将上传的文件,存入磁盘路径中
//source.transferTo(new File("d:/xxxx/xxxx/xx.jpg"))
source.transferTo(new File(real_path+"\\"+uniqueFileName));
return "index";
}
十一、下载(非重点)
11.1 超链
<a href="${pageContext.request.contextPath}/download/test1?name=Koala.jpg">下载</a>
11.2 Handler
@RequestMapping("/test1")
public void hello1(String name,HttpSession session,HttpServletResponse response){
System.out.println("name:"+name);
//获得要下载文件的绝对路径
String path = session.getServletContext().getRealPath("/upload_file");
//文件的完整路径
String real_path = path+File.separator+name;
//设置响应头 告知浏览器,要以附件的形式保存内容 filename=浏览器显示的下载文件名
response.setHeader("content-disposition","attachment;filename="+name);
//读取目标文件,写出给客户端
IOUtils.copy(new FileInputStream(real_path), response.getOutputStream());
//上一步,已经是响应了,所以此handler直接是void
}