I have removed the #EnableSwagger2 & io.springfox:springfox-swagger2 dependency so as to not use the traditional swagger related annotations and instead use a JSON file.
I have changed the Swagger from annotations based to JSON based using the details shared here : Overcoming Swagger Annotation Overload by Switching to JSON
But now it is showing
Can someone help me with what may be causing the issue?
I think you have not added the swagger dependency in the Pom.xml file:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.5.0</version>
</dependency>
I think might be this can be the reason it is not opening the Swagger UI.
Please try to create the bean in the configuration file so that swagger can run it properly.
#Configuration
#EnableSwagger2
public class SwaggerConfig {
#Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
.apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.boot")))
.apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.cloud")))
.apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.data.rest.webmvc")))
.paths(PathSelectors.any()).build();
}}
Related
I want to use the #Slf4j annotation, so I imported this dependency in my pom.xml file
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.29</version>
</dependency>
but I have the error Cannot resolve symbol Slf4j
#Service
#Slf4j
#Transactional(readOnly = true)
public class PasswordResetTokenService {
..
}
The annotation #Slf4j is a Lombok annotation and is not present in the slf4j dependency.
If you want to use this annotation instead of declaring a logger field, you will need to add an extra dependency to Lombok:
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
<scope>provided</scope>
</dependency>
In case of Spring Boot, the parent POM might already specify the version. Then you don't need to declare the specific version anymore.
See:
https://projectlombok.org/features/log
https://projectlombok.org/api/lombok/extern/slf4j/Slf4j.html
I believe #Slf4j annotation is not actually coming from Slf4j but from Lombok. please look at this link that seems to provide a very good template to start from https://howtodoinjava.com/spring-boot2/logging/logging-with-lombok/
if you look at the excerpt of Application.java. the import for the annotation is coming from lombok
import lombok.extern.slf4j.Slf4j;
I have a Spring MVC project which does NOT USE spring-boot.
Im trying to use Jackson2ObjectMapperBuilderCustomizer in my appConfig class to configure a default date format.
I have these two dependencies in already but I'm still getting an error on the ObjectMapper? I am using spring version 4. All the examples for using the Jackson2ObjectMapperBuilderCustomizer are using spring boot which seems to not need need a dependency
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>`
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.9.8</version>
</dependency>`
The class is only part of Spring Boot dependencies. If you are determined to not use any Spring Boot dependencies, you will have to look for alternatives, otherwise you can use the following:
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-autoconfigure -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>1.2.2.RELEASE</version>
</dependency>
Add jackson-datatype-jsr310 dependency . its an alternative for non spring boot projects.
https://mvnrepository.com/artifact/com.fasterxml.jackson.datatype/jackson-datatype-jsr310/2.10.1
Starting with Spring Boot 2.0.2-RELEASE actuator 'metrics' endpoint isn't available even using following configuration:
management:
endpoints.web.exposure.include: "*"
The same configuration exposes metrics endpoint with Spring Boot 2.0.0-RELEASE
pom.xml:
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
...
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
</dependency>
....
Any ideas how to resolve this issue?
Finally I found that there is an instance of org.springframework.boot.actuate.metrics.MetricsEndpoint should exist in Spring context in order to let Actuator show '/metrics' endpoint.
org.springframework.boot.actuate.autoconfigure.metrics.MetricsEndpointAutoConfiguration
is responsible to create an instance of MetricsEndpoint but for some reason it never creates it.
So, I've created this bean in my own configuration:
#Bean
public MetricsEndpoint metricsEndpoint(MeterRegistry registry) {
return new MetricsEndpoint(registry);
}
It's fixed the problem but I'm not sure this this the best solution.
I made a similar sample . My application.yml is like this.
I create a springboot+mybatis+swagger project, I want to use swagger to export the project api, I did write swagger annotation in my code.
How Can I to export api as html or doc or chm?
To bring Swagger in, we need the following dependency declaration in our Maven POM
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>
Configuring Swagger 2 in the Application
#Configuration
#EnableSwagger2
public class SwaggerConfig {
#Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select() .apis(RequestHandlerSelectors.basePackage("com.springframework.example"))
.paths("/")
.build();
}
}
At this point, you should be able to test the configuration by starting the app and pointing your browser to http://localhost:8080/v2/api-docs
On pointing your browser to http://localhost:8080/swagger-ui.html, you will see the generated documentation rendered by Swagger UI.
Reference Document
I've got a Maven project which uses Java-configured Spring (#Configuration etc.). Properties which are referenced by #Value are stored in different places, e.g. Tomcat's context.xml.
For testing I've created a .properties file to provide some values for the components and services. In my JUnit test (which uses a spring test context) this .properties file is added via #PropertySource. The problem is that the values will not be loaded from the file, instead the value identifier is set as value, e.g. ${someFlag:false} (so I get ClassCastExceptions for any other than String). Also the default value will not be set, so I think, the values won't be processed at all.
I'm sure Spring finds this file because when I change the value of #PropertySource I get some FileNotFoundException. Nevertheless I've tried different variants to point to this file an all have worked (tested by renaming which produced FileNotFoundException):
classpath:/test.properties (my preferred notation)
/test.properties
file:src/test/resources/test.properties
I'm also sure that Spring itself works, because when I remove the #Value, the class under test is injected via #Autowired in my test as expected.
Down below you'll find the problem scenario stripped down as much as possible. For versions and dependencies please see the pom.xml at the bottom.
MyService.java
package my.package.service;
// Imports
#Service
public class MyService {
#Value("${someFlag:false}")
private Boolean someFlag;
public boolean hasFlag() {
return BooleanUtils.isTrue(someFlag);
}
}
MyConfiguration.java
#Configuration
#ComponentScan(basePackages = {"my.package.service"})
public class MyConfiguration {
}
MyServiceComponentTest.java
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = {MyTestConfiguration.class})
public class MyServiceComponentTest {
#Autowired
private MyService service;
#Test
public void hasFlagReturnsTrue() {
assertThat(service.hasFlag(), is(true));
}
}
MyTestConfiguration.java
#Configuration
#Import({MyConfiguration.class})
#PropertySource("classpath:/test.properties")
public class MyTestConfiguration {
}
src/test/resources/test.properties
someFlag=true
pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>3.2.3.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
The issue here is you need a PropertySourcesPlaceholderConfigurer also which is actually responsible for resolving the ${..} fields, just add another bean which creates this bean:
#Bean
public static PropertySourcesPlaceholderConfigurer propertiesResolver() {
return new PropertySourcesPlaceholderConfigurer();
}
With Spring 4, it's now possible to use TestPropertySource:
#TestPropertySource(value="classpath:/config/test.properties")
In order to load specific properties for a junit test
In addition to Biju Kunjummen answer:
If you use #ConfigurationProperties to inject properties into bean setters, then ConfigurationPropertiesBindingPostProcessor need to be created (instead of PropertySourcesPlaceholderConfigurer):
#Configuration
static class PropertyConfig {
#Bean
public static ConfigurationPropertiesBindingPostProcessor propertiesProcessor() {
return new ConfigurationPropertiesBindingPostProcessor();
}
}