diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e4de1bd --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +target + +# Editor directories and files +.idea +.vscode +*.suo +*.ntvs* +*.njsproj +*.sln +*.sw? diff --git a/annotation/.idea/compiler.xml b/annotation/.idea/compiler.xml index ca67c5c..430bed4 100644 --- a/annotation/.idea/compiler.xml +++ b/annotation/.idea/compiler.xml @@ -7,6 +7,7 @@ + diff --git a/annotation/.idea/vcs.xml b/annotation/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/annotation/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/annotation/pom.xml b/annotation/pom.xml index f9c53f4..daa4c3f 100644 --- a/annotation/pom.xml +++ b/annotation/pom.xml @@ -17,8 +17,8 @@ UTF-8 1.7 1.7 + 3.0.0 - junit @@ -57,6 +57,16 @@ jackson-core 2.12.2 + + io.springfox + springfox-swagger2 + ${swagger.version} + + + io.springfox + springfox-swagger-ui + ${swagger.version} + diff --git a/annotation/src/main/java/org/example/spring_annotation/config/SwaggerConfig.java b/annotation/src/main/java/org/example/spring_annotation/config/SwaggerConfig.java new file mode 100644 index 0000000..f56cb29 --- /dev/null +++ b/annotation/src/main/java/org/example/spring_annotation/config/SwaggerConfig.java @@ -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"); + } + +} diff --git a/annotation/src/main/java/org/example/spring_annotation/WebInit.java b/annotation/src/main/java/org/example/spring_annotation/config/WebInit.java similarity index 97% rename from annotation/src/main/java/org/example/spring_annotation/WebInit.java rename to annotation/src/main/java/org/example/spring_annotation/config/WebInit.java index 3a057b2..9900149 100644 --- a/annotation/src/main/java/org/example/spring_annotation/WebInit.java +++ b/annotation/src/main/java/org/example/spring_annotation/config/WebInit.java @@ -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; diff --git a/annotation/src/main/java/org/example/spring_annotation/WebMvcConfig.java b/annotation/src/main/java/org/example/spring_annotation/config/WebMvcConfig.java similarity index 66% rename from annotation/src/main/java/org/example/spring_annotation/WebMvcConfig.java rename to annotation/src/main/java/org/example/spring_annotation/config/WebMvcConfig.java index 488a7e1..02e0bf7 100644 --- a/annotation/src/main/java/org/example/spring_annotation/WebMvcConfig.java +++ b/annotation/src/main/java/org/example/spring_annotation/config/WebMvcConfig.java @@ -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 mediaTypes = new ArrayList(); mediaTypes.add(new MediaType("utf-8")); converter.setSupportedMediaTypes(mediaTypes); - ; return converter; } + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + logger.info("静态资源映射"); + // +// registry.addResourceHandler("/swagger/**").addResourceLocations("/WEB-INF/swagger/"); + +// +// + registry.addResourceHandler("swagger-ui/**").addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/"); + registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); + } } diff --git a/annotation/src/main/java/org/example/spring_annotation/controller/SC.java b/annotation/src/main/java/org/example/spring_annotation/controller/SC.java index a9ea2bb..0d044b0 100644 --- a/annotation/src/main/java/org/example/spring_annotation/controller/SC.java +++ b/annotation/src/main/java/org/example/spring_annotation/controller/SC.java @@ -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); } } diff --git a/annotation/src/main/java/org/example/spring_annotation/controller/api/UserController.java b/annotation/src/main/java/org/example/spring_annotation/controller/api/UserController.java new file mode 100644 index 0000000..4c4e742 --- /dev/null +++ b/annotation/src/main/java/org/example/spring_annotation/controller/api/UserController.java @@ -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 login(String username, String password) { + ApiResponseData 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; + } +} diff --git a/annotation/src/main/java/org/example/spring_annotation/dto/ApiResponseData.java b/annotation/src/main/java/org/example/spring_annotation/dto/ApiResponseData.java new file mode 100644 index 0000000..8f504d1 --- /dev/null +++ b/annotation/src/main/java/org/example/spring_annotation/dto/ApiResponseData.java @@ -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 { + @ApiModelProperty(name = "响应码", required = true) + private Integer code = 0; + @ApiModelProperty(name = "消息") + private String message = "success"; + @ApiModelProperty(name = "数据") + private T data; +} diff --git a/annotation/src/main/java/org/example/spring_annotation/po/User.java b/annotation/src/main/java/org/example/spring_annotation/po/User.java index 8eceec6..3660999 100644 --- a/annotation/src/main/java/org/example/spring_annotation/po/User.java +++ b/annotation/src/main/java/org/example/spring_annotation/po/User.java @@ -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; }