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

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