Spring Boot and Thymeleaf 3.0.0.RELEASE integration - java

I have a problem, when I try integrate Spring Boot 1.3.5.RELEASE and Thymeleaf 3.0.0.Release. I know that Spring Boot now support Thymeleaf 3 version so I try workaround this problem like this :
#SpringBootApplication(exclude={org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration.class})
and add my own SpringWebConfig configuration.
unfortunately received error like this :
java.lang.ClassNotFoundException: org.thymeleaf.resourceresolver.IResourceResolver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_66]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_66]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_66]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_66]
... 37 common frames omitted
Wrapped by: java.lang.NoClassDefFoundError: org/thymeleaf/resourceresolver/IResourceResolver
wrapped by: java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration due to org/thymeleaf/resourceresolver/IResourceResolver not found. M ake sure your own configuration does not rely on that class. This can also happen if you are #ComponentScanning a springframework package (e.g. if you put a #ComponentScan in the default package by mistake)

It's much simpler, just read this:
http://docs.spring.io/spring-boot/docs/1.5.x/reference/htmlsingle/#howto-use-thymeleaf-3
<properties>
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
</properties>

spring boot 1.4.0 + thymeleaf 3.0.1
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
<version>2.0.3</version>
</dependency>

As stated in the documentation for 1.4.7.RELEASE:
https://docs.spring.io/spring-boot/docs/1.4.7.RELEASE/reference/htmlsingle/#howto-use-thymeleaf-3
If you use Gradle put this in the build.gradle file:
Gradle: build.gradle
ext['thymeleaf.version'] = '3.0.2.RELEASE'
ext['thymeleaf-layout-dialect.version'] = '2.1.1'
If you use Maven put this in the pom.xml file:
Maven: pom.xml
<properties>
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
</properties>
(since I have not enough reputation, I posted an answer instead of a comment on one of the previous answers. Especially the build.gradle part.)

For Maven project just add following line on pom.xml
<properties>
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
</properties>
For gradle project create a gradle.properties file with following contents
thymeleaf.version=3.0.2.RELEASE
thymeleaf-layout-dialect.version=2.1.1

I have Spring Boot 1.3.3 working with Thymeleaf 3 using this Configuration class. I recall having to work to get around that same exception. Also, ThymeleafAutoConfiguration is excluded in my autoscan setup, like it is in yours.
#Configuration
#EnableConfigurationProperties(ThymeleafProperties.class)
#ConditionalOnClass(SpringTemplateEngine.class)
#AutoConfigureAfter(WebMvcAutoConfiguration.class)
public class ThymeleafConfiguration extends WebMvcConfigurerAdapter implements ApplicationContextAware {
private ApplicationContext applicationContext;
#Autowired
ThymeleafProperties properties;
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
#Bean
public ViewResolver viewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
resolver.setCharacterEncoding("UTF-8");
return resolver;
}
private TemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.addTemplateResolver(urlTemplateResolver());
engine.addTemplateResolver(templateResolver());
// pre-initialize the template engine by getting the configuration. It's a side-effect.
engine.getConfiguration();
return engine;
}
private ITemplateResolver templateResolver() {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setApplicationContext(applicationContext);
resolver.setPrefix("classpath:templates/");
resolver.setSuffix(".html");
resolver.setTemplateMode(TemplateMode.HTML);
resolver.setCacheable(properties.isCache());
return resolver;
}
private UrlTemplateResolver urlTemplateResolver() {
return new UrlTemplateResolver();
}
}
(The resolver.setPrefix, resolver.setSuffix, and resolver.setTemplateMode may not be necessary anymore, but they were with the first beta release.)

Do the simply step by step
1. adding dependency for thymeleaf.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2. adding dependency for thymeleaf dialect.
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
</dependency>
3. SpringBoot autoconfiguration for thymeleaf
spring.thymeleaf.cache=false
spring.thymeleaf.check-template=true
spring.thymeleaf.check-template-location=true
spring.thymeleaf.enabled=true
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.prefix=/WEB-INF/html/
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML
spring.thymeleaf.reactive.max-chunk-size=0

