SpringBoot2.X实战应用

springBoot2.x依赖环境和版本特性说明

  1、依赖版本jsk8以上,springboot2.x用jdk8,因为底层是spring framework5

  2、安装maven最新版本,maven3.2以上版本,下载地址:https://maven.apache.org/download.cgi

  3、Eclipse或者IDE

  4、新特性

 

 

配置讲解

  1、@RestController and @RequestMapping 是springMVC的注解,不是springBoot特有的

  2、@RestController = @Controller+ResponseBody

  3、@SpringBootApplication = @Configuration+@EnableAutoConfiguration+@ComponentScan

开发接口必备工具PostMan接口调试工具介绍和使用

  1、接口调试工具安装和基本使用

  2、下载地址:http://www.getpostman.com/

springBoot基础HTTP接口GET请求实战

  1、GET请求

    单一参数@RequestMappiing(path = "/{id}",method = RequestMethod.GET)
    public String getUser(@PathVariable String id){}

    @RequestMapping(path = "/{depid}/{userid}",method = RequestMethod.GET)
    可以指定多个提交方法
    getUser(@PathVariable("depid") String departmentID,@PathVariable("userid") String userid)

    一个定两个
    @GetMapping = @RequestMapping(method = RequestMethod.GET)
    @PostMapping = @RequestMapping(method = RequestMethod.POST)
    @PutMapping = @RequestMapping(method = RequestMethod.PUT)
    @DeleteMapping = @RequestMapping(method = RequestMethod.DELETE)

    @RequestParam(value="name",required="true")
    可以设置默认值,比如分页

    @RequestMapping+RequestBody请求体映射实体类
    注意需要指定HTTP头为 content-type为application/json

    @RequestHeader 请求头,比如鉴权
    @RequestHeader("access_token") String accessToken

    HttpServletRequest request自动注入获取参数

    

 

 

 

 常用json框架介绍和Jackson返回结果处理

  1、常用框架 阿里 fastjson 谷歌gson等
    JavaBean序列化为Json,性能:JackSon>FastJson>Gson>Json-lib

  2、jackson处理相关注解
    指定字段不返回:@JsonIgnore
    指定日期格式:@JsonFormat(pattern="yyyy-MM-dd hh:mm:ss",locale="zh",timezone="GMT+8")
    空字段不返回:@JsonInclude(Include.NON_NULL)
    指定别名:@JsonProperty

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章