发布/放置/删除未显示在招摇的弹簧靴中



这是我的swagger配置文件

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;

import static springfox.documentation.builders.PathSelectors.regex;
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("example.com"))
.paths(regex("/product.*"))
.build()
.apiInfo(metaData());
}
private ApiInfo metaData() {
return new ApiInfo(
"Spring Boot REST API",
"Spring Boot REST API for Online",
"1.0",
"Terms of service",
new Contact("Example Example", "https://springframework.guru/about/", "example@gmail.com"),
"Apache License Version 2.0",
"https://www.apache.org/licenses/LICENSE-2.0");
}
}

学生控制器

import java.util.List;
import java.util.Optional;
import com.example.learnspring.StudentRepository.StudentRepository;
import com.example.learnspring.model.StudentDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
public class StudentController {

@Autowired
private StudentRepository eRepo;
@PostMapping("/employees")
public StudentDto save (@RequestBody StudentDto employee) {
return eRepo.save(employee);
}

//@RequestMapping("/greeting/{lang}")
@RequestMapping(value = "/greeting/{lang}", method = RequestMethod.GET)

@GetMapping("/student")
public List<StudentDto> get () {
return eRepo.findAll();
}
@GetMapping("/student/{id}")
public StudentDto get (@PathVariable int id) {
Optional<StudentDto> employee = eRepo.findById(id);
if (employee.isPresent()) {
return employee.get();
}
throw new RuntimeException("Not found for the id "+id);
}
@PutMapping("/student/{id}")
public StudentDto update (@PathVariable int id, @RequestBody StudentDto employee) {
employee.setId(id);
return eRepo.save(employee);
}
@DeleteMapping("/student/{id}")
public ResponseEntity<HttpStatus> delete (@PathVariable int id) {
eRepo.deleteById(id);
return new ResponseEntity<HttpStatus>(HttpStatus.NO_CONTENT);

}
}

学生DTO级

import lombok.*;
import lombok.experimental.FieldDefaults;
import javax.persistence.*;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
@Entity
@Table(name = "student")
@Setter
@Getter
@NoArgsConstructor
@Data
@FieldDefaults(level= AccessLevel.PRIVATE)
public class StudentDto {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false)
private int id;
@Column(name = "name")
@NotNull(message = "Name cannot be null")
private String name;
@Column(name = "age")
@NotNull(message = "Age cannot be null")
@Min(value = 15, message = "Age should not be less than 15")
@Max(value = 65, message = "Age should not be greater than 65")
private int age;
public String getName() {
return name;
}
public void setName(String name){
this.name=name;
}
public int getAge() {
return age;
}
public void setAge(int age){
this.age=age;
}
}

我的存储库

import com.example.learnspring.model.StudentDto;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface StudentRepository extends JpaRepository<StudentDto, Integer> {
}

我的应用程序类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
@EnableSwagger2
public class LearnSpringApplication {
public static void main(String[] args) {
SpringApplication.run(LearnSpringApplication.class, args);
}
}

application.properties

spring.datasource.url=jdbc:postgresql://localhost:5432/testdb
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driver-class-name=org.postgresql.Driver
server.port=9090
spring.jpa.hibernate.ddl-auto=update

spring.mvc.pathmatch.matching-strategy=ant-path-matcher

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>LearnSpring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>LearnSpring</name>
<description>LearnSpring</description>
<properties>
<java.version>18</java.version>
<swagger.version>3.0.0</swagger.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
<!--springfox dependencies for api documentations in swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>

<dependency>
<groupId>org.apache.ivy</groupId>
<artifactId>ivy</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.project-lombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

当我运行这个应用程序时,一切看起来都很好,但我看不到POST/PUT/或DELETE。我试着从网上找到很多东西,但都不起作用。我是这个话题的新手。实际上,我想编写null输入并获取异常以进行异常处理。

因此您的Docket配置为product指定了正则表达式模式

@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("example.com"))
.paths(regex("/product.*"))
.build()
.apiInfo(metaData());
}

但是您的控制器没有任何带有/product的API。因此,您无法在swagger文档中看到API。

尝试使用下面的配置,看看它是否显示了所有端点,然后开始处理regex配置。

@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}

上面的配置基本上在任何地方都匹配web应用程序中的所有API路径。

您可以访问您的API更改APPLICATION_PORTCONTEXT-PATH

http://localhost:{APPLICATION_PORT}}/{CONTEXT-PATH}}/swagger-ui.html

最新更新