Related

Spring cloud-config client giving error Failed to configure a DataSource: 'url' attribute is not specified

Spring cloud-config server started with application.properties :
server.port:8888
spring.application.name=test-config-server
spring.cloud.config.server.git.uri=https://gitlab.com/pearsontechnology/gpt/sms/sms-micro-services/config-server.git
spring.cloud.config.server.git.default-label=develop
#Private repo. access credentials
spring.cloud.config.server.git.username=xxx
spring.cloud.config.server.git.password=xxxx
spring.cloud.config.server.git.clone-on-start=true
spring.cloud.config.profile=dev
On starting the config-client,
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
My version of spring boot, spring-cloud and dependencies are as follows from pom.xml :
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.2</version>
<java.version>17</java.version>
<spring-cloud.version>2021.0.3</spring-cloud.version>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<scope>runtime</scope>
</dependency>
application.prop of config-client :
spring.application.name=systems-lookup-service
spring.cloud.config.profile=dev
spring.config.import=optional:configserver:
server.port=8081
Properties related to Datasource like url etc. need to be taken from
systems-lookup-service-dev.properties hosted on Git.
custom.url=jdbc:oracle:thin:#localhost:1998/smscert
custom.username=smscert
custom.password=go#salt
custom.driverClassName=
And the DAO class in config-client accessing the db :
public class XXDaoImpl implements XXDao {
private JdbcTemplate jdbcTemplate;
#Autowired(required=false)
private DataSourceConfig config;
#Autowired
public SystemDaoImpl(JdbcTemplate jdbcTemplateIn){
final DataSource dataSource = DataSourceBuilder.create()
.driverClassName(config.getDriverClassName())
.url(config.getUrl())
.username(config.getUsername())
.password(config.getPassword())
.build();
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
...............
}
#Component
#ConfigurationProperties("custom")
public class DataSourceConfig {
private String url;
private String username;
private String password;
//#Value("${greeting.message}")
private String driverClassName;
....
}
I believe you follow the first boot-strapping for your central cloud registration to do so you need following artifact with in your client service pom file.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
Add the following properties to you client service property file application.prop
spring.application.name=systems-lookup-service
spring.cloud.config.uri=http://localhost:"cloud-config-port"
spring.profiles.active=dev
spring.config.import=optional:configserver:
With in main class on central cloud config add the annotation #EnableConfigServer and within it pom file add following artifact
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
append following properties to your central cloud config property file
spring.application.name=configuration-server
server.port=8780
management.endpoints.web.exposure.include=*
spring.cloud.config.server.git.uri=file:absoluthe-path
spring.cloud.config.server.git.clone-on-start=true
spring.cloud.config.allowOverride=true
Finally add your client service properties in your gitrepo with naming servicename-profile convention.
Extra point
You may consider to use spring cloud boss, for hot reloading the configs and not restarting services to handshake again, find out more in here.
Disabled auto-configuration of the data source by annotating client main with #SpringBootApplication(exclude={DataSourceAutoConfiguration.class})

Remove resources automatically added by FF4J

recently i have added FF4j dependency on my application, after that new resource end points have appeared in my swagger ui, is there any way to disable these end points?
the dependency add was
<dependency>
<groupId>org.ff4j</groupId>
<artifactId>ff4j-spring-boot-starter</artifactId>
<version>${ff4j.version}</version>
</dependency>
<dependency>
If you are using SpringDoc:
springdoc.packages-to-exclude=com.unwantedPackage
If you are using SpringFox:
#Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select().paths(Predicate.not(PathSelectors.regex("/api/ff4j/store.*")).apis(RequestHandlerSelectors.basePackage("com.package.controller"))
.paths(PathSelectors.any())
.build().apiInfo(apiInfo());
Remove Basic Error Controller In SpringFox SwaggerUI

JMSListener annotation is not working along with REST service

I am using "Spring Boot - 2.1.5.RELEASE" and "Spring framework - 5.1.7.RELEASE".
JMS listener annotation is not picking a message from a MQ queue. No error logs are rolling in my IntelliJ IDEA as well.
About my project - I am exposing a REST service which sends and receives a message from MQ (used JMSTemplate annotation). Till now it was working. Now I have to add another class which should listen for a queue. Hence added #JMSListener annotation which is not picking any message. Any insight will be really helpful.
The pom has below specified dependencies.
<dependency>
<groupId>com.ibm.mq</groupId>
<artifactId>mq-jms-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.ibm.mq</groupId>
<artifactId>com.ibm.mq.allclient</artifactId>
<version>9.1.4.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
The class has below specified method.
import org.springframework.jms.annotation.JmsListener;
#Component
public class PickMyMessage {
#JmsListener(destination = "IN",containerFactory = "myFactory")
public void pullMyMessaage(String message){
System.out.println("Message is pulled..");
}
}
My main application has #EnableJMS annotation. In my application.yml file I have given below information.
ibm:
mq:
channel: MY.APP.SVRCONN
connName: 192.168.0.1(1415)
password: Pswd
queueManager: QM01
user: appsrv
My Bean class configs are
My JMS connection factory as below.
#Bean
public JmsListenerContainerFactory<?> myFactory(ConnectionFactory connectionFactory,
DefaultJmsListenerContainerFactoryConfigurer configurer) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
configurer.configure(factory, connectionFactory);
}
Not sure what else I am missing here. Any insight will be really helpful.
Instead of my existing project, if i create any simple demo project it is working as expected.
The class PickMyMessage is not a Spring bean, that's why Spring ignores it. Declare it as a #Bean or as a #Component.
May be there are further problems. But this is the first one that needs to be resolved.

Swagger with Spring Boot 2.0 leads to 404 error page

I'm trying to integrate my Spring Boot version 2.0.1.RELEASE with Swagger.
From this blog post it seemed like it will be easy by just adding two Maven dependencies and everything should work.
So I added the following dependencies to the pom:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
And created the SwaggerConfig bean:
#Configuration
#EnableSwagger2
public class SwaggerConfig {
#Bean
public Docket api() {
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
return docket;
}
}
And in the properties file I ended up with these 3 entries during the attempts to make it work:
spring.application.name=cat-service
management.server.servlet.context-path=/cat-service
server.servlet.contextPath=/cat-service
But at the end, when accessing
http://localhost:8080/cat-service/api/v2/api-docs
or the UI page at
http://localhost:8080/cat-service/swagger-ui.html
I get a page not found error.
I found this issues in the swagger github page and this question in stackoverflow but I was not able to change my 404 error.
I was able to make it work with Spring boot version 2.0.4.RELEASE and this blog post:
I added these dependencies:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
And this configuration file:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
#Configuration
#EnableSwagger2
public class SpringFoxConfig {
#Bean
public Docket apiDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
And it worked.
The Swagger UI can be reached at /swagger-ui.html#
Please check the reference: https://springfox.github.io/springfox/docs/current/
"2.1.3. Migrating from existing 2.x version"
You can remove springfox-swagger2 and springfox-swagger-ui from your pom.xml and add springfox-boot-starter instead (for example version 3.0.0). Also you can remove the #EnableSwagger2 annotations
And: "swagger-ui location has moved from https://host/context-path/swagger-ui.html to https://host/context-path/swagger-ui/index.html OR https://host/context-path/swagger-ui/ for short. This makes it work much better with pulling it as a web jar and turning it off using configuration properties if not needed."
First add SwaggerConfig.java file at the same package of your springboot file like the following example.
#Configuration
#EnableSwagger2
#EnableWebMvc
public class SwaggerConfig extends WebMvcConfigurerAdapter {
#Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
try this
http://localhost:8080/spring-security-rest/api/swagger-ui.html
or
http://localhost:8080/spring-security-rest/swagger-ui.html
If that does not work, try to change the path in application.properties
Add this to application.properties:
server.servlet-path=/loop-service
and try the following urls:
http://localhost:8080/loop-service/swagger-ui.html (UI Docs)
http://localhost:8080/loop-service/v2/api-docs (JSON Docs)
Result :
OpenAPI
Just use springdoc-openapi-ui instead.
The Maven dependency:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.11</version>
</dependency>
Or Gradle:
implementation 'org.springdoc:springdoc-openapi-ui:1.6.11'
Links
UI: http://localhost:8080/swagger-ui.html
Json: http://localhost:8080/v3/api-docs
Yaml: http://localhost:8080/v3/api-docs.yaml
That's really all there is to it... No annotations / configuration needed. Cheers!
For Spring Security
If you are using spring security, make sure you can reach these paths for it to work:
"/swagger-ui.html" <-- for UI
"/swagger-ui/**" <-- for UI redirects
"/v3/api-docs/**" <-- for json docs and openapi configuration
"/v3/api-docs.yaml" <-- for yaml docs
For more information:
https://www.baeldung.com/spring-rest-openapi-documentation
I also had the same issue (404 Not Found with springfox 3.0.0). By setting the logging level to "DEBUG", I was able to see endpoints for /v3/api-docs and they worked, but there was nothing about "swagger-ui".
I finally found https://github.com/springfox/springfox/issues/3285, which indicates that:
the new url in 3.0.0 is /swagger-ui/index.html or /swagger-ui/ rather than /swagger-ui.html"
Could they not have added a debug log to indicate where the swagger UI is available?
Springfox swagger UI opens at path /swagger-ui/index.html.
Make sure you are allowing correct ResourceLocations and path excluded from interceptors
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/swagger-ui/**").addResourceLocations("classpath:/META-INF/resources/swagger-ui.html");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
Note: i'm using Springfox swagger version 3.0.0 and Springboot version 2.5.3
This worked for me, I used the WebMvcConfigurer instead of the WebMvcConfigurerAdapter because that class is already deprecated.
#Configuration
#EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {
#Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.illary.controller"))
.paths(PathSelectors.any())
.build()
.apiInfo(metaData());
}
private ApiInfo metaData() {
return new ApiInfoBuilder()
.title("Spring Boot Swagger App")
.description("\"Spring Boot Swagger Server App\"")
.version("1.0.0")
.license("Apache License Version 2.0")
.licenseUrl("https://www.apache.org/licenses/LICENSE-2.0\"")
.build();
}
public ApiInfo apiInfo() {
final ApiInfoBuilder builder = new ApiInfoBuilder();
builder.title("Swagger Test App").version("1.0").license("(C) Copyright Test")
.description("The API provides a platform to query build test swagger api");
return builder.build();
}
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
For Spring Boot, just use the dependency below, it's all it needs to work on the URL /swagger-ui/ (the trailing slash is mandatory).
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
Before trying that I was trying to use the classic dependencies of swagger2 and swagger-ui, and none of the suggested URLs were working.
Another possibility is the location of your Swagger config file; you need to place it at the same package or a subpackage of the spring boot file's.
Like the above picture:
Don't forget to change server.contextPath to server.servlet.contextPath if you upgrade Spring Boot to 2+.
Solution: You just need to remove #EnableWebMvc from the configuration classes.
Description: #EnableWebMvc turn on the class org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport. In spring-boot, there is an autoconfiguration class org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration, which has the annotation #ConditionalOnMissingBean(WebMvcConfigurationSupport.class).
What we get in the end: By adding #EnableWebMvc to the project, we ourselves become responsible for everything, since we turn off spring-boot auto-configuration.
After adding below line in application.properties, it started working
spring.web.resources.static-locations: classpath:/webapp/
But I'm not sure why do we have to add this.
Adding the code, which I think could be relevant. Dependencies are as below:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
...
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<!-- Swagger Dependencies -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
And main class as
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
#SpringBootApplication
#EnableSwagger2
public class ProxyApplication {
public static void main(String[] args) {
SpringApplication.run(ProxyApplication.class, args);
}
#Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.ghsatpute.proxy"))
.paths(PathSelectors.any())
.build();
}
}
Adding this if it can help somebody. This URL worked for me
http://localhost:8003/v2/api-docs
and for swagger-ui this url worked for me out of the box:
http://localhost:8003/swagger-ui.html
Check if you are using right configured url for swagger specs. The urls like http://localhost:8080/spring-security-rest/api/swagger-ui/
didn't work , and I had been getting 404.
Below are the steps to enable swagger in spring boot application:
Add springfox-swagger2 and springfox-swagger-ui dependencies.
To enable the Swagger2 in your project you should use #EnableSwagger2
Define a docket bean ad below
#Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2).groupName("group-name").apiInfo(apiInfo()).select().paths(predicate()).build();
}
private Predicate<String> predicate() {
return or(regex("/api/v1.*"), regex("/api/v2.*"));
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("The title of the your choice")
.contact("your#email.com").license("Licence name ").version("1.0").build();
}
Swagger UI url pattern:
localhost:<server.port>/<server.servlet.context-path>/swagger-ui.html
For anyone still looking for this, using OpenAPI 3.0, not Springfox, I got it running using WebMvcConfigurationSupport without the need of #EnableWebMvc.
The only Dependency needed:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.8</version>
</dependency>
I initially just got the JSON and a 404 on the UI.
What ultimately worked was adding the right resource handlers to my WebConfig and configuring my SecurityConfig to allow unrestricted access for everyone.
The resource handlers in my WebConfig:
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/swagger-ui/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/4.10.3/swagger-initializer.js")
.addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/4.10.3/index.html")
.addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/4.10.3/swagger-ui.css")
.addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/4.10.3/index.css")
.addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/4.10.3/swagger-ui-bundle.js")
.addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/4.10.3/swagger-ui-standalone-preset.js");
}
Important was to add each one specifically (with version and everything, ** didn't work). Otherwise it wouldn't run in the cloud.
I also needed to add the paths "/v3/api-docs/**", "/swagger-ui/**" my SecurityConfig to allow access to these resources. So in my case I added those here:
#Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity.addFilterBefore(...
.authorizeRequests().antMatchers("/v3/api-docs/**", "/swagger-ui/**", "...")...
}
and:
#Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers(HttpMethod.GET, "/v3/api-docs/**", "/swagger-ui/**", "...");
}
It became working for me after removing #EnableWebMvc
Faced same issue just resolved with change in dependency
Refer https://www.vojtechruzicka.com/documenting-spring-boot-rest-api-swagger-springfox/
compile "io.springfox:springfox-swagger2:2.9.2"
Previously was using - Not working
compile group: 'io.springfox', name: 'springfox-swagger2', version: '3.0.0'
I was stuck for a day with this issue. My issue was incorrect port configuration. Check your application.yml/application.properties file for port configuration. There I had mistakenly added both
server.port , management.server.port with different values 🤣 .

How can I create ws inbound-gateway with Java DSL in Spring integration?

I have the following ws inbound gateway. How can I do this configuration with Spring Integration Java 8 DSL?
<int-ws:inbound-gateway id="ws-inbound-gateway"
request-channel="ws-request-channel"
reply-channel="ws-response-channel"
error-channel="ws-error-channel"/>
Unfortunally I don't find a first level support for this kind of inbound gateway, however you can fix this as below:
#Configuration
#EnableIntegration
public class IntegrationConfiguration {
#Bean
public SimpleWebServiceInboundGateway SimpleWebServiceInboundGateway() {
SimpleWebServiceInboundGateway simpleWebServiceInboundGateway = new SimpleWebServiceInboundGateway();
// your inbound configurtion
.....
return simpleWebServiceInboundGateway;
}
#Bean
public IntegrationFlow integrationFlow(){
return IntegrationFlows.from(SimpleWebServiceInboundGateway())
// your pipeline
.....
.get();
}
}
in your maven pom don't forget this dependency
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-java-dsl</artifactId>
<version>1.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-ws</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
I hope that this can help you

Categories

Resources