Commit 9b7ba8d0 authored by bboymoney's avatar bboymoney

swagger状态码自定义

parent 1e2d33e6
...@@ -16,6 +16,11 @@ ...@@ -16,6 +16,11 @@
<option name="name" value="Central Repository" /> <option name="name" value="Central Repository" />
<option name="url" value="http://39.99.202.89:8081/repository/maven-public/" /> <option name="url" value="http://39.99.202.89:8081/repository/maven-public/" />
</remote-repository> </remote-repository>
<remote-repository>
<option name="id" value="public" />
<option name="name" value="public" />
<option name="url" value="http://49.234.59.211:8081/repository/maven-public/" />
</remote-repository>
<remote-repository> <remote-repository>
<option name="id" value="central" /> <option name="id" value="central" />
<option name="name" value="Maven Central repository" /> <option name="name" value="Maven Central repository" />
...@@ -26,5 +31,10 @@ ...@@ -26,5 +31,10 @@
<option name="name" value="JBoss Community repository" /> <option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" /> <option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository> </remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="http://49.234.59.211:8081/repository/maven-public/" />
</remote-repository>
</component> </component>
</project> </project>
package com.servicemall.car.micro;
import com.servicemall.systemcommon.data.Constants;
import com.servicemall.systemcommon.data.ValidCheckException;
import com.servicemall.systemcommon.util.JsonUtil;
import com.servicemall.website.data.Slider;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import team.bangbang.common.data.response.DataResponse;
import team.bangbang.common.data.response.ResponseBase;
import java.util.HashMap;
import java.util.Map;
/**
* 车龄 - 微服务
*
* 对应Feign:com.servicemall.website.feign.SliderFeign
*
* @author 帮帮组
* @version 1.0 2020-12-08
*/
@Api(description = "车龄选项获取接口API")
@RestController
@CrossOrigin(allowCredentials="true", allowedHeaders="*", origins="*", maxAge=3600)
@RequestMapping("/microservice/car/carage")
public final class CarAgeMicro {
/**************************************************************************
* !!除非设计、指导人员有特别说明,否则此处不得随意增加、修改、删除!!
* ------------------------------------
*
*************************************************************************/
/**
* 查询多条轮播图,并转化为相应的POJO对象列表
*
*
* @return 返回结果记录,并转化为相应的POJO对象列表
*/
@ApiOperation(value = "车龄选项", notes = "车龄选项列表", httpMethod = "POST")
@PostMapping(value="/list", produces={ MediaType.APPLICATION_JSON_UTF8_VALUE }, consumes={ MediaType.APPLICATION_JSON_UTF8_VALUE })
@ResponseBody
public ResponseBase carAgeList(@RequestBody String json) {
DataResponse<Object> result = new DataResponse<>();
try {
Map<String, Object> paramMap = JsonUtil.toBean(json, Map.class);
Map<String, Object> datas = new HashMap<>();
String[] carAges= Constants.carAge;
datas.put("carAge", carAges);
result.setData(carAges);
return result;
} catch (ValidCheckException e) {
return new ResponseBase(e.getCode(),e.getMessage());
}
}
}
package com.servicemall.systemcommon.data;
/**
* 常量类
*/
public class Constants {
/*
* 车龄
*/
public static final String[] carAge = {"1年", "2年", "3年", "4年", "5年", "6年", "7年", "8年", "9年", "10年", "10年以上"};
}
package com.servicemall.systemcommon.data;
import org.apache.commons.codec.binary.StringUtils;
/**
* 错误枚举类
*
* @author Warren
*/
public enum ErrorEnum {
/**
* 公共返回码
**/
E_100(100, "成功"),
E_200(200, "请求的服务不存在"),
E_201(201, "请求次数超限或者过于频繁,暂时封禁"),
E_202(202, "请求参数缺少必要字段"),
E_203(203, "json格式有误");
// 成员变量
private int code;
private String message;
// 构造方法
ErrorEnum(int code, String message) {
this.code = code;
this.message = message;
}
// 普通方法
public static String getErrorMsg(int code) {
for (ErrorEnum c : ErrorEnum.values()) {
if (c.getCode() == code) {
return c.message;
}
}
return null;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
package com.servicemall.systemcommon.data;
public class ValidCheckException extends RuntimeException {
private static final long serialVersionUID = 1L;
public ValidCheckException(int code, String message) {
this.code = code;
this.message = message;
}
public ValidCheckException(String message) {
this.message = message;
}
private String message;
private int code;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public String toString() {
return String.format("ValidCheckException-->code:%s,msg:%s", this.getCode(), this.getMessage());
}
}
package com.servicemall.systemcommon.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.servicemall.systemcommon.data.ValidCheckException;
import org.springframework.util.StringUtils;
public class JsonUtil {
public static <T> T toBean(String json, Class<T> clazz) {
if (StringUtils.isEmpty(json))
throw new ValidCheckException(202, "缺少参数");
try {
return JSONObject.toJavaObject(JSON.parseObject(json), clazz);
} catch (Exception e) {
}
throw new ValidCheckException(203, "json格式有误");//TODO 需要再基础类添加
}
public static String toString(Object obj) {
if (obj == null) {
return null;
}
return JSONObject.toJSONString(obj);
}
public static void main(String args[]) {
}
}
package team.bangbang; package team.bangbang;
import com.servicemall.systemcommon.data.ErrorEnum;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.swagger2.annotations.EnableSwagger2; import springfox.documentation.swagger2.annotations.EnableSwagger2;
import team.bangbang.common.config.Config; import team.bangbang.common.config.Config;
import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.ApiInfoBuilder;
...@@ -12,13 +17,17 @@ import springfox.documentation.service.ApiInfo; ...@@ -12,13 +17,17 @@ import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType; import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.spring.web.plugins.Docket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/** /**
* Swagger2配置类 * Swagger2配置类
* * <p>
* 在与spring boot集成时,放在与Application.java同级的目录下。 * 在与spring boot集成时,放在与Application.java同级的目录下。
* * <p>
* 通过@Configuration注解,让Spring来加载该类配置。 * 通过@Configuration注解,让Spring来加载该类配置。
* * <p>
* 再通过@EnableSwagger2注解来启用Swagger2。 * 再通过@EnableSwagger2注解来启用Swagger2。
* *
* @author 帮帮组 * @author 帮帮组
...@@ -37,7 +46,23 @@ public class Swagger2 { ...@@ -37,7 +46,23 @@ public class Swagger2 {
*/ */
@Bean @Bean
public Docket createRestApi() { public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
//添加全局响应状态码
List<ResponseMessage> responseMessageList = new ArrayList<>();
Arrays.stream(ErrorEnum.values()).forEach(errorEnum -> {
responseMessageList.add(
new ResponseMessageBuilder().code(errorEnum.getCode()).message(errorEnum.getMessage()).build()
);
});
return new Docket(DocumentationType.SWAGGER_2)
// 添加全局响应状态码
.globalResponseMessage(RequestMethod.GET, responseMessageList)
.globalResponseMessage(RequestMethod.POST, responseMessageList)
.globalResponseMessage(RequestMethod.PUT, responseMessageList)
.globalResponseMessage(RequestMethod.DELETE, responseMessageList)
.apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.basePackage("com.servicemall")).paths(PathSelectors.any()) .apis(RequestHandlerSelectors.basePackage("com.servicemall")).paths(PathSelectors.any())
.build(); .build();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment