springboot系列文章(四)——fastjson、定时任务、全局异常配置
来源:互联网 发布:手机音频软件 编辑:程序博客网 时间:2024/05/23 12:01
配置其它json框架
引入maven依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.37</version> </dependency>
新建WebMvcConfigurer类
package com.example.demo.config;import com.alibaba.fastjson.serializer.SerializerFeature;import com.alibaba.fastjson.support.config.FastJsonConfig;import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;import org.springframework.context.annotation.Configuration;import org.springframework.http.converter.HttpMessageConverter;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import java.util.List;/** * @author :小虎 * @date :2017/12/22 */@Configurationpublic class WebMvcConfigurer extends WebMvcConfigurerAdapter { @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); //自定义配置... FastJsonConfig config = new FastJsonConfig(); config.setSerializerFeatures(SerializerFeature.PrettyFormat); //config.set ... converter.setFastJsonConfig(config); converters.add(converter); }}
新建model类
package com.example.demo.Controller;import com.alibaba.fastjson.annotation.JSONField;/** * @author :小虎 * @date :2017/12/22 */public class Student { @JSONField(serialize = false) private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Student(int id, String name, int age) { this.id = id; this.name = name; this.age = age; }}
新建controller
package com.example.demo.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * @author :小虎 * @date :2017/12/22 */@RestControllerpublic class HomeController { @RequestMapping("/") public Student home(){ Student student = new Student(1,"zhangsan",12); return student; }}
访问浏览器
没有id表示使用fastjson成功
参考网址
https://github.com/alibaba/fastjson/wiki/%E5%9C%A8-Spring-%E4%B8%AD%E9%9B%86%E6%88%90-Fastjson
全局异常配置
新建GlobalDefaultExceptionHandler类
import org.springframework.ui.Model;import org.springframework.web.bind.WebDataBinder;import org.springframework.web.bind.annotation.*;import java.util.HashMap;import java.util.Map;/** * @author :小虎 * @date :2017/12/22 */@ControllerAdvicepublic class GlobalDefaultExceptionHandler { /** * 应用到所有@RequestMapping注解方法,在其执行之前初始化数据绑定器 * @param binder */ @InitBinder public void initBinder(WebDataBinder binder) {} /** * 把值绑定到Model中,使全局@RequestMapping可以获取到该值 * @param model */ @ModelAttribute public void addAttributes(Model model) { model.addAttribute("author", "Magical Sam"); } /** * 全局异常捕捉处理 * @param ex * @return */ @ResponseBody @ExceptionHandler(value = Exception.class) public Map errorHandler(Exception ex) { /* *返回json数据或者String数据: *那么需要在方法上加上注解:@ResponseBody *添加return即可。 */ /* *返回视图: *定义一个ModelAndView即可, *然后return; *定义视图文件(比如:error.html,error.ftl,error.jsp); * */ Map map = new HashMap(); map.put("code", 200); map.put("msg", ex.getMessage()); return map; } /** * 拦截捕捉自定义异常 MyException.class * @param ex * @return */ @ResponseBody @ExceptionHandler(value = MyException.class) public Map myErrorHandler(MyException ex) { Map map = new HashMap(); map.put("code", ex.getCode()); map.put("msg", ex.getMsg()); return map; }}
新建MyException
package com.example.demo.handler;/** * @author :小虎 * @date :2017/12/22 */public class MyException extends RuntimeException { public MyException(String code, String msg) { this.code = code; this.msg = msg; } private String code; private String msg; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; }}
新增连接
@RequestMapping("/zeroException") public int zeroException(){ return 100/0; } @RequestMapping("/myException") public int myException(){ throw new MyException("100","codeError"); }
访问结果
创建定时任务
新加注解 @EnableScheduling
@SpringBootApplication@EnableSchedulingpublic class Study4Application { public static void main(String[] args) { SpringApplication.run(Study4Application.class, args); }}
新建定时任务类
package com.example.demo.task;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;import java.text.SimpleDateFormat;import java.util.Date;/** * @author :小虎 * @date :2017/12/25 */@Componentpublic class ScheduledTasks { private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); @Scheduled(fixedRate = 5000) public void reportCurrentTime() { System.out.println("现在时间:" + dateFormat.format(new Date())); }}
输出
@Scheduled详解
在上面的入门例子中,使用了@Scheduled(fixedRate = 5000) 注解来定义每过5秒执行的任务,对于@Scheduled的使用可以总结如下几种方式:
@Scheduled(fixedRate = 5000) :上一次开始执行时间点之后5秒再执行
@Scheduled(fixedDelay = 5000) :上一次执行完毕时间点之后5秒再执行
@Scheduled(initialDelay=1000, fixedRate=5000) :第一次延迟1秒后执行,之后按fixedRate的规则每5秒执行一次
@Scheduled(cron=”*/5 * * * * *”) :通过cron表达式定义规则
源码地址
https://github.com/itmybaby/springboot/tree/master/study4
阅读全文
0 0
- springboot系列文章(四)——fastjson、定时任务、全局异常配置
- springboot系列文章(四)-springboot使用fastjson
- SpringBoot系列之四全局配置
- 【SpringBoot】——创建定时任务
- SpringBoot系列<五>全局异常处理
- springboot(十)定时任务
- springboot 2(定时任务)
- springmvc-springboot配置全局异常跳转页
- SpringBoot基于@ControllerAdvice配置全局异常处理
- SpringBoot-01SpringBoot配置FastJson
- SpringBoot系列—基础配置
- SpringBoot系列—SSL配置
- SpringBoot入门——局部与全局的异常处理
- springboot配置fastjson
- SpringBoot—动态修改定时任务cron参数
- Spring系列——定时任务
- SpringBoot学习——全局异常处理设置(返回JSON)
- SpringBoot入门-7(全局异常处理)
- 已经删除了datanode所有数据,但是Hbase创建表格时,仍Hbase Table already exists的处理方法~!
- 项目重构
- openCV学习笔记(七):傅里叶变换
- 每天一个算法--插入排序
- Linux下搭建Oracle11g RAC
- springboot系列文章(四)——fastjson、定时任务、全局异常配置
- windows 实现本地Eclipse远程调试MapReduce
- 用Python建设企业认证和权限控制平台
- 【spring+mybatis】MapperScannerConfigurer
- window.localStorage
- hibernate防止sql注入对参数赋值传参的例子
- 关于Java中用Double型运算时精度丢失的问题,真的很蛋疼!
- Java Arrays类的几种方法详解
- mac上配置java jdk环境