新增requestBodyAdvice配置

This commit is contained in:
Forget 2021-04-25 16:06:29 +08:00
parent ecc4e09921
commit bea4c1a2ed
6 changed files with 45 additions and 19 deletions

View File

@ -7,7 +7,9 @@
2. 修复配置responseBodyAdvice后controller中void方法返回显示错误。
3. 修复往torna推送漏掉pathParams的问题。
4. 修复非json请求集合中绑定枚举强制检查错误的问题。
5. 新增requestAdvice支持可以实现请求参数包装
5. 新增requestBodyAdvice支持可以实现请求参数包装。
6. 修复泛型为List数据时类型为object问题。
7. 修复customFiled为继承参数时配置失效问题。
#### 版本号2.1.3
- 更新日期: 2020-04-11
- 更新内容:

View File

@ -90,7 +90,8 @@ public class ProjectDocConfigBuilder {
this.initCustomRequestFieldsMap(apiConfig);
this.initReplaceClassMap(apiConfig);
this.initConstants(apiConfig);
this.checkResponseBodyAdvice(apiConfig);
this.checkBodyAdvice(apiConfig.getRequestBodyAdvice());
this.checkBodyAdvice(apiConfig.getResponseBodyAdvice());
}
public JavaClass getClassByName(String simpleName) {
@ -179,20 +180,18 @@ public class ProjectDocConfigBuilder {
}
}
private void checkResponseBodyAdvice(ApiConfig config) {
ResponseBodyAdvice responseBodyAdvice = config.getResponseBodyAdvice();
if (Objects.nonNull(responseBodyAdvice) && StringUtil.isNotEmpty(responseBodyAdvice.getClassName())) {
if (Objects.nonNull(responseBodyAdvice.getWrapperClass())) {
private void checkBodyAdvice(BodyAdvice bodyAdvice) {
if (Objects.nonNull(bodyAdvice) && StringUtil.isNotEmpty(bodyAdvice.getClassName())) {
if (Objects.nonNull(bodyAdvice.getWrapperClass())) {
return;
}
try {
Class.forName(responseBodyAdvice.getClassName());
Class.forName(bodyAdvice.getClassName());
} catch (ClassNotFoundException e) {
throw new RuntimeException("Can't find class " + responseBodyAdvice.getClassName() + " for ResponseBodyAdvice.");
throw new RuntimeException("Can't find class " + bodyAdvice.getClassName() + " for ResponseBodyAdvice.");
}
}
}
/**
* 设置高亮样式
*/

View File

@ -112,4 +112,6 @@ public interface DocTags {
* Ignore ResponseBodyAdvice
*/
String IGNORE_RESPONSE_BODY_ADVICE = "ignoreResponseBodyAdvice";
String IGNORE_REQUEST_BODY_ADVICE = "ignoreRequestBodyAdvice";
}

View File

@ -268,7 +268,9 @@ public class ApiConfig {
* Support Spring MVC ResponseBodyAdvice
* @since 1.9.8
*/
private ResponseBodyAdvice responseBodyAdvice;
private BodyAdvice responseBodyAdvice;
private BodyAdvice requestBodyAdvice;
private String style;
@ -718,14 +720,22 @@ public class ApiConfig {
this.displayActualType = displayActualType;
}
public ResponseBodyAdvice getResponseBodyAdvice() {
public BodyAdvice getResponseBodyAdvice() {
return responseBodyAdvice;
}
public void setResponseBodyAdvice(ResponseBodyAdvice responseBodyAdvice) {
public void setResponseBodyAdvice(BodyAdvice responseBodyAdvice) {
this.responseBodyAdvice = responseBodyAdvice;
}
public BodyAdvice getRequestBodyAdvice() {
return requestBodyAdvice;
}
public void setRequestBodyAdvice(BodyAdvice requestBodyAdvice) {
this.requestBodyAdvice = requestBodyAdvice;
}
public String getStyle() {
return style;
}

View File

@ -26,7 +26,7 @@ package com.power.doc.model;
* @since 1.9.8
* @author yu 2020/11/5.
*/
public class ResponseBodyAdvice {
public class BodyAdvice {
private String className;
@ -34,15 +34,15 @@ public class ResponseBodyAdvice {
private String dataField;
public static ResponseBodyAdvice builder(){
return new ResponseBodyAdvice();
public static BodyAdvice builder(){
return new BodyAdvice();
}
public String getClassName() {
return className;
}
public ResponseBodyAdvice setClassName(String className) {
public BodyAdvice setClassName(String className) {
this.className = className;
return this;
}
@ -51,7 +51,7 @@ public class ResponseBodyAdvice {
return dataField;
}
public ResponseBodyAdvice setDataField(String dataField) {
public BodyAdvice setDataField(String dataField) {
this.dataField = dataField;
return this;
}
@ -60,7 +60,7 @@ public class ResponseBodyAdvice {
return wrapperClass;
}
public ResponseBodyAdvice setWrapperClass(Class wrapperClass) {
public BodyAdvice setWrapperClass(Class wrapperClass) {
this.wrapperClass = wrapperClass;
return this;
}

View File

@ -46,7 +46,7 @@ import java.util.stream.Stream;
import static com.power.doc.constants.DocGlobalConstants.FILE_CONTENT_TYPE;
import static com.power.doc.constants.DocGlobalConstants.JSON_CONTENT_TYPE;
import static com.power.doc.constants.DocTags.IGNORE;
import static com.power.doc.constants.DocTags.*;
/**
* @author yu 2019/12/21.
@ -365,6 +365,19 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate<ApiDoc> {
}
if (SpringMvcAnnotations.REQUEST_BODY.equals(annotationName) || DocGlobalConstants.REQUEST_BODY_FULLY.equals(annotationName)) {
apiMethodDoc.setContentType(JSON_CONTENT_TYPE);
if (Objects.nonNull(configBuilder.getApiConfig().getRequestBodyAdvice())
&& Objects.isNull(method.getTagByName(IGNORE_REQUEST_BODY_ADVICE))) {
String requestBodyAdvice = configBuilder.getApiConfig().getRequestBodyAdvice().getClassName();
gicTypeName = new StringBuffer()
.append(requestBodyAdvice)
.append("<")
.append(gicTypeName).append(">").toString();
typeName = new StringBuffer()
.append(requestBodyAdvice)
.append("<")
.append(typeName).append(">").toString();
}
if (JavaClassValidateUtil.isPrimitive(simpleTypeName)) {
StringBuilder builder = new StringBuilder();
builder.append("{\"")