springboot整合swagger
新建配置类SwaggerConfig
(不用配置.yaml文件啥的)
package com.twoGroup.boot.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.twoGroup.boot.controller")) //扫描包所在位置
.paths(PathSelectors.any())
.build();
}
public ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("二手交易平台") //整个项目的名称
.description("阿巴阿巴交易平台") //项目描述
.termsOfServiceUrl("https://blog.csdn.net/YSecret_Y?spm=1000.2115.3001.5113")
.version("1.0")
.build();
}
}
加入swagger注解
package com.twoGroup.boot.controller;
import com.twoGroup.boot.pojo.Address;
import com.twoGroup.boot.service.imp.AddressServiceImpl;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.List;
@Controller
@RequestMapping("/Address")
public class AddressController {
@Resource
private AddressServiceImpl addressService;
//添加新的收件人
@ApiOperation(value = "添加新的收件人(<=5)", notes = "传入userId(用户id)")
@PostMapping("/addAddressee")
@ApiImplicitParams(
{
@ApiImplicitParam(name = "userId",value = "用户id"),
@ApiImplicitParam(name="addressee",value = "收件人名字(长度1-6 跟nickname,可以不是真实姓名)"),
@ApiImplicitParam(name = "addresseeTelephone",value = "收件人手机号(11为且第一个值为1)"),
@ApiImplicitParam(name = "address",value = "收件人地址")
}
)
@ResponseBody
public List<Address> addAddressee(Integer userId, String addressee, String addresseeTelephone, String address){
List<Address> addresses = addressService.queryAllAddressee(userId);
if(addressService.queryAddress(userId)<=5){
addressService.addAddressee(userId, addressee, addresseeTelephone, address);
return addressService.queryAllAddressee(userId);
}
return addresses;
}
完成图