<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ## 一、開發REST接口 在本專欄之前的章節中已經給大家介紹了《Spring的常用注解》、《提高開發效率必備插件lombok》以及《RESTful接口協議狀態的表述》,本節內容就是將之前學到的內容以代碼的方式體現出來。 ### 1\. 第一步:定義資源(對象) ~~~ @Data @Builder public class Article { private Long id; private String author; private String title; private String content; private Date createTime; private List<Reader> reader; } ~~~ ~~~ @Data public class Reader { private String name; private Integer age; } ~~~ Data、Builder都是lombok提供給我們的注解,有利于我們簡化代碼。可以參考本專欄之前章節對lombok進行學習。 * @Builder為我們提供了通過對象屬性的鏈式賦值構建對象的方法,下文中代碼會有詳細介紹。 * @Data注解幫我們定義了一系列常用方法,如:getters、setters、hashcode、equals等 ### 2.第二步:HTTP方法與Controller(動作) 我們實現一個簡單的RESTful接口 * 增加一篇Article ,使用POST方法 * 刪除一篇Article,使用DELETE方法,參數是id * 更新一篇Article,使用PUT方法,以id為主鍵進行更新 * 獲取一篇Article,使用GET方法 下面代碼中并未真正的進行數據庫操作,本專欄后面會講解mybatis和JPA,屆時會做補充。 ~~~ @Slf4j @RestController @RequestMapping("/rest") public class ArticleController { //獲取一篇Article,使用GET方法,根據id查詢一篇文章 //@RequestMapping(value = "/articles/{id}",method = RequestMethod.GET) @GetMapping("/articles/{id}") public AjaxResponse getArticle(@PathVariable("id") Long id){ //使用lombok提供的builder構建對象 Article article = Article.builder() .id(id) .author("zimug") .content("spring boot 從青銅到王者") .createTime(new Date()) .title("t1").build(); log.info("article:" + article); return AjaxResponse.success(article); } //增加一篇Article ,使用POST方法(RequestBody方式接收參數) //@RequestMapping(value = "/articles",method = RequestMethod.POST) @PostMapping("/articles") public AjaxResponse saveArticle(@RequestBody Article article, @RequestHeader String aaa){ //因為使用了lombok的Slf4j注解,這里可以直接使用log變量打印日志 log.info("saveArticle:" + article); return AjaxResponse.success(); } //增加一篇Article ,使用POST方法(RequestParam方式接收參數) /*@PostMapping("/articles") public AjaxResponse saveArticle(@RequestParam String author, @RequestParam String title, @RequestParam String content, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam Date createTime){ log.info("saveArticle:" + createTime); return AjaxResponse.success(); }*/ //更新一篇Article,使用PUT方法,以id為主鍵進行更新 //@RequestMapping(value = "/articles",method = RequestMethod.PUT) @PutMapping("/articles") public AjaxResponse updateArticle(@RequestBody Article article){ if(article.getId() == null){ //article.id是必傳參數,因為通常根據id去修改數據 //TODO 拋出一個自定義的異常 } log.info("updateArticle:" + article); return AjaxResponse.success(); } //刪除一篇Article,使用DELETE方法,參數是id //@RequestMapping(value = "/articles/{id}",method = RequestMethod.DELETE) @DeleteMapping("/articles/{id}") public AjaxResponse deleteArticle(@PathVariable("id") Long id){ log.info("deleteArticle:" + id); return AjaxResponse.success(); } } ~~~ 因為使用了lombok的@Slf4j注解(類的定義處),就可以直接使用log變量打印日志。不需要寫下面的這行代碼。 ~~~ private static final Logger log = LoggerFactory.getLogger(HelloController.class); ~~~ ## 二、統一規范接口響應的數據格式 下面這個類是用于統一數據響應接口標準的。它的作用是:統一所有開發人員響應前端請求的返回結果格式,減少前后端開發人員溝通成本,是一種RESTful接口標準化的開發約定。下面代碼只對請求成功的情況進行封裝,在后續的異常處理相關的章節會做更加詳細的說明。 ~~~ @Data public class AjaxResponse { private boolean isok; //請求是否處理成功 private int code; //請求響應狀態碼(200、400、500) private String message; //請求結果描述信息 private Object data; //請求結果數據(通常用于查詢操作) private AjaxResponse(){} //請求成功的響應,不帶查詢數據(用于刪除、修改、新增接口) public static AjaxResponse success(){ AjaxResponse ajaxResponse = new AjaxResponse(); ajaxResponse.setIsok(true); ajaxResponse.setCode(200); ajaxResponse.setMessage("請求響應成功!"); return ajaxResponse; } //請求成功的響應,帶有查詢數據(用于數據查詢接口) public static AjaxResponse success(Object obj){ AjaxResponse ajaxResponse = new AjaxResponse(); ajaxResponse.setIsok(true); ajaxResponse.setCode(200); ajaxResponse.setMessage("請求響應成功!"); ajaxResponse.setData(obj); return ajaxResponse; } //請求成功的響應,帶有查詢數據(用于數據查詢接口) public static AjaxResponse success(Object obj,String message){ AjaxResponse ajaxResponse = new AjaxResponse(); ajaxResponse.setIsok(true); ajaxResponse.setCode(200); ajaxResponse.setMessage(message); ajaxResponse.setData(obj); return ajaxResponse; } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看