记 Swagger 2
Maven坐标:
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
配置Swagger2:
package com.pinecone.leaf.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @EnableSwagger2 @Configuration public class Swagger2 { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.pinecone.leaf.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder().title("Springboot使用Swagger2构建Api文档") .description("构建简单优雅的Restful风格api") .termsOfServiceUrl("localhost") .version("1.0") .build(); } }
实体类(使用Lombok):
package com.pinecone.leaf.domain; import lombok.*; import javax.persistence.*; @Getter @Setter @NoArgsConstructor @AllArgsConstructor @ToString @Entity @Table(name = "user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String name; }
Dao层:
package com.pinecone.leaf.repository; import com.pinecone.leaf.domain.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface UserRepository extends JpaRepository<User, Integer> { }
提供单例:
package com.pinecone.leaf.singleton; import com.pinecone.leaf.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class RepositorySingleTon { public static UserRepository userRepository; @Autowired public RepositorySingleTon(UserRepository userRepository) { RepositorySingleTon.userRepository = userRepository; } }
控制器:
package com.pinecone.leaf.controller; import com.pinecone.leaf.domain.User; import com.pinecone.leaf.singleton.RepositorySingleTon; import org.springframework.web.bind.annotation.*; import java.util.List; import java.util.Optional; @RestController public class UserController { @GetMapping("/users") public List<User> findAll() { return RepositorySingleTon.userRepository.findAll(); } @PostMapping("/users") public void save(User user) { RepositorySingleTon.userRepository.save(user); } @PutMapping("/users") public void put(User user) { RepositorySingleTon.userRepository.save(user); } @DeleteMapping("/users") public void deleteAll() { RepositorySingleTon.userRepository.deleteAll(); } @GetMapping("/users/{id}") public Optional<User> findById(@PathVariable Integer id) { return RepositorySingleTon.userRepository.findById(id); } @DeleteMapping("/users/{id}") public void deleteById(@PathVariable Integer id) { RepositorySingleTon.userRepository.deleteById(id); } }
访问 Swagger UI(我端口直接用的80),大致如下图: