I'm using Swagger with Microprofile 4.1 and OpenAPI 3.0 to document my Jakarta REST api.
I have a simple Entity class and I want to add the "example" property to the fields of the Schema.
Here my pom file:
<dependency>
<groupId>org.eclipse.microprofile</groupId>
<artifactId>microprofile</artifactId>
<version>4.1</version>
<type>pom</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.microprofile.openapi</groupId>
<artifactId>microprofile-openapi-api</artifactId>
<version>3.0</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.microprofile-ext.openapi-ext</groupId>
<artifactId>openapi-ui</artifactId>
<version>2.0.0</version>
</dependency>
Here the code for the id field:
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Basic(optional = false)
#Column(name = "id")
#Schema(example="123")
private Integer id;
I obtain the following result:
Why I see the others properties? I want to see only the type and the example
Related
Adding #NotBlank annotation to member variables in DTO does not take effect. What is the reason? Please help me
first check this dependency is present in the pom.xml file. If it doesn't present then add.
With dependency present in your project now use can use #NotBlack and #Valid annotation.
<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>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
For more information about Validation in Spring Boot check this link
Apart from including spring-boot-starter-validation in dependency, you must include #Valid annotation on #RequestBody in the controller for #NotBlank annotation to apply on member variables in DTO .
I have a Java 1.7 starts application. It does have some RESTful api's.
I would like to add #JsonIgnore to a filed so that it is not returned in the api.
E.g.
Member.java
import com.fasterxml.jackson.annotation.JsonIgnore;
private java.lang.String username;
#JsonIgnore
private java.lang.String password;
Does not ignore the password.
"member": {
"password": "**************",
"username": "richard"
}
I think the reason why #JsonIgnore does not work, is because I use com.fasterxml.jackson.annotation.JsonIgnore. Should I use a different annotation from a different library? i.e. Is my implementation of jaxrs maybe not com.fasterxml.jackson? How do I tell?
The IntelliJ classpath has:
(I have tried net.minidev.json.annotate.JsonIgnore with no success)
More info:
pom.xml
<dependency>
<groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId>
<version>2.3</version>
</dependency>
and
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.11.2</version>
</dependency>
Is this the problem? two versions! I am not sure where the 2.0.5 version comes from, as it is not defined in the pom.
You don't need a #JsonIgnore in this case. You can simply omit the variable that you don't want deserialized(the password in this case) and jackson will just return you the username.
I'm trying to integrate Hibernate Search in a Spring Boot 2 app. I have tried different versions following the compatibility list on their site but I always get this error when the app is started and "fullTextEntityManager.createIndexer().startAndWait();" is executed:
Exception in thread "main" java.lang.IllegalArgumentException: HSEARCH000349: Some of the specified entity types ('class java.lang.Object') are not indexed, nor is any of their subclasses.
After 3 days googling and trying lot of things I have no idea what else I can do.
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
...
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>${opencsv.version}</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search-orm</artifactId>
<version>5.10.6.Final</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-spatial</artifactId>
<version>5.5.5</version>
</dependency>
</dependencies>
As the datasource configuration is handled by spring boot I don't have a persistence.xml file so I put the Hibernate Search properties in application.properties. Actually I put these properties also in hibernate.properties trying to make this work.
application.properties
...
spring.datasource.username=*******
spring.datasource.password=*******
spring.jpa.database=MYSQL
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=false
spring.jpa.hibernate.naming.physicalstrategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.hibernate.search.default.directory_provider=filesystem
spring.jpa.hibernate.search.default.indexBase=/lucene/indexes
hibernate.properties
hibernate.search.default.directory_provider=filesystem
hibernate.search.default.indexBase=/lucene/indexes
My entity Restaurant to be indexed:
#Data
#Builder
#NoArgsConstructor
#AllArgsConstructor
#EqualsAndHashCode
#Indexed
#Spatial
#Entity
#Table(name = "restaurant")
public class Restaurant {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#Field(index = Index.YES, analyze = Analyze.NO, store = Store.YES)
private String name;
private String address;
#Latitude
private Double latitude;
#Longitude
private Double longitude;
}
I'm initializing Hibernate Search just after spring boot is started
#SpringBootApplication
public class PlacesApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context =
SpringApplication.run(PlacesApplication.class, args);
context.getBean(HibernateSearchService.class)
.initializeHibernateSearch();
}
And my Hibernate Search Service looks like:
#AllArgsConstructor
#Service
#Slf4j
public class HibernateSearchService {
private EntityManager entityManager;
#Transactional
public void initializeHibernateSearch() {
try {
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
fullTextEntityManager.createIndexer().startAndWait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
I found the issue, very very silly issue....
My IDE just imported the wrong Indexed annotation
It set import org.springframework.stereotype.Indexed; instead of import org.hibernate.search.annotations.Indexed;
I am trying to integration Spring 4.2.3 with hibernate validator v.5.2.2.Final to validate input JSON data expose with REST Controller.
I don't see any compile time or running time exception but at the same time it does not validate input data.
Pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.3.RELEASE</version>
<scope>compile</scope>
</dependency>
<!-- jsr303 validation dependencies -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.2.Final</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator-cdi</artifactId>
<version>5.2.2.Final</version>
</dependency>
Pojo java class:
public class RequestVO {
#NotEmpty
private String testValidation;
#NotNull
private String testNull;
#NotBlank
private String testBlank;
.... getters and setters .....
}
#Controller
#RequestMapping("/login")
public class LoginController {
#RequestMapping(method=RequestMethod.POST, value="/user")
public #ResponseBody ResponseVO getLoginResponse(#Valid #RequestBody RequestVO request, Errors error) {
ResponseVO response = new ResponseVO ();
if (error.hasErrors()) {
System.err.println("Success");
}
return response;
}
}
In your dependency list there's a comment saying ...jsr303 validation dependencies.... Note the bean validation has an updated specification (JSR-349), and this can very well be your issue.
You should align your dependencies in the following manner
hibernate-validator-5.x.x
validation-api-1.1.x
which implement JSR-349
OR
hibernate-validator-4.x.x
validation-api-1.0.x.
which implements JSR-303
If you mix the dependencies, the validation will simply not kick-in. As a solution suggestion, add
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
and make sure that you remove any dependency to validation-api-1.0 jar
I think is interest to take a look at this link
and if you to remove the validation just remove #Valid from method and you're ok.
I decided to play with hibernate using annotations this weekend and have encountered an interesting issue which I'm hoping you all can help with. The problem I am encountering is that the schema created is not correct when I attempt to use a ManyToOne relationship.
I have two classes of note. A User class and a CourseTaken class. The user can have any number of courses they have taken. Code is below.
#Entity
#Table(name = "course_taken")
public class CourseTaken {
#Id
#GeneratedValue
#Column(name = "course_id", columnDefinition = "INT NOT NULL AUTO_INCREMENT")
private int id;
#ManyToOne
#JoinColumn(name = "user_id")
private User user;
...getters/setters
}
#Entity
#Table(name = "user")
public class User {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "user_id", columnDefinition = "INT NOT NULL AUTO_INCREMENT")
private int id;
#OneToMany(mappedBy = "user")
private Set<CourseTaken> coursesTaken = new HashSet<CourseTaken>();
...getters/setters
}
When I have hibernate generate the schema, here is the creation statement for the course_taken table:
create table course_taken (course_id INT NOT NULL AUTO_INCREMENT not null, courseID integer not null, user_id INT NOT NULL AUTO_INCREMENT, primary key (course_id))
Hibernate seems very confused by my attempt to use the #OneToMany annotation. Any ideas?
I have played with various configurations:
org.hibernate.dialect.
org.hibernate.dialect.MySQL5Dialect
org.hibernate.dialect.MySQLDialect
org.hibernate.dialect.MySQL5
I'm using maven, here are my hibernate pom dependencies:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.2.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-osgi</artifactId>
<version>5.0.2.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>5.0.2.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>5.0.2.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-proxool</artifactId>
<version>5.0.2.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-infinispan</artifactId>
<version>5.0.2.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.0.2.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
</dependency>
It looks like I must have had my dialect a little incorrect. The following seemed to fix the issue
org.hibernate.dialect.MySQLDialect