本章介绍了SpringBoot控制器类型分类,对于路由的组成、作用进行了详细的描述,同时对于SpringBoot Controller如何实现参数传递进行了详细的说明和实现,为了实现轻量级的数据结果返回,本章引入了SpringBoot对于JSON的集成实现,对于定时任务的处理主要讲解了三种定时器进行实战操作演练。
编写HelloController.java
@RestController @RequestMapping("/user") public class HelloController { @GetMapping(value = "/get") public String testGet(){ return "测试GetMapping...."; } @PostMapping(value = "/post") public String testPost(){ return "测试PostMapping....."; } @PutMapping(value = "/put") public String testPut(){ return "测试PutMapping....."; } @DeleteMapping(value = "/del") public String testDelete(){ return "测试DeleteMapping....."; } @PatchMapping (value = "/patch") public String testPatch(){ return "测试PatchMapping....."; } @RequestMapping (value = "/req",method = RequestMethod.GET) public String testRequest(){ return "测试RequestMapping....."; } }
采用Postman进行API测试
编写ParamController.java
@RestController @RequestMapping("/user") public class ParamController { //账户验证 @RequestMapping(value="/login",method=RequestMethod.POST) public String login(@RequestParam String loginId,@RequestParam String pwd){ return "接收的账号:"+loginId+",密码:"+pwd; } //根据姓氏查询用户列表信息 @RequestMapping(value="/query") public String queryUsers(@RequestParam(value="uname",required = false,defaultValue = "张") String userName){ return "参数:"+userName; } //根据id查询用户详情 @RequestMapping(value="/detail/{id}",method= RequestMethod.GET) public String queryDetail(@PathVariable String id){ return "接收到的参数是:"+id; } }
采用Postman进行测试
编写ResponseController.java
@Controller public class ResponseController { @RequestMapping(value="/query",method = RequestMethod.GET) public ModelAndView queryUser(ModelAndView mv){ List
在templates目录下创建userList.html页面
执行程序,观察运行情况
对象
数组
[ {"name":"张三","age":20}, {"name":"李四","age":22}, {"name":"王五","age":19} ]
pom.xml引入library依赖
编程JsonController.java
@RestController @RequestMapping("/user") public class JsonController { @ResponseBody @RequestMapping(value="/queryAll",method = RequestMethod.GET) public String getData(){ //封装查询结果 Map
运行程序,观察执行效果
执行效果是:
{"data":[ {"userId":1001,"userName":"admin"}, {"userId":1002,"userName":"jains"}, {"userId":1003,"userName":"tony"}, ], "status":200 }
需求:每隔1秒显示当前服务器时间
TimerTask方式
//方式1:采用Timer方式 public static void testTimer(){ TimerTask task = new TimerTask() { @Override public void run() { System.out.println("当前时间是:"+TimeUtils.getNowTime()); } }; Timer timer = new Timer(); timer.schedule(task, 0, 1000); // 0标识 1000指延迟的时间1000毫秒 }
Quartz定时器方式
public class MyJob implements Job { @Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { // TODO Auto-generated method stub //需要定时执行的方法 System.out.println("当前的时间是:"+ TimeUtils.getNowTime()); } }
//方式2:采用Quartz public static void testQuartz()throws Exception{ // TODO Auto-generated method stub // 创建工作 JobDetail jobDetail = JobBuilder.newJob(MyJob.class) .withDescription("工作的描述") .withIdentity("工作的名称", "工作的组") .build(); // 创建触发器 Trigger trigger = TriggerBuilder.newTrigger() .withDescription("触发器的描述") .withIdentity("触发器的名称", "触发器的组") .withSchedule(CronScheduleBuilder.cronSchedule("0/1 * * * * ?")) .startAt(new Date())// 不设置,默认为当前时间 .build(); // 创建调度器,粘合工作和触发器 SchedulerFactory schedulerFactory = new StdSchedulerFactory(); Scheduler scheduler = schedulerFactory.getScheduler(); scheduler.scheduleJob(jobDetail, trigger); // 启动调度器 scheduler.start(); }
@Scheduled注解方式
@Component public class Scheduler{ //每隔2秒执行一次 @Scheduled(fixedRate = 2000) public void testTasks() throws Exception{ System.out.println("定时任务执行时间:" + TimeUtils.getNowTime()); } //每天2:01执行 @Scheduled(cron = "0 01 02 ? * *") public void testTasks2() { System.out.println("定时任务执行时间:" + TimeUtils.getNowTime()); } }
@SpringBootApplication @EnableScheduling public class SpringbootWebApplication { public static void main(String[] args) { SpringApplication.run(SpringbootWebApplication.class, args); } }
、