目录结构及含义
Swaggerconf类
用于设置路径等
配置路径,基本不做修改,复制粘贴即可
package com.example.demo.conf;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
public class Swaggerconf implements WebMvcConfigurer {
private final String baseUrl;
public Swaggerconf(String baseUrl) {
this.baseUrl = baseUrl;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
String baseUrl = StringUtils.trimTrailingCharacter(this.baseUrl, '/');
registry.
addResourceHandler(baseUrl + "/swagger-ui/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
.resourceChain(false);
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController(baseUrl + "/swagger-ui/")
.setViewName("forward:" + baseUrl + "/swagger-ui/index.html");
}
}
SwaggerSecConf核心配置类
注意事项:
- 自定义mode,注意GetScoreByVin.class为自定义mode的类,只用写get和set方法
.additionalModels(
typeResolver.resolve(GetScoreByVin.class) - 配置包扫描路径,注意!!包之间用“.”分隔,切不可用斜杠,如下
.apis(RequestHandlerSelectors.basePackage(
“com.example.demo.control”
package com.example.demo.conf;
import com.example.demo.conf.model.getallbyid;
import com.fasterxml.classmate.TypeResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.*;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @author Xpon
* @date 2022/11/09 17:03
*/
@Configuration
@EnableSwagger2
public class SwaggerSecConf implements WebMvcConfigurer {
@Bean
public Docket showApi(TypeResolver typeResolver) {
return new Docket(DocumentationType.SWAGGER_2)
.useDefaultResponseMessages(false)
//自定义mode,注意!下面getallbyid.class)为自定义mode的类,只用写get和set方法
.additionalModels(
typeResolver.resolve(getallbyid.class)
)
.select()
//配置包扫描路径,注意!!包之间用“.”分隔,切不可用斜杠
.apis(RequestHandlerSelectors.basePackage(
"com.example.demo.control"
))
.paths(PathSelectors.any())
.build();
}
@Bean
SecurityConfiguration security() {
return SecurityConfigurationBuilder.builder()
.clientId("test-app-client-id")
.clientSecret("test-app-client-secret")
.realm("test-app-realm")
.appName("test-app")
.scopeSeparator(",")
.additionalQueryStringParams(null)
.useBasicAuthenticationWithAccessCodeGrant(false)
.enableCsrfSupport(false)
.build();
}
@Bean
UiConfiguration uiConfig() {
return UiConfigurationBuilder.builder()
.deepLinking(true)
.displayOperationId(false)
.defaultModelsExpandDepth(1)
.defaultModelExpandDepth(1)
.defaultModelRendering(ModelRendering.EXAMPLE)
.displayRequestDuration(false)
.docExpansion(DocExpansion.NONE)
.filter(false)
.maxDisplayedTags(null)
.operationsSorter(OperationsSorter.ALPHA)
.showExtensions(false)
.showCommonExtensions(false)
.tagsSorter(TagsSorter.ALPHA)
.supportedSubmitMethods(UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS)
.validatorUrl(null)
.build();
}
}
swagger中的自定义model
与配置类中的 .additionalModels(
typeResolver.resolve(GetScoreByVin.class)
)
相呼应
package com.yxdk.cbit.insurance.api.conf.model;
import io.swagger.annotations.ApiModelProperty;
/**
* @author Xpon
* @date 2022/11/10 16:34
*/
public class GetScoreByVin {
@ApiModelProperty(required = true)
String orgName;
@ApiModelProperty(required = true)
String orgCode;
@ApiModelProperty(required = true, value = "<span style='color: green'>AES加密信息JSON,包含必填字段vin与非必填字段plateNumber</span>")
String data;
public String getOrgName() {
return orgName;
}
public void setOrgName(String orgName) {
this.orgName = orgName;
}
public String getOrgCode() {
return orgCode;
}
public void setOrgCode(String orgCode) {
this.orgCode = orgCode;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
接口实现
- 首先,接口要在报包下,与配置文件的 .apis(RequestHandlerSelectors.basePackage(
“com.example.demo.control”
))
相呼应
package com.example.demo.control;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.service.SelectService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping(value = "/model")
public class SelectController {
private static Logger logger = LoggerFactory.getLogger(SelectController.class);
private final SelectService scoreService;
public SelectController(SelectService scoreService) {
this.scoreService = scoreService;
}
@RequestMapping(value = "/getallbyid", method = RequestMethod.POST)
//设置为这个接口被扫描
@ApiImplicitParams({
@ApiImplicitParam(
name = "body",
//设置参数类型
dataType = "getallbyid")
})
//注意!!,接口获得参数必须要写@RequestBody在参数前
public JSONObject getallbyid(@RequestBody String body){
JSONObject requestParams = JSONObject.parseObject(body);
String id = requestParams.getString("id");
Map<String, Object> map = new HashMap<>(3);
map.put("id", id);
liberty.school s=scoreService.getallbyid(map);
JSONObject responsjson=JSONObject.parseObject(s.toString());
return responsjson;
}
}
model类型及注意事项,
- 1.自定义model一般为标准化接口传参类型,如
//设置参数类型
dataType = "getallbyid")
- 2.接口返回值会自动扫描为一个model。
-
- 一般swagger测试时端口号不用8080,因为容易报错,
-
- yaml中要加上
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher