Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

my project is java spring boot 2 with maven . I use springdoc-openapi-ui dependency. problem is @Parameter(required = false) not working on my api params. Have you tried using the Swagger-specific @ApiParam annotation in addition to the Spring annotations? filpa Apr 28, 2021 at 9:15 @Parameter isn't a spring annotation, so not sure how that should be handled. You should be using @RequestParam which is a Spring MVC annotation. M. Deinum Apr 28, 2021 at 9:21 thank you . my problem is with annotation @Parameter that belongs to io.swagger.v3.oas.annotations.Parameter. by default should be required = false, but as in swagger-ui screen shot shows, labeled with * required! Bashir Zamani Apr 28, 2021 at 9:32

I don't know how much of swagger annotations Springdoc-openapi supports but according to its own example at PetApiDemo in /findByStatus or /findByTags endpoints you can see by applying @RequestParam(required = false) which is a Spring Annotation! a parameter has become optional.

import org.springframework.web.bind.annotation.RequestParam;
default ResponseEntity<List<Pet>> findPetsByStatus(
        @Parameter(
                explode = Explode.TRUE, 
                name = "status", 
                in = ParameterIn.QUERY, 
                description = "Status values that need to be considered for filter", 
                style = ParameterStyle.FORM, 
                schema = @Schema(
                        type = "string", defaultValue = "available", 
                        allowableValues = {"available", "pending", "sold"}
        @Valid @RequestParam(value = "status", required = false)
        List<String> status) {
    return getDelegate().findPetsByStatus(status);

I had the same problem. My solution is add jakarta.annotation.@Nullable for non required parameters.

public ResponseEntity<?> getAllByMonthAndYear(
        @Nullable @RequestParam("month") Integer month,
        @Nullable @RequestParam("year") Integer year
) {...
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.