add swagger to annotation
This commit is contained in:
parent
81a3ca894f
commit
d03f13a2bf
10
.gitignore
vendored
Normal file
10
.gitignore
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
target
|
||||
|
||||
# Editor directories and files
|
||||
.idea
|
||||
.vscode
|
||||
*.suo
|
||||
*.ntvs*
|
||||
*.njsproj
|
||||
*.sln
|
||||
*.sw?
|
1
annotation/.idea/compiler.xml
generated
1
annotation/.idea/compiler.xml
generated
@ -7,6 +7,7 @@
|
||||
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||
<outputRelativeToContentRoot value="true" />
|
||||
<module name="spring_annotation" />
|
||||
<module name="annotation" />
|
||||
</profile>
|
||||
</annotationProcessing>
|
||||
</component>
|
||||
|
6
annotation/.idea/vcs.xml
generated
Normal file
6
annotation/.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||
</component>
|
||||
</project>
|
@ -17,8 +17,8 @@
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.7</maven.compiler.source>
|
||||
<maven.compiler.target>1.7</maven.compiler.target>
|
||||
<swagger.version>3.0.0</swagger.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
@ -57,6 +57,16 @@
|
||||
<artifactId>jackson-core</artifactId>
|
||||
<version>2.12.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger2</artifactId>
|
||||
<version>${swagger.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger-ui</artifactId>
|
||||
<version>${swagger.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -0,0 +1,48 @@
|
||||
package org.example.spring_annotation.config;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import springfox.documentation.builders.ApiInfoBuilder;
|
||||
import springfox.documentation.builders.PathSelectors;
|
||||
import springfox.documentation.builders.RequestHandlerSelectors;
|
||||
import springfox.documentation.service.ApiInfo;
|
||||
import springfox.documentation.service.Contact;
|
||||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spring.web.plugins.Docket;
|
||||
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
||||
|
||||
@EnableWebMvc
|
||||
@EnableSwagger2
|
||||
@Configuration
|
||||
public class SwaggerConfig {
|
||||
private boolean enabled = true;
|
||||
private Logger logger = Logger.getLogger(SwaggerConfig.class);
|
||||
|
||||
@Bean
|
||||
public Docket createRestApi() {
|
||||
logger.info("开始创建api 文档");
|
||||
return new Docket(DocumentationType.SWAGGER_2)
|
||||
.apiInfo(apiInfo())
|
||||
.select()
|
||||
.apis(RequestHandlerSelectors.basePackage("org.example.spring_annotation.controller")) // 注意修改此处的包名
|
||||
.paths(PathSelectors.any())
|
||||
.build();
|
||||
}
|
||||
|
||||
private ApiInfo apiInfo() {
|
||||
return new ApiInfoBuilder()
|
||||
.title("接口列表 v1.1.0") // 任意,请稍微规范点
|
||||
.description("接口测试") // 任意,请稍微规范点
|
||||
// .termsOfServiceUrl("http://java.xiaoyan.me/demo/spring/swagger-ui.html") // 将“url”换成自己的ip:port
|
||||
.contact(getContact()) // 无所谓(这里是作者的信息)
|
||||
.version("1.1.0")
|
||||
.build();
|
||||
}
|
||||
|
||||
private Contact getContact() {
|
||||
return new Contact("xiaoyan", "http://xiaoyan.me", "me@xiaoyan.me");
|
||||
}
|
||||
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package org.example.spring_annotation;
|
||||
package org.example.spring_annotation.config;
|
||||
|
||||
import org.apache.log4j.LogManager;
|
||||
import org.apache.log4j.Logger;
|
@ -1,4 +1,4 @@
|
||||
package org.example.spring_annotation;
|
||||
package org.example.spring_annotation.config;
|
||||
|
||||
import org.apache.log4j.LogManager;
|
||||
import org.apache.log4j.Logger;
|
||||
@ -11,6 +11,7 @@ import org.springframework.http.converter.json.MappingJackson2HttpMessageConvert
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -28,6 +29,7 @@ public class WebMvcConfig implements WebMvcConfigurer {
|
||||
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
|
||||
logger.info("MVC配置:启用静态资源解析-------->DefaultServletHandlerConfigurer");
|
||||
configurer.enable();
|
||||
//
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ -37,7 +39,18 @@ public class WebMvcConfig implements WebMvcConfigurer {
|
||||
List<MediaType> mediaTypes = new ArrayList<MediaType>();
|
||||
mediaTypes.add(new MediaType("utf-8"));
|
||||
converter.setSupportedMediaTypes(mediaTypes);
|
||||
;
|
||||
return converter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
logger.info("静态资源映射");
|
||||
//<mvc:resources mapping="/swagger/**" location="/WEB-INF/swagger/"/>
|
||||
// registry.addResourceHandler("/swagger/**").addResourceLocations("/WEB-INF/swagger/");
|
||||
|
||||
// <mvc:resources mapping="/swagger-ui/**" location="classpath:/META-INF/resources/webjars/springfox-swagger-ui/"/>
|
||||
// <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>
|
||||
registry.addResourceHandler("swagger-ui/**").addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");
|
||||
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
|
||||
}
|
||||
}
|
@ -17,6 +17,6 @@ public class SC {
|
||||
@RequestMapping("/sc-test-json")
|
||||
@ResponseBody
|
||||
public User testUser() {
|
||||
return new User(1, "张三");
|
||||
return new User(1, "张三",null,null);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,37 @@
|
||||
package org.example.spring_annotation.controller.api;
|
||||
|
||||
import io.swagger.annotations.*;
|
||||
import org.example.spring_annotation.dto.ApiResponseData;
|
||||
import org.example.spring_annotation.po.User;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@Api
|
||||
@RequestMapping("/user")
|
||||
@RestController
|
||||
public class UserController {
|
||||
|
||||
@PostMapping("/login")
|
||||
@ApiOperation(
|
||||
value = "登录", httpMethod = "POST", notes = "账号登录",
|
||||
response = User.class
|
||||
)
|
||||
@ApiImplicitParams({
|
||||
@ApiImplicitParam(required = true, name = "username", value = "账号", dataType = "String"),
|
||||
@ApiImplicitParam(required = true, name = "password", value = "密码", dataType = "String")
|
||||
})
|
||||
public ApiResponseData<User> login(String username, String password) {
|
||||
ApiResponseData<User> data = new ApiResponseData<>();
|
||||
if (null == username || null == password) {
|
||||
data.setCode(1);
|
||||
data.setMessage("用户名和密码不能为空");
|
||||
} else if ("admin".equals(username) && "admin".equals(password)) {
|
||||
data.setData(new User(1, "张三", username, password));
|
||||
} else {
|
||||
data.setCode(2);
|
||||
data.setMessage("账号或密码错误");
|
||||
}
|
||||
return data;
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package org.example.spring_annotation.dto;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@ApiModel
|
||||
public class ApiResponseData<T> {
|
||||
@ApiModelProperty(name = "响应码", required = true)
|
||||
private Integer code = 0;
|
||||
@ApiModelProperty(name = "消息")
|
||||
private String message = "success";
|
||||
@ApiModelProperty(name = "数据")
|
||||
private T data;
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
package org.example.spring_annotation.po;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
@ -7,7 +9,14 @@ import lombok.NoArgsConstructor;
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@ApiModel("用户实体")
|
||||
public class User {
|
||||
@ApiModelProperty(value = "ID")
|
||||
private Integer id;
|
||||
@ApiModelProperty(value = "姓名")
|
||||
private String name;
|
||||
@ApiModelProperty(value = "用户名")
|
||||
private String username;
|
||||
@ApiModelProperty(value = "密码")
|
||||
private String password;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user