|
|
|
@ -9,26 +9,16 @@ SOP采用微服务架构实现,因此文档应该由各个微服务各自实 |
|
|
|
|
- maven添加swagger |
|
|
|
|
|
|
|
|
|
```xml |
|
|
|
|
<!-- swagger2 --> |
|
|
|
|
<dependency> |
|
|
|
|
<groupId>io.springfox</groupId> |
|
|
|
|
<artifactId>springfox-swagger2</artifactId> |
|
|
|
|
<version>2.9.2</version> |
|
|
|
|
<exclusions> |
|
|
|
|
<exclusion> |
|
|
|
|
<groupId>io.swagger</groupId> |
|
|
|
|
<artifactId>swagger-models</artifactId> |
|
|
|
|
</exclusion> |
|
|
|
|
</exclusions> |
|
|
|
|
</dependency> |
|
|
|
|
<dependency> |
|
|
|
|
<groupId>io.swagger</groupId> |
|
|
|
|
<artifactId>swagger-models</artifactId> |
|
|
|
|
<version>1.5.21</version> |
|
|
|
|
</dependency> |
|
|
|
|
<dependency> |
|
|
|
|
<groupId>io.springfox</groupId> |
|
|
|
|
<artifactId>springfox-swagger-ui</artifactId> |
|
|
|
|
<version>2.9.2</version> |
|
|
|
|
<groupId>com.github.xiaoymin</groupId> |
|
|
|
|
<artifactId>swagger-bootstrap-ui</artifactId> |
|
|
|
|
<version>1.9.5</version> |
|
|
|
|
</dependency> |
|
|
|
|
|
|
|
|
|
``` |
|
|
|
@ -38,14 +28,16 @@ SOP采用微服务架构实现,因此文档应该由各个微服务各自实 |
|
|
|
|
```java |
|
|
|
|
@Configuration |
|
|
|
|
public class OpenServiceConfig extends AlipayServiceConfiguration { |
|
|
|
|
// 开启文档 |
|
|
|
|
/** |
|
|
|
|
* 开启文档,本地微服务文档地址:http://localhost:2222/doc.html |
|
|
|
|
* http://ip:port/v2/api-docs |
|
|
|
|
*/ |
|
|
|
|
@Configuration |
|
|
|
|
@EnableSwagger2 |
|
|
|
|
public static class Swagger2 extends SwaggerSupport { |
|
|
|
|
@Override |
|
|
|
|
protected String getDocTitle() { |
|
|
|
|
// 不能重复。比如订单服务返回:`订单API`;库存服务返回:`库存API` |
|
|
|
|
return "图书API"; |
|
|
|
|
return "故事API"; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -60,39 +52,48 @@ public class OpenServiceConfig extends AlipayServiceConfiguration { |
|
|
|
|
```java |
|
|
|
|
// 请求参数 |
|
|
|
|
@Data |
|
|
|
|
public class BookParam { |
|
|
|
|
@ApiModelProperty(value = "图书id", example = "1") |
|
|
|
|
public class StoryParam { |
|
|
|
|
@ApiModelProperty(value = "故事ID", example = "111") |
|
|
|
|
private int id; |
|
|
|
|
|
|
|
|
|
@ApiModelProperty(value = "图书ISBN", example = "xxxx") |
|
|
|
|
private String isbn; |
|
|
|
|
@ApiModelProperty(value = "故事名称", required = true, example = "白雪公主") |
|
|
|
|
private String name; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 返回结果 |
|
|
|
|
@Data |
|
|
|
|
public class BookVO { |
|
|
|
|
@ApiModelProperty(value = "图书id", example = "1") |
|
|
|
|
private int id; |
|
|
|
|
public class StoryResult { |
|
|
|
|
@ApiModelProperty(value = "故事ID", example = "1") |
|
|
|
|
private Long id; |
|
|
|
|
|
|
|
|
|
@ApiModelProperty(value = "图书名称", example = "白雪公主") |
|
|
|
|
@ApiModelProperty(value = "故事名称", example = "海底小纵队") |
|
|
|
|
private String name; |
|
|
|
|
|
|
|
|
|
@ApiModelProperty(value = "isbn", example = "xxxxxx") |
|
|
|
|
private String isbn; |
|
|
|
|
@ApiModelProperty(value = "创建时间", example = "2019-04-14 19:02:12") |
|
|
|
|
private Date gmt_create; |
|
|
|
|
} |
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
- 在接口方法上编写`@ApiOperation`注解 |
|
|
|
|
|
|
|
|
|
```java |
|
|
|
|
@ApiOperation(value="查询书本信息", notes = "可以根据ISBN查询书本信息") |
|
|
|
|
@ApiMapping(value = "book.search") |
|
|
|
|
public BookVO searchBook(BookParam param) { |
|
|
|
|
BookVO bookVO = new BookVO(); |
|
|
|
|
bookVO.setId(1); |
|
|
|
|
bookVO.setName("白雪公主,ISBN:" + param.getIsbn()); |
|
|
|
|
bookVO.setIsbn("ABCSSSSDDD"); |
|
|
|
|
return bookVO; |
|
|
|
|
/** |
|
|
|
|
* 参数绑定 |
|
|
|
|
* |
|
|
|
|
* @param story 对应biz_content中的内容,并自动JSR-303校验 |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
@ApiOperation(value = "获取故事信息", notes = "说明接口的详细信息,介绍,用途,注意事项等。") |
|
|
|
|
@ApiMapping(value = "alipay.story.find") |
|
|
|
|
public StoryResult getStory2(StoryParam story) { |
|
|
|
|
log.info("获取故事信息参数, story: {}", story); |
|
|
|
|
// 获取其它参数 |
|
|
|
|
OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext(); |
|
|
|
|
String app_id = openContext.getAppId(); |
|
|
|
|
StoryResult result = new StoryResult(); |
|
|
|
|
result.setName("白雪公主, app_id:" + app_id); |
|
|
|
|
result.setGmt_create(new Date()); |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
``` |
|
|
|
|
|
|
|
|
@ -100,10 +101,9 @@ public BookVO searchBook(BookParam param) { |
|
|
|
|
|
|
|
|
|
## 查看文档 |
|
|
|
|
|
|
|
|
|
- 启动website-server(运行WebsiteServerApplication.java) |
|
|
|
|
- 找到sop-website/website-front/pages/doc/doc.html,IDEA下右键--Debug |
|
|
|
|
|
|
|
|
|
如果没有IDEA可以下个webstorm,同样有本地静态服务器功能。 |
|
|
|
|
- 确保注册中心、网关、微服务正常启动 |
|
|
|
|
- 运行WebsiteServerApplication.java |
|
|
|
|
- 访问http://localhost:8083 |
|
|
|
|
|
|
|
|
|
效果图如下 |
|
|
|
|
|
|
|
|
|