//响应字符串格式数据 @RequestMapping("/hello") public Result hello(){ System.out.println("Hello World"); // return new Result(1,"success","Hello World"); return Result.success("Hello World"); } //响应对象类数据 //返回类型为json格式数据 @RequestMapping("/getAddr") public Result getAddr(){ Address addr = new Address(); addr.setProvince("河北"); addr.setCity("石家庄"); return Result.success(addr); } //集合响应json格式 @RequestMapping("/getData") public Result getData(){ List<Data> dataArrayList = this.getDataFirst(); return Result.success(dataArrayList); } //数组格式封装 @RequestMapping("/arrayParam") public String arrayParam(String[] hobby){ System.out.println(Arrays.toString(hobby)); return "OK"; } //类封装 //添加注解@RequestParam @RequestMapping("/listParam") public String listParam(@RequestParam List<String> hobby){ System.out.println(hobby); return "OK"; } //日期格式参数添加注解 //@DataTimeFormat @RequestMapping("/dataParam") public String dataParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime updateTime){ System.out.println(updateTime); return "OK"; } //json格式数据参数添加注解 //@RequestBody @RequestMapping("/jsonParam") public String jsonParam(@RequestBody User user){ System.out.println(user); return "OK"; } //路径参数添加注解 //@PathVariable @RequestMapping("/path/{id}") public String pathParam(@PathVariable Integer id){ System.out.println(id); return "OK"; } @RequestMapping("/path/{id}/{name}") public String pathParam(@PathVariable Integer id,@PathVariable String name){ System.out.println(id+":"+name); return "OK"; }
值得一提的是当响应的类型为集合,且想要将这个集合在响应时自动转化为json格式的数据的话
在类名前加上注解@RestController即可
实现自动转化json格式的注解为@ResponseBody,这个注解@RestController中已经存在了,所以只需要在类名前加上注解@RestController
方法的返回的数据类型需要是List<>
标签:return,RequestMapping,System,String,封装,out,方法,public,springboot From: https://www.cnblogs.com/qq2803572183/p/17818264.html