I'm looking to develop the Spring Boot + OpenAPI 3 example by taking a look at the https://www.dariawan.com/tutorials/spring/documenting-spring-boot-rest-api-springdoc-openapi-3/ and https://techsparx.com/software-development/openapi/spring-boot-rest-api-docs.html. In this example, I am looking to pass the pagination details through Swagger UI that I got by adding the springdoc-openapi-ui.
What Spring Boot Configurations do I need to do in order to support custom pagination.?
Add #PageableAsQueryParam annotation on the method
and add springdoc-openapi-data-rest as a dependency
Their is more than one possiblity the simpliest and fastest is if only want to enable the support of spring Pageable Type, you can just enable it using:
SpringDocUtils.getConfig().replaceWithClass(org.springframework.data.domain.Pageable.class,
org.springdoc.core.converters.models.Pageable.class);
Alternately, the projects that use Pageable type can aslo add the follwing dependency together with the springdoc-openapi-ui dependency. This dependency enables the support of spring-data-rest types as well: #RepositoryRestResource and QuerydslPredicate annotations.
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-data-rest</artifactId>
<version>1.5.6</version>
</dependency>
And the add #ParameterObject on the Pageable object on your controller method.
#GetMapping("/books")
public ResponseEntity<List<BookDTO>> getBooks(#ParameterObject Pageable pageable)
"The support for Pageable of spring-data-commons is available out-of-the box since springdoc-openapi v1.6.0. For this, you have to combine #ParameterObject annotation with the Pageable type." - Source
Assuming you are using springdoc-openapi v1.6.0 or grater, you could do something like this:
#GetMapping("/foo")
public Page<Foo> getFoo(#ParameterObject Pageable pageable) {
// do something
}
Related
I have a Spring Boot 2.4 project that is using spring-boot-starter-data-solr. Unfortunately, spring-boot-starter-data-solr is not supported anymore (see https://spring.io/projects/spring-data-solr#overview) , so I can't easily upgrade Spring Boot as I usually do for other projects.
However, I thought I would still try to upgrade my project and keep using the latest spring-boot-starter-data-solr I could get : maybe it's not supported anymore, but I am not making an extensive use of it, so maybe I can take advantage of latest Spring Boot features for a some more time, before there's really a breaking change and I really can't use spring-boot-starter-data-solr
So, I upgraded my project to Spring Boot 2.6.4 letting Spring BOM pull all the latest versions of all dependencies managed. Of course, it failed on spring-boot-starter-data-solr, so I had to declare the version explicitly :
implementation 'org.springframework.boot:spring-boot-starter-data-solr:2.4.13'
I fixed few other things, and my project compiles. However, at startup (and in some integration tests), I have a problem :
#Configuration
public class PersistenceConfig {
#Bean
public TicketRepository ticketRepository(SolrTicketEntityRepository solrTicketEntityRepository) {
return new TicketRepositoryImpl(solrTicketEntityRepository);
}
... some other beans..
}
with SolrTicketEntityRepository defined like below :
public interface SolrTicketEntityRepository extends SolrCrudRepository<TicketEntity, String> {
#Query("?0")
Page<TicketEntity> search(String searchText, Pageable pageable);
With Spring Boot 2.6, the SolrTicketEntityRepository doesn't get instanciated anymore, so I have a missing bean at startup. I've tried adding the #EnableSolrRepositories on PersistenceConfig, but it doesn't do anything.
is there a way to mimic Spring Boot magic, and trigger programmatically SolrTicketEntityRepository / SolrCrudRepository instanciation, to be able to start my application ?
or is it way too complicated, and as recommended here , should I implement the stuff myself with the core Solr libraries without Spring Boot's help (which would be the objective at some point anyway) ?
I have a Spring Boot application that uses springdoc-openapi-ui to document REST endpoint. Also, there is some simple UI with Spring MVC. For some reason, in Swagger UI I see not only schemas for REST but also the schema for ModelAndView class. Is there a way to remove it from there?
I've tried already some options like limiting packages to scan with springdoc.packagesToScan or springdoc.model-and-view-allowed but without any results?
You can hide a Controller or Schema classes with #Hidden annotation, like this:
import io.swagger.v3.oas.annotations.Hidden;
#RestController
#Hidden
public class ItemController
#Hidden annotation is part of springdoc-openapi-ui library.
I am using Camel with Spring Boot and Micrometer. In one of my routes I am using a circuitbreaker with Resilience4j:
.circuitBreaker()
.resilience4jConfiguration()
.timeoutEnabled(true)
.timeoutDuration(2000)
.end()
I am using Micrometer managed by Spring. Before moving to resilience4j with Hystrix I could simply bind it to my Micrometer registry:
#Configuration()
public class MetricsRegistryBuilder {
#Bean
HystrixMetricsBinder registerHystrixMetricsBinder() {
return new HystrixMetricsBinder();
}
}
For Resilience4j there does not exist a binder unfortunately. There is some documentation about how to bind the Resilience4j CircuitBreakerRegistry to Micrometer:
https://resilience4j.readme.io/docs/micrometer
and also how to do it with Spring:
https://resilience4j.readme.io/docs/getting-started-3
I tried to simply autowire the Resilience4j CircuitBreakerRegistry to Micrometer:
#Configuration()
public class MetricsRegistryBuilder {
#Autowired
private CircuitBreakerRegistry circuitBreakerRegistry;
}
Unfortunately Spring does not find the CircuitBreakerRegistry Bean.
Therefore my question is how to bind the CircuitBreakerRegistry, or more abstract the metrics from Resilience4j, to Micrometer when using Camel with Spring?
The only other possible solution I could think of is to manage all Resilience4j configuration manually, define the beans, and hand it over to my Camel configuration. This though seems to me to a lot of work and boilerplate code considering the simple task of binding my Resilience4j metrics.
I am using the following versions:
Camel 3.4.3
Spring 2.3.3.RELEASE
Micormeter 1.5.4
Also I am using camel spring boot starter dependencies:
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-micrometer-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-resilience4j-starter</artifactId>
</dependency>
Why can't you use our Spring Boot 2 Starter?
The Starter autoconfigures the CircuitBreakerRegistry, MeterRegistry and allows external configuration.
I have a project (Spring Boot App + Kotlin) that I would like to have an Open API 3.0 spec for (preferably in YAML). The Springfox libraries are nice but they generate Swagger 2.0 JSON. What is the best way to generate an Open Api 3.0 spec from the annotations in my controllers? Is writing it from scratch the only way?
We have used springdoc-openapi library in our kotlin project, and it meets our need for automating the generation of API documentation using spring boot projects.
It automatically deploys swagger-ui to a spring-boot application
The Swagger UI page should then be available at:
- http://server:port/context-path/swagger-ui.html
The OpenAPI description will be available at the following url for json format:
- http://server:port/context-path/v3/api-docs
Add the library to the list of your project dependencies (No additional configuration is needed)
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.2.32</version>
</dependency>
You could look at spring-restdocs and restdocs-api-spec.
spring-restdocs takes a test-driven approach to API documentation which has many advantages over the introspection-driven approach spring-fox uses. restdocs-api-spec is an extension for spring-restdocs that adds API specification support. Currently it supports OpenAPI2 OpenAPI3 and Postman.
I decided to implement my own generator https://github.com/jrcodeza/spring-openapi maybe you can check it out too. It's based on reflection and supports javax and spring annotations. It also generates inheritance model (with discriminators) based on Jackson annotations. Besides you can define your own interceptors if you want to alter generation process (e.g. when you have your own annotations and need to adjust generated sections of schema). You can use it in runtime mode or as a maven plugin. There is also OpenAPI3 to java client generator, which generates the model from openapi3 spec. Again it generates also Javax annotations and Jackson annotations for correct inheritance.
If you're using jax-rs this tutorial helps. It uses the Apache CXF implementation. I couldn't find any other implementation of jaxrs that uses Spring Boot AND generate Open API 3.0 spec.
You'll need these depedencies:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-service-description-openapi-v3</artifactId>
<version>3.2.4</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>swagger-ui</artifactId>
<version>3.13.6</version>
</dependency>
Here is the general configuration, more detail is in the link:
#Configuration
#EnableAutoConfiguration
#ComponentScan(basePackageClasses = PeopleRestService.class)
public class AppConfig {
#Autowired private PeopleRestService peopleRestService;
#Bean(destroyMethod = "destroy")
public Server jaxRsServer(Bus bus) {
final JAXRSServerFactoryBean factory = new JAXRSServerFactoryBean();
factory.setApplication(new JaxRsApiApplication());
factory.setServiceBean(peopleRestService);
factory.setProvider(new JacksonJsonProvider());
factory.setFeatures(Arrays.asList(new OpenApiFeature()));
factory.setBus(bus);
factory.setAddress("/");
return factory.create();
}
#Bean
public ServletRegistrationBean cxfServlet() {
final ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new CXFServlet(), "/api/*");
servletRegistrationBean.setLoadOnStartup(1);
return servletRegistrationBean;
}
}
https://dzone.com/articles/moving-with-the-times-towards-openapi-v300-adoptio
You can also refer to
https://www.baeldung.com/spring-rest-openapi-documentation
which provides a tutorial on implementing OpenAPI 3.0 with a SpringBoot 1.x or 2.x application using springdoc-openapi.
To summarize, you just add the maven dependency for springdoc-openapi into your application and when you bootRun, go to path
http://server:port/v3/api-docs.yaml/ and you will download an Open API 3.0 spec file in yaml, generated from your application's code.
You can do some other stuff with springdoc-openapi, by accessing the following when your SpringBoot application is running:
http://server:port/v3/api-docs: Gives your spec file in Json format.
http://server:port/swagger-ui.html: Access this in your browser and you will see the swagger documentation.
I'd like to use #Valid annotation to validate my beans in controller method. Unfortunatelly it does not work. I know that in order to make it work I'd have to include spring-mvc into my project and put there mvc:annotation-driven or #EnableMvc... .
But I do not use spring-mvc! I use Wicket framework. How to make #Valid working without incorporating spring-mvc?
Thanks!
#Valid is not specific to spring it is an implementation of JSR 303 bean validation. You can use any other reference implementation or write your own. e.g Apache and Hibernate Validator has reference implementation available. Take a look at this answer Is there an implementation of JSR-303 (bean validation) available?