我已经使用 OpenAPI 库实现了 swagger。 我的应用程序在 spring boot 2.7.1 上运行 我收到“规范中没有定义操作!”,甚至我的控制器也有 api。可能是什么问题呢。 这是代码。
OrderController.java
package com.example.service;
import com.example.service.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/api/v1/orders")
@Tag(name = "contact", description = "the Contact API")
public class OrderController {
//System.out.println("OrderController");
@PostMapping("/orders")
public int saveOrderDetails(@RequestBody Order order) {
int orderId = 1;
return orderId;
}
@Operation(summary = "Find Contacts by name", description = "Name search by %name% format", tags = { "contact" })
@GetMapping(value = "/orders/{orderId}", produces = { "application/json", "application/xml" })
public Order getOrderDetails(@RequestParam("orderId") int orderId) {
Order order = new Order();
return order;
}
@PutMapping("/orders")
public ResponseEntity<String> updateOrderDetails(@RequestBody Order order) {
return new ResponseEntity<>("Updated successfully",
HttpStatus.OK);
}
@DeleteMapping("/orders")
public ResponseEntity<String> deleteOrderDetails(@RequestParam("orderId") int orderId) {
return new ResponseEntity<>("Deleted successfully",
HttpStatus.NO_CONTENT);
}
}
应用程序.java
public class Application {
public static void main(String[] args) throws VtmException
{
SpringApplication.run(Application.class, args);
}
@Bean
public OpenAPI vOpenAPI() {
return new OpenAPI()
.info(new Info().title("Advanced REST API")
.description("REST API application"))
.components(new Components());
}
pom.xml
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.8.0</version>
</dependency>
应用程序.属性
springdoc.packagesToScan=com.example.service
springdoc.pathsToMatch=/vtm/v1
# swagger-ui custom path
springdoc.swagger-ui.path=/swagger-ui.html
# /api-docs endpoint custom path
springdoc.api-docs.path=/v3/api-docs
springdoc.api-docs.enabled=true
尝试在 swagger 中获取输出,但得到“规范中没有定义操作!”
我希望能够为 api 获得正确的输出。