相关文章推荐
潇洒的电梯  ·  OpenAPI 规范 v2.0 - ...·  4 月前    · 
叛逆的眼镜  ·  CRMEB JAVA ...·  3 月前    · 
会开车的夕阳  ·  NuGet Gallery ...·  3 月前    · 
冷静的葫芦  ·  iOS ...·  3 年前    · 
當您輸入電子信箱、訂閱本公司之「電子報」時,我們會向您蒐集、處理、利用的個資為您的「電子信箱」以及「提交日期」,當您輸入電子信箱並送出時,即表示您同意我們使用您的個資,為保障您的權益,關於更多相關政策更新資訊,請務必閱讀我們的「 隱私權政策 」、「 使用條款 」及「 免責聲明 」。如您不同意本網站之「隱私權政策」、「使用條款」及「 免責聲明」,您可以隨時「 取消訂閱 」,謝謝您。
@ApiModel(description = "學生資料")
public class Student {

@ApiModelProperty(value = "學生姓名",required = true)
private String name;

@ApiModelProperty(value = "學生編號",required = true)

private Integer no;

@ApiModelProperty(value = "學生年齡",required = true)

private Integer age;

public String getName() {

return name;
}


public void setName(String name) {
this.name = name;
}


public Integer getNo() {
return no;
}


public void setNo(Integer no) {
this.no = no;
}


public Integer getAge() {
return age;
}


public void setAge(Integer age) {
this.age = age;
}

用在API上,代表此API的名稱或作用

其他用法可參考以下網址

https://hk.saowen.com/a/84e86300f9c93433dfd72b14a1fa142ce1a5356185f5c28ed40dcfc440d6a2da


@RestController
public class BaseController {


@Autowired
private StudentService studentService;

@ApiResponses(value = {@ApiResponse(code=200,message = "查詢成功")})

@ApiOperation("查詢學生")
@GetMapping(value = "/getStudent/{no}",produces = {MediaType.APPLICATION_JSON_VALUE})
public Student getStudent(@PathVariable int no) {
return studentService.getStudentByNo(no);
}



@ApiResponses(value = {@ApiResponse(code=200,message = "新增成功")})
@ApiOperation("新增學生")
@PostMapping(value = "/addStudent",consumes = MediaType.APPLICATION_JSON_VALUE,produces = MediaType.APPLICATION_JSON_VALUE)
public Student addStudent(@RequestBody Student student) {
return studentService.addStudent(student);
}


@ApiResponses(value = {@ApiResponse(code=200,message = "查詢成功")})
@ApiOperation("取得所有學生")
@GetMapping(value = "/getAllStudent",consumes = MediaType.APPLICATION_JSON_VALUE,produces = MediaType.APPLICATION_JSON_VALUE)
public List<Student> getAllStudents(){
return studentService.getAllStudent();
}


@ApiResponses(value = {@ApiResponse(code=200,message = "刪除成功")})
@ApiOperation("刪除學生")
@GetMapping(value = "/delStudent/{no}",produces = {MediaType.APPLICATION_JSON_VALUE})
public void delStudent(@PathVariable int no){
studentService.delStudent(no);
}


}
@Configuration
@EnableSwagger2
public class SwaggerConfig {


@Bean
public Docket api(){

return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
.paths(PathSelectors.any())
.build();
}


}

5.最後啟動服務,進入http://localhost:8080/swagger-ui.html, 就能進入swegger的UI介面

6.可以點選各API,就可以直接看到各API 需要Request和回應Response的資料

7.點選右上角的Try It Out按鈕,會直接產出Example Value,就可以直接輸入需要參數測試API是否正常。

8. 按下Excute,結果就會顯示在下方