使用swagger建立功能強大的API
阿新 • • 發佈:2019-01-22
package com.example.demo.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.domain.Student;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
@RestController
@RequestMapping("/students")
public class SwaggerController {
private static Map<String, Student> students=new HashMap<String, Student>();
@ApiOperation(value="加入一個學生",notes="根據提交的資訊加入一個學生")
@ApiImplicitParam(name="student",value="要增加的學生資訊",required=true,dataType="Student")
@RequestMapping(value="addStudent",method=RequestMethod.POST)
public String addStudent(@RequestBody Student student){
students.put(student.getStId(), student);
return "add student to students successfully";
}
@RequestMapping(value="/removeStudent/{stId}",method=RequestMethod.DELETE)
@ApiImplicitParam(name="id",required=true,dataType="String")
@ApiOperation(value="刪除一個學生",notes="根據學生id刪除學生")
public String removeStudent(@PathVariable("stId") String stId){
students.remove(stId);
return "remove student from students successfully";
}
@RequestMapping(value={""},method=RequestMethod.GET)
@ApiOperation(value="獲取學生列表",notes="獲取所有學生列表")
public String getAllStudent(){
return JSONObject.toJSONString(students);
}
}
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.domain.Student;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
@RestController
@RequestMapping("/students")
public class SwaggerController {
private static Map<String, Student> students=new HashMap<String, Student>();
@ApiOperation(value="加入一個學生",notes="根據提交的資訊加入一個學生")
@ApiImplicitParam(name="student",value="要增加的學生資訊",required=true,dataType="Student")
@RequestMapping(value="addStudent",method=RequestMethod.POST)
public String addStudent(@RequestBody Student student){
students.put(student.getStId(), student);
return "add student to students successfully";
}
@RequestMapping(value="/removeStudent/{stId}",method=RequestMethod.DELETE)
@ApiImplicitParam(name="id",required=true,dataType="String")
@ApiOperation(value="刪除一個學生",notes="根據學生id刪除學生")
public String removeStudent(@PathVariable("stId") String stId){
students.remove(stId);
return "remove student from students successfully";
}
@RequestMapping(value={""},method=RequestMethod.GET)
@ApiOperation(value="獲取學生列表",notes="獲取所有學生列表")
public String getAllStudent(){
return JSONObject.toJSONString(students);
}
}