1,pom.xml
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.7.0.RELEASE</version>
</dependency>
2,yml配置里添加配置:
swagger:
base-package: com.itmayidu.api.service
3,启动类添加注解: @EnableSwagger2//表示生成swagger2文档
package com.itmayidu.api.service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
*@author tom
*Date 2020/4/21 0021 23:28
*
*/
@EnableSwagger2//表示生成swagger2文档
@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class AppMember {
public static void main(String[] args) {
SpringApplication.run(AppMember.class,args);
}
}
4,类:
package com.itmayidu.api.service.impl;
import com.itmayidu.api.entity.UserEntity;
import com.itmayidu.api.service.IMemberService;
import com.taotao.BaseApiService;
import com.taotao.ResponseBase;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
*@author tom
*Date 2020/4/21 0021 20:23
*
*/
@Api("会员服务")
@RestController
public class MemberServiceImpl extends BaseApiService implements IMemberService {
@Value("${server.port}")
private String serverport;
@ApiOperation("添加用户的接口")
@ApiImplicitParams({
@ApiImplicitParam(name = "username", value = "用户名", defaultValue = "李四")}
)
@GetMapping("index")
public String index(String name){
return name;
}
@Override
@RequestMapping("/getMember")
public UserEntity getMember(@RequestParam(value = "name") String name) {
UserEntity userEntity = new UserEntity();
userEntity.setName(name);
userEntity.setAge(21);
return userEntity;
}
@Override
@RequestMapping("/getUserInfo")
public ResponseBase getUserInfo() throws InterruptedException {
//请求延迟
System.out.println("订单服务调用会员服务");
Thread.sleep(1500);
return setResultSuccess("订单服务调用会员服务接口成功......"+ "端口号:"+serverport);
}
}