I am trying to configure OpenAPI with swagger to my existing non-spring boot application.
Spring v.5.1.8.RELEASE
SpringDoc v.1.4.6
Spring-boot v.2.3.2.RELEASE
Open doc and swagger with spring mv
My application is not spring boot enable that’s why I have following dependencies in my Pom copied from article linked above
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>last.version</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.1.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.1.11.RELEASE</version>
</dependency>
My Spring mvc project already has servlet context configuration so I assumed I only need to register/import open api and swagger configuration files.
So I did the following
#EnableMvc
#Configuration
#import({org.springdoc.ui.SwaggerConfig.class,
org.springdoc.core.SwaggerUiConfigProperties.class, org.springdoc.core.SwaggerUiOAuthProperties.class,
org.springdoc.webmvc.core.SpringDocWebMvcConfiguration.class,
org.springdoc.webmvc.core.MultipleOpenApiSupportConfiguration.class,
org.springdoc.core.SpringDocConfiguration.class, org.springdoc.core.SpringDocConfigProperties.class,
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration.class
#Bean
Public OpenAPI(){
return new OpenAPI();
}
When I run my application I gets the following error
“Java.lang.NoSuchMethodError:org.springframework.core.type.AnnotatgedTypeMetadata.getAnnotations()/Lorg/springframework/core/annotations/MergeAnnotations”
It was caused due to incompatible version of spring mvc and spring-boot
Related
I am new at spring framework, I want to connect my MySQL database which located in localhost with spring boot application.
I'm listing out the minimal configuration:
In pom.xml
<!-- JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- Use MySQL Connector-J -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
In application.properties
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
You can get such examples everywhere. Prefer to read https://spring.io/guides/gs/accessing-data-mysql/
Source : https://spring.io
Checkout other questions too :
How to use Spring Boot with MySQL database and JPA?
Spring Boot MYSQL connection
Spring boot - MySQL settings are not working
You need to add database configuration properties for Mysql in your application.properties:
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=mysql
spring.datasource.password=mysql
spring.jpa.hibernate.ddl-auto=create this configuration will create tables based on your entities which were created by you.
spring.datasource.url=jdbc:mysql://localhost:3306/db_example this configuration connects through jdbc to your database called db_example. You need to create that database.
spring.datasource.username=mysql this configuration you need to put your user who will connect via jdbc to your mysql database.
spring.datasource.password=mysql this configuration you need to provide your password which is represented by your user in order to connect through jdbc to your mysql database.
In addition tou you need to add dependencies for Mysql and JPA.
If you are using maven, add the dependencies in your pom.xml:
<dependencies>
<!-- JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- Use MySQL Connector-J -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
If you are using gradle, add the dependencies in build.gradle:
dependencies {
// JPA Data (We are going to use Repositories, Entities, Hibernate, etc...)
compile 'org.springframework.boot:spring-boot-starter-data-jpa'
// Use MySQL Connector-J
compile 'mysql:mysql-connector-java'
}
Add below dependencies in pom.xml.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
Add below database properties in application.properties.
spring.datasource.url=jdbc:mysql://localhost:3306/dbname
spring.datasource.username=root
spring.datasource.password=root
For more details please refer this link
To connect with mysql need some dependencies as below
Dependencies (maven -> pom.xml).
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
in application.properties need some extra configuration setting which must like like
application.properties (src/main/resources/application.properties)
# DataSource settings: set here your own configurations for the database
# connection. In this example we have "netgloo_blog" as database name and
# "root" as username and password.
spring.datasource.url = jdbc:mysql://localhost:8889/database_name
spring.datasource.username = mysql-userId
spring.datasource.password = mysql-pwd
# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy (Not necessary to add but you can use this too)
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager)
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
Now your EntityManager Object will be constructed when you starts your spring boot Configuration.
Above defined configuration is enough to connect your local my-sql database but here is some more configuration that you need to make your code more readable.
Enable JPARepositories such that CRUD Repositories must defined in a defined package.
Add #EnableJPARepositories("basepackage.*") to your SpringBootMainApplicationClass like this..
#SpringBootApplication
#EnableJPARepositories("com.demo.application.*.repositories")
#ComponentScan("com.demo.application.*")
public class SpringBootMainApplicationClass {
public static void main(String[] args) {
SpringApplication.run(SpringBootMainApplicationClass.class, args);
}
}
by adding #EnableJPARepositories Annotation in MainClass enables user to make your code more readable and EntityManager Object only restricted to only defined package.
application.properties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/jpa
spring.datasource.username = root
spring.datasource.password =
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
build.gradle
implementation 'mysql:mysql-connector-java:8.0.15'
How I get the CXF version used on the follwowing maven dependency
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-cxf</artifactId>
<version>2.16.5</version>
</dependency>
I got it using maven dependences by click on the camel-cxf dependency in my project to open the camel-cxf pom then switched to 'Effective POM'. There you can search for cxf-core dependency and get the version.
I have a Spring REST project configured with hsqldb.
I would like to change it to MySQL.
I have MySQL server installed and running, but I don't know how to modify this pom:
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
PS.: Project im talking about comes as a source code for 'Spring REST' book:
http://www.apress.com/9781484208243
source code download link:
http://www.apress.com/downloadable/download/sample/sample_id/1704/
As far I see you are using Spring Boot on this, so you can easy change the databases changing the drivers dependencies from:
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
To
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
The driver version will be version on parent pom.
Then specify the parameters on properties
spring.datasource.url=jdbc:mysql://localhost:port/yourdb
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver # we can ommit this if we want, Spring Boot will deduce based on the classpath
For more configuration on databases you can see the properties available on appendix here
I am using Maven and Jersey on Tomcat7 to build a web server, but I keep getting a 415 response. My request is made using Postman and Advanced Rest Client
My stubbed method:
#POST
#Path("/createuser")
#Consumes(MediaType.APPLICATION_JSON)
public Response createUser(UserInformation user){
return Response.ok().build();
}
Custom class:
package efile.models;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
#JsonInclude(Include.NON_NULL)
public class UserInformation {
private String id;
private String username;
private String password;
private String firstName;
private String lastName;
private String emailAddress;
/* getters and setters */
}
Request:
{myhost}/createuser
Headers:
accept: application/json
content-type: application/json
Request body:
{
"id":"1234567",
"userName":"qwer",
"password":"zxcv",
"firstName":"jasdfme",
"lastName":"qwetad",
"emailAddress": "qwet#gf4elk.com"
}
My dependencies:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.25</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.17</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.17</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.17</version>
</dependency>
<dependency>
<groupId>com.dropbox.core</groupId>
<artifactId>dropbox-core-sdk</artifactId>
<version>1.7.6</version>
</dependency>
<dependency>
<groupId>org.jvnet.mimepull</groupId>
<artifactId>mimepull</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>1.17</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.2.0</version>
</dependency>
Thanks.
On my side it was because I forgot to add
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.18</version>
</dependency>
after
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.18</version>
</dependency>
No warnings/errors at compilation but 415 Unsuported Media Type at runtime trying to consum JSON on Jersey 2.18 running on AppEngine
Hope it helps
If you are sending the 'Accept' header, you may also need to add the #Produces(MediaType.APPLICATION_JSON) annotation to your method, or, remove the Accept header from the request if you are not expecting a response body.
Edit:
Ok... After a bit of experimentation, I believe I have been able to reproduce your problem and find a solution.
I see from your pom file that you are using Jersey 1.17. I reproduced your problem using Jersey 1.18, but I suspect it is equally valid for 1.17. It appears that Jersey does not come with Jackson support built in, but Jackson must be installed as a provider. I've always used it with containers like Glassfish where the providers come pre-configured.
I was not able to make Jersey 1.18 work with the latest Jackson 2 libraries (under the com.fasterxml.jackson groupId) that you were pulling in with your pom.xml, but I was able to get it working with the older Jackson libraries when the groupId was still org.codehaus.jackson. It appears that Jersey 1.18 uses a class called com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy which looks specifically for a Jackson implementation in org.codehaus.jackson... So you might not be able to make the newer versions of Jackson work without upgrading to a newer version of Jersey.
Firstly, I added the XmlRootElement annotation to the UserInformation class. Jax-RS will serialize and deserialize JAXB objects and this annotation marks this class as a JaxB object. This required an additional dependency in the pom on the jaxb-api:
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.1</version>
</dependency>
Next, I removed all of the Jersey dependencies from your pom.xml file, and added a dependency on the jersey-server to give access to the necessary JaxRS annotations:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.18</version>
<scope>provided</scope>
</dependency>
I then took a clean installation of Tomcat 7.0.52, and added the following jars to the CATALINA_HOME/lib directory:
asm-3.3.1.jar (Byte code manipulation used by Jackson)
jacksore-core-asl-1.9.13.jar (Includes the Jackson JSON Provider)
jersey-bundle-1.18.jar (The Jersey Implementation)
I found it necessary to add these jars to the Tomcat lib folder rather than include them in the project WAR file. I don't work with Tomcat much, so I don't recall the rules about exactly when it is necessary to place jars in the Tomcat lib directory vs. including them in the WAR file.
Following all of those steps I was able to go from getting the 415 Media Unsupported error to a 200 OK on the POST.
I hope this helps.
I was able to resolve the issue after modifying the dependencies in the pom.xml. I updated the jersey dependencies to 1.18.1 and added the fasterxml json provider dependency:
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.2.1</version>
</dependency>
My guess is that it was just the json-provider dependency that resolved the issue.
I have Jax-rs endpoint deployed in WAR archive on JBoss 7.1.1.
In its JSON response I don't want my null field name to be included, so I put #JsonSerialize on it.
class MyResponse {
private Long id;
#JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
private String name;
private List<String> addresses;
// getters and setters
}
My pom.xml has the following
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>2.3.2.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>2.3.2.Final</version>
<scope>provided</scope>
</dependency>
When the scope for resteasy-jackson-provider is set to provided it ignores the annotation and returns null in JSON response. However when I remove the scope from maven dependency - it works.
From the page here https://docs.jboss.org/author/display/AS71/Implicit+module+dependencies+for+deployments it looks like JBoss should autoload this module if Jax-RS deployment found.
Now I don't know if this is a bug and if I should really include this dependency (NOT keeping it provided). Or maybe I'm doing something wrong there?
You need to make sure to create a JBoss Deployment Structure descriptor.
Since this is a Maven project I assume it would be under src/main/webapp/WEB-INF/jboss-deployment-structure.xml
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.0">
<deployment>
<dependencies>
<module name="org.codehaus.jackson.jackson-core-asl" />
<module name="org.codehaus.jackson.jackson-mapper-asl" />
</dependencies>
</deployment>
</jboss-deployment-structure>
This will allow the built-in support for RESTEasy and Jackson to work correctly in JBoss 7.1.x or JBoss EAP 6.x. Without this descriptor RESTEasy will use the Jettison provider.