Http通信協議接口接入swagger步驟實現自動生成接口文檔


步驟如下:
1、將framework-server版本號由1.5.5升級至1.5.6:
<framework-server.version>1.5.6</framework-server.version>
2、在application.properties添加以下配置:
#是否開啟swagger生產環境需要設置為false
swagger.enable = true
#swagger api-docs版本號
swagger.api.info.version = 1.0.0
#swagger api-docs標題
swagger.api.info.title = framework-example api文檔
#swagger掃描annotation包路徑,該路徑要包含接口和實體類
swagger.resource.package = com.meitong.framework.server.example
3、將接口和實體類以及接口入參添加swagger相關注解,示例如下:
接口:
@Path("/restResource")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Api(value = "Rest resource operations")
public interface RestResource {
    @GET
    @Path("/invokeGet/{id}/{name}")
    @ApiOperation(value = "invokeGet示例", notes = "invokeGet說明", response = List.class, httpMethod = "GET")
    List<String> invokeGet(@PathParam("id") Integer id, @PathParam("name") String name);
    /**
     * invokePost method
     *
     * @param ids
     * @return List<String>
     */
    @POST
    @Path("/invokePost")
    @ApiOperation(value = "invokePost示例", notes = "invokePost說明", response = List.class, httpMethod = "POST")
    List<String> invokePost(List<Integer> ids);
    @PUT
    @Path("/invokePut")
    @ApiOperation(value = "invokePut示例", notes = "invokePut說明", response = Void.class, httpMethod = "PUT")
    void invokePut(List<String> names);
    @DELETE
    @Path("/invokeDelete/{id}")
    @ApiOperation(value = "invokeDelete示例", notes = "invokeDelete說明", response = Void.class, httpMethod = "DELETE")
    void invokeDelete(@PathParam("id") Integer id);

    @GET
    @Path("/getAllUser")
    @ApiOperation(value = "獲取所有的用戶",notes = "使用說明",response = User.class,responseContainer = "List",httpMethod = "GET")
    List<User> getAllUser();
    @POST
    @Path("/insert")
    @ApiOperation(value = "新增用戶",notes = "使用說明",response = Void.class,httpMethod = "POST")
    void insert(@ApiParam(value = "用戶信息",name = "user",required = true) User user);
}
實體類:
@ApiModel(description = "用戶信息")
public class User {
    @ApiModelProperty(value = "用戶名",required = true)
    private String userName;
    @ApiModelProperty(value = "用戶ID",required = true)
    private String userId;
    //get and set
}
4.接口文檔訪問:
 http://ip:port/swagger.json
5.完成系統接入以后,將步驟4生成的接口文檔地址匯總發到我這邊。我需要將這些接口集成到swagger-ui中
6.swagger-ui訪問地址
http://101.37.117.177:8080/static/#/
 

如果集成到swagger-ui中會存在跨域請求問題,所以需要添加一個全局的過濾器:

com.meitong.framework.server.filters.CORSFilter
 
        
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM