spring常用注解開發(fā)一個RESTful接口示例
一、開發(fā)REST接口
在本專欄之前的章節(jié)中已經(jīng)給大家介紹了
Spring常用注解及http數(shù)據(jù)轉(zhuǎn)換教程
Spring Boot提高開發(fā)效率必備工具lombok使用
Spring Boot開發(fā)RESTful接口與http協(xié)議狀態(tài)表述
本節(jié)內(nèi)容就是將之前學到的內(nèi)容以代碼的方式體現(xiàn)出來。
第一步:定義資源(對象)
@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提供給我們的注解,有利于我們簡化代碼??梢詤⒖急緦谥罢鹿?jié)對lombok進行學習。
@Builder為我們提供了通過對象屬性的鏈式賦值構建對象的方法,下文中代碼會有詳細介紹。
@Data注解幫我們定義了一系列常用方法,如:getters、setters、hashcode、equals等
第二步:HTTP方法與Controller(動作)
我們實現(xiàn)一個簡單的RESTful接口
- 增加一篇Article ,使用POST方法
- 刪除一篇Article,使用DELETE方法,參數(shù)是id
- 更新一篇Article,使用PUT方法,以id為主鍵進行更新
- 獲取一篇Article,使用GET方法
下面代碼中并未真正的進行數(shù)據(jù)庫操作,本專欄后面會講解mybatis和JPA,屆時會做補充。
@Slf4j
@RestController
@RequestMapping("/rest")
public class ArticleController {
//獲取一篇Article,使用GET方法,根據(jù)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方式接收參數(shù))
//@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方式接收參數(shù))
/*@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是必傳參數(shù),因為通常根據(jù)id去修改數(shù)據(jù)
//TODO 拋出一個自定義的異常
}
log.info("updateArticle:" + article);
return AjaxResponse.success();
}
//刪除一篇Article,使用DELETE方法,參數(shù)是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);
二、統(tǒng)一規(guī)范接口響應的數(shù)據(jù)格式
下面這個類是用于統(tǒng)一數(shù)據(jù)響應接口標準的。它的作用是:統(tǒng)一所有開發(fā)人員響應前端請求的返回結果格式,減少前后端開發(fā)人員溝通成本,是一種RESTful接口標準化的開發(fā)約定。下面代碼只對請求成功的情況進行封裝,在后續(xù)的異常處理相關的章節(jié)會做更加詳細的說明。
@Data
public class AjaxResponse {
private boolean isok; //請求是否處理成功
private int code; //請求響應狀態(tài)碼(200、400、500)
private String message; //請求結果描述信息
private Object data; //請求結果數(shù)據(jù)(通常用于查詢操作)
private AjaxResponse(){}
//請求成功的響應,不帶查詢數(shù)據(jù)(用于刪除、修改、新增接口)
public static AjaxResponse success(){
AjaxResponse ajaxResponse = new AjaxResponse();
ajaxResponse.setIsok(true);
ajaxResponse.setCode(200);
ajaxResponse.setMessage("請求響應成功!");
return ajaxResponse;
}
//請求成功的響應,帶有查詢數(shù)據(jù)(用于數(shù)據(jù)查詢接口)
public static AjaxResponse success(Object obj){
AjaxResponse ajaxResponse = new AjaxResponse();
ajaxResponse.setIsok(true);
ajaxResponse.setCode(200);
ajaxResponse.setMessage("請求響應成功!");
ajaxResponse.setData(obj);
return ajaxResponse;
}
//請求成功的響應,帶有查詢數(shù)據(jù)(用于數(shù)據(jù)查詢接口)
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;
}
}以上就是springboot常用注解開發(fā)一個RESTful接口示例的詳細內(nèi)容,更多關于springboot注解開發(fā)RESTful接口的資料請關注腳本之家其它相關文章!
相關文章
Mybatis基于TypeHandler實現(xiàn)敏感數(shù)據(jù)加密
業(yè)務場景中經(jīng)常會遇到諸如用戶手機號,身份證號,銀行卡號,郵箱,地址,密碼等等信息,屬于敏感信息,本文就來介紹一下Mybatis基于TypeHandler實現(xiàn)敏感數(shù)據(jù)加密,感興趣的可以了解一下2023-10-10
Java 調(diào)用天氣Webservice詳解及實例代碼
這篇文章主要介紹了Java 調(diào)用天氣Webservice詳解及實例代碼的相關資料,這里附實例代碼,使用java 調(diào)用webservice 的小應用,需要的朋友可以參考下2016-11-11
SpringBoot Import及自定義裝配實現(xiàn)方法解析
這篇文章主要介紹了SpringBoot Import及自定義裝配實現(xiàn)方法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-08-08
Spring Cloud Gateway Hystrix fallback獲取異常信息的處理
這篇文章主要介紹了Spring Cloud Gateway Hystrix fallback獲取異常信息的處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07

