Reputation: 203
I'm doing an exercise with Spring Boot to check version changes. I have created a simple API and implemented spring swagger for API testing. What is missing are the variable path parameters that do not appear in the swagger interface to enter the values. According to the documentation, it is only necessary to define the RestController class and Maven dependency and test it, but that is not the case.
@RestController
@RequestMapping("/customer")
public class CustomerRestController {
@Autowired
CustomerRepository customerRepository;
@GetMapping()
public List<Customer> customers() {
return customerRepository.findAll();
}
@GetMapping("/{id}")
public Customer get(@PathVariable Long id) {
return customerRepository.findById(id).orElse(new Customer());
}
}
I try with 2.1.0, 2.2.0 and 2.3.0. same result.
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.1.0</version>
</dependency>
The project structure is complex, but after compiling the entire paymentchain-parent project you can run the independent customer project and see the result by accessing the url http://localhost:8081/swagger.html
https://github.com/argades/spring-boot-ms-test
Upvotes: 2
Views: 1144
Reputation: 51
This problem is reproduced on the latest dependency versions:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.8.3</version>
</dependency>
I've created an issue for this problem:
https://github.com/springdoc/springdoc-openapi/issues/2874
Marco Osorio's solution of explicitly specifying the name
attribute for @PathVariable
and @RequestParam
worked for me:
@RestController
public class MyRestController {
@Operation(summary = "Create")
@PostMapping(value = "/projects/{projectId}/models/{modelId}/item", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public void createNewItem(
@PathVariable(value = "projectId") String projectId,
@PathVariable(name = "modelId") String modelId,
@RequestParam("itemId", required = false) String itemId,
@RequestBody Map<String, Object> item
) {
return;
}
}
Upvotes: 0
Reputation: 203
I found the solution. Adding this ("id") to PathVariable annotation, show the input into ui
@GetMapping("/{id}")
public Customer get(@PathVariable("id") Long id) {
return customerRepository.findById(id).orElse(new Customer());
}
Thanks
Upvotes: 5