Guice, Jetty, Jersey+Jackson BIG PLUS: Bean validation - java

I use guice, jetty, jersey+jackson in my stack to run a restful app. It works perfectly.
Then, I tried to add Jersey's Bean validation but I got no errors, no warnings... and no validation. I've read many articles, but non of these helped me out.
Here is my JerseyConfigModule:
public class JerseyConfigModule extends ServletModule {
#Override
protected void configureServlets() {
Map<String, String> initParams = new HashMap<String, String>();
initParams.put("com.sun.jersey.api.json.POJOMappingFeature", "true");
bind(GuiceContainer.class);
Set<Class<?>> classes=new ResourceConfig().property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE,true).getClasses();
for (Class<?> aClass : classes) {
bind(aClass);
}
serve("/rest/*").with(GuiceContainer.class, initParams);
}
}
My Jersey resource:
#Path("/user")
public class UserResource {
#POST
#Consumes(MediaType.APPLICATION_JSON)
public Response post(#Valid StoreUserDTO user){
}
}
In an another Guice module I bind this resource:
bind(UserResource.class);
The Bean used in parameter:
public class StoreUserDTO {
#NotNull
private String name;
#NotNull
private String email;
public String getName() {
return name;
}
public String getEmail() {
return email;
}
}
I use jersey-guice and glassfish's jersey-bean-validation (and I want this):
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-guice</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-bean-validation</artifactId>
<version>2.22.1</version>
</dependency>
What did I forget? Lots of examples I've found is not worked or wasn't for Jersey 2, just for 1.
Yes, I know Jersey supports the bean validation officially, but in the offical docs, I didn't find any info about how to integrate with Guice.

Related

Quarkus custom rest endpoints with the PanacheEntityResource

Im trying to work through a few tutorials of quarkus and got a problem with creating a simple REST endpoint. Im following this tutorial: https://quarkus.io/guides/rest-data-panache .
Im using the approach from the guide to create an interface that extends PanacheEntityResource<Entity, Id>
public interface ActorResource extends PanacheEntityResource<Actor, Long>{
}
The respective Entity is:
#Entity
public class Actor extends PanacheEntity{
public String first_name;
public String last_name;
public Timestamp last_update;
public static List<Actor> findByFirstName(String name) {
return list("first_name", name);
}
}
As in the guide, doing it like this auto-generates the basic rest endpoints for getById, getAll, create, update and delete. As you can see in my Entity class I have a findByFirstName method which gets all Entities, which match the given method parameter "name" . Now I want to expose a REST endpoint for this method. Ive so far found a way to implement this, but that doesnt seem quite right. Ive had no luck with implementing the REST endpoint for the method directly into the interface
#ResourceProperties(path = "actors")
public interface ActorResource extends PanacheEntityResource<Actor, Long>{
#GET
#Produces("application/json")
#Path("/first_name={name}")
public static List<Actor> getByFirstName(#PathParam("name") String name) {
return Actor.findByFirstName(name);
}
}
No errors with this implementation, but the REST endpoint isnt exposed.
Now as I said, Ive found a way to do this and this is to create an interface as shown in the first code bracket with nothing in it and in addition to that a ResourceClass in which I implement my custom endpoint. To reduce the clutter (in the IDE) Ive combined this so the interface is created inside my ResourceClass like this (example is for another entity):
#Path("/countries")
#ApplicationScoped
#Produces("application/json")
public class CountryResource {
#GET
#Path("/name={name}")
public List<Country> getByName(#PathParam("name") String name) {
return Country.findByName(name);
}
#ResourceProperties(path = "/countries")
public interface CountryResourceTest extends PanacheEntityResource<Country, Long>{
}
}
This works, by creating the interface the basic rest endpoints are auto-generated and inside the resource class I can add other endpoints, but it just feels like this is not the right approach. Am I wrong, and this is just how Im supposed to do this, or is there a way to implement this with only the interface approach that was originally used in the guide?
I think adding custom endpoints straight to generated resource classes isnt supported yet because this is still an experimental feature for evaluation only. Feel free to give feedback as issues in quarkus' GitHub issue tracker.
I think the best way to add custom endpoints is by creating a second resource class with the same path. Then you can define your custom endpoints there. If you have no name collisions that will work. I tested it with the hibernate-orm-panache-quickstart with following code changes:
My Entity with my custom query:
#Entity
#Cacheable
public class Fruit extends PanacheEntity {
#Column(length = 40, unique = true)
public String name;
public Fruit() {
}
public Fruit(String name) {
this.name = name;
}
public static Fruit findByName(String name) {
return find("name=?1", name).firstResult();
}
}
Resource for generated rest-api (remoe the given resource in this tutorial):
public interface FruitResource extends PanacheEntityResource<Fruit, Long> {
}
My specific resource:
#Path("fruit")
#ApplicationScoped
#Produces("application/json")
#Consumes("application/json")
public class FruitSpecificResource {
#GET
#Path("/first_name={name}")
public Fruit getByName(#PathParam("name") String name) {
return Fruit.findByName(name);
}
}
I also added openapi and replaced postgres with h2 for testing purposes. Here are my dependencies:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-orm-panache</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jsonb</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-h2</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-openapi</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-orm-rest-data-panache</artifactId>
</dependency>
And i changed the application.properties to this:
quarkus.datasource.db-kind=h2
quarkus.datasource.jdbc.url=jdbc:h2:mem:test
quarkus.datasource.jdbc.max-size=8
quarkus.datasource.jdbc.min-size=2
quarkus.hibernate-orm.database.generation=drop-and-create
quarkus.hibernate-orm.log.sql=true
quarkus.hibernate-orm.sql-load-script=import.sql
Ok, now i can start quarkus:dev and there will be a swagger-ui under localhost:8080/swagger-ui
The new endpoint is there. I tested it manually. It works.

returning all result using spring-data-rest

I am following spring data rest from https://spring.io/guides/gs/accessing-data-rest/ and I am only using
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
I would like to know how can I return all records (without pagination) but not using spring-boot-starter-web.I wants to keep my code as small as possible.
I tried following but it is not working
#RepositoryRestResource(collectionResourceRel = "people" , path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
List<Person> findAllByLastName(#Param("name") String name);
default List<Person> findAll(){
Pageable pageable = null;
return (List<Person>) this.findAll(pageable);
};
}
I mean if I have whole MVC, I can do it but I like to keep my code to minimum.
Spring Data REST is itself a Spring MVC application and is designed in
such a way that it should integrate with your existing Spring MVC
applications with little effort. An existing (or future) layer of
services can run alongside Spring Data REST with only minor additional
work.
If you are using current version of spring boot, there is no need to mark your repository with #RepositoryRestResource; also spring will auto-configure Spring Data Rest once it found the spring-data-rest dependency in your path, bellow you will find steps with minimum config :
In pom.xml :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
Define your Entity + Repository :
Order.java
#Entity(name = "SampleOrder")
#Data
public class Order {
#Id #GeneratedValue//
private Long id;
private String name;
}
OrderRepository.java
public interface OrderRepository extends CrudRepository<Order, Long> {
}
Application.java
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Test your API :
curl http://localhost:8080
< HTTP/1.1 200 OK
< Content-Type: application/hal+json
{ "_links" : {
"orders" : {
"href" : "http://localhost:8080/orders"
}
}
}
As #abdelghani-roussi shows, you can use the CrudRepository instead of the PagingAndSortingRepository, e.g.:
public interface PersonRepository extends CrudRepository<Person, Long> {
List<Person> findAllByLastName(#Param("name") String name);
// don't need to define findAll(), it's defined by CrudRepository
}
and then the default findAll() method will return a List<Person> that isn't paged.
Note: as I mentioned in my comment, by including the dependency on spring-boot-starter-data-rest you are also pulling in the Web dependencies, so you can't avoid that.

Rest service won't work when using CDI: Service stays null

I am making a rest service application with JAX-RS. Its for some project for school. For this project I need to use follow techniques:
• Maven
• JAX-RS
• CDI
• JPA - EJB
• JNDI
• Bean Validation
So now I already maded my domain "Cafes" with a Fake DB ("CafeStub") and a real DB using JPA ("CafeDB"). My domain also makes a little usage of CDI. (#Inject in the CafeService class ...)
Non I wanted to create my rest service, using JAX-RS. This worked fine:
My problem is when I try to use CDI again it fails and it gives an 500 exception, NullPointerException, "Severe: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container"
Full Stacktrace:
I don't know how to fix this, already searched a long time .. Hopefully somebody can help me :s
This is my "CafeController" class. Producing the rest service
Path("/cafes")
public class CafeController {
#Inject
private CafeFacade cafeFacade;
public CafeController() {
//this.cafeFacade = new CafeService();
}
#GET
#Produces("application/json")
public Response getCafes(){
try{
// test ........
ObjectMapper mapper = new ObjectMapper();
Cafe cafe = cafeFacade.getCafe(new Long(1));
String jsonInString = mapper.writeValueAsString(cafe);
return Response.status(200).entity(jsonInString).build();
}catch (JsonProcessingException jsonEx) {
System.out.println("Json Exception");
System.out.println(jsonEx.getMessage());
return null;
}
}
This one is the "CafeService" class, the one who implemented "CafeFacade"
public class CafeService implements CafeFacade {
#Inject
private CafeRepository cafeRepository;
public CafeService() {
//cafeRepository = new CafeStub();
//cafeRepository = new CafeDB("CafesPU");
}
#Override
public long addCafe(Cafe cafe) {
return this.cafeRepository.addCafe(cafe);
}
#Override
public Cafe getCafe(long cafeID) {
return this.cafeRepository.getCafe(cafeID);
}
Her you see the "CafeStub" class, the one who implemented "CafeRepository"
public class CafeStub implements CafeRepository {
private static Map<Long, Cafe> cafes;
private static long counter = 0;
public CafeStub() {
cafes = new HashMap<Long, Cafe>();
// adding some dara
this.addSomeData();
}
#Override
public long addCafe(Cafe cafe) {
if(cafe == null){
throw new DBException("No cafe given");
}
counter++;
cafe.setCafeID(counter);
cafes.put(cafe.getCafeID(), cafe);
return cafe.getCafeID();
}
#Override
public Cafe getCafe(long cafeID) {
if(cafeID < 0){
throw new DBException("No correct cafeID given");
}
if(!cafes.containsKey(cafeID)){
throw new DBException("No cafe was found");
}
return cafes.get(cafeID);
}
At least here you can see my pom.xml (dependencies from CafeService project) - web.xml (from CafeService project) and project structure ...
<dependencies>
<dependency>
<groupId>Cafes</groupId>
<artifactId>Cafes</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.3</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.19.4</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.19.4</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.19.4</version>
</dependency>
</dependencies>
Thanks in advance ...
Cheers
Tom
A class annotated with just #Path does not mark the class as a CDI bean as it is not in the list of bean defining annotations in the CDI spec. Adding RequestScoped to the REST service marks it as a CDI bean so injection works as you've discovered.
This answer here lists the annotations which mark a class as a CDI bean.
Is #javax.annotation.ManagedBean a CDI bean defining annotation?
Solved .. RequestScoped did the trick.. Daimn searched so long for one annotation.
#RequestScoped
#Path("/cafes")
public class CafeController {
Still I don't understand why I need to use it.
#RequestScoped : CDI instantiates and manages the bean
-> I thought my bean.xml would have instantiates and manages the bean ?

jackson - jackson not creating json for nested objects

I have a user list class and an API that returns user list and total records.
The class is as follows :
#JsonInclude(JsonInclude.Include.NON_NULL)
public class FMSResponseInfo {
#JsonProperty("status")
private String status;
#JsonProperty("message")
private String message;
#JsonProperty("data")
private Object data;
#JsonProperty("status")
public String getStatus() {
return status;
}
#JsonProperty("status")
public void setStatus(String status) {
this.status = status;
}
#JsonProperty("message")
public String getMessage() {
return message;
}
#JsonProperty("message")
public void setMessage(String message) {
this.message = message;
}
#JsonProperty("data")
public Object getData() {
return data;
}
#JsonProperty("data")
public void setData(Object data) {
this.data = data;
}
}
#JsonInclude(JsonInclude.Include.NON_NULL)
public class UserListResDTO {
#JsonProperty("users")
private List<UserDTO> users;
#JsonProperty("totalRecords")
private long totalRecords;
public List<UserDTO> getUsers() {
return users;
}
public void setUsers(List<UserDTO> users) {
this.users = users;
}
public long getTotalRecords() {
return totalRecords;
}
public void setTotalRecords(long totalRecords) {
this.totalRecords = totalRecords;
}
}
I am setting an object of type UserListResDTO in FMSResponseInfo as shown below.
I have been successful in creating web services and returning response as json, so far. But the problem I am facing is that the API returns the response as follows :
{"data":"org.package.UserListResDTO#70783307","message":"Success","status":"200"}
And this is how I have written the web service :
#Path("/getUsers")
#GET
#Produces(MediaType.APPLICATION_JSON)
public FMSResponseInfo getUsers(#QueryParam("page") #DefaultValue("0") int page) {
System.out.println("In getUsers()");
FMSResponseInfo fmsResponseInfo = new FMSResponseInfo();
try {
UserListResDTO userList = fmsUserManager.getAllUsers(page);
fmsResponseInfo.setData(userList);
fmsResponseInfo.setStatus(FMSErrorMessageEnum.SUCCESS_CODE.getValue());
fmsResponseInfo.setMessage(FMSErrorMessageEnum.SUCCESS_MESSAGE.getValue());
} catch (Exception e) {
return FMSUtil.getErrorResponseInfo(FMSErrorMessageEnum.INTERNAL_SERVER_ERROR_CODE.getValue(),
e.getMessage());
}
System.out.println("Out getUsers()");
return fmsResponseInfo;
}
I guess there is some problem with the dependencies or something that I am unable to resolve. Major dependencies in my pom are :
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
and I am creating Web services by extending Application class as follow :
#ApplicationPath("rest")
public class FMSApplication extends Application {
public Set<Class<?>> getClasses(){
Set<Class<?>> set = new HashSet<Class<?>>();
set.add(FMSUserManagerWebService.class);
set.add(FMSDocumentManagerWebService.class);
set.add(FMSInboxManagerWebService.class);
set.add(FMSLocationManagerWebService.class);
return set;
}
}
Any help will be really appreciated as I am new to this REST web services and have been stuck for quite long.
This link will explain the answer
https://jersey.java.net/documentation/latest/media.html#d0e7963
9.1.1.1. POJO support
POJO support represents the easiest way to convert your Java Objects
to JSON and back.
Media modules that support this approach are MOXy and Jackson
The link to Jackson includes
9.1.4.1. Dependency
To use Jackson 2.x as your JSON provider you need to add
jersey-media-json-jackson module to your pom.xml file
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.22.2</version>
</dependency>
I don't know much about moxy but you had jackson on your CLASSPATH and were using Jackson annonations. Jersey however was configured to use moxy.
From link
JSON binding support via MOXy is a default and preferred way of
supporting JSON binding in your Jersey applications since Jersey 2.0.
When JSON MOXy module is on the class-path, Jersey will automatically
discover the module and seamlessly enable JSON binding support via
MOXy in your applications.
MOXy seemed to have handled FMSResponseInfo. Why it didn't handle the other object I do not know. But since you wanted to use Jackson you should have been using the Jackson module.
As suggested by Shire Resident in the comments using the following dependency I was able to resolve the problem :
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.22.2</version>
</dependency>

Jersey + Jetty + JSON

I would like to produce an JSON using Jetty + Jersey.
My POM.XML is similar to this post: How do I update example to work with latest versions Jetty (9.1.0.RC2) and Jersey (2.7)?. I imagine that i am missing some dependecy. The result from inferFile() is returning blank.
I can see that the method toStirng from Student class was not been called.
Maven
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.1.3.v20140225</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.1.3.v20140225</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.14</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.14</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-http</artifactId>
<version>2.14</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.14</version>
</dependency>
Java
public class Student {
public Student(){
}
#Override
public String toString(){
return new StringBuffer(" First Name : ").append("MY FIRST NAME").toString();
}
}
#Path("/bulkload")
public class BulkLoadAPI {
#POST
#Path("inference")
#Consumes(MediaType.TEXT_PLAIN)
#Produces(MediaType.APPLICATION_JSON)
public Student inferFile() throws URISyntaxException, IOException {
Student s = new Student();
return s;
}
}
public static void main(String[] args) throws Exception {
ServletHolder jerseyServlet = new ServletHolder(ServletContainer.class);
jerseyServlet.setInitParameter("jersey.config.server.provider.classnames", "service.api.BulkLoadAPI");
jerseyServlet.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", "true");
Server server = new Server(10500);
ServletContextHandler context = new ServletContextHandler (server, "/", Servl etContextHandler.SESSIONS);
context.addServlet(jerseyServlet, "/*");
server.start();
server.join();
}
I'm not really sure what you're expecting. If you're expecting toString() to be called (which it isn't), that wouldn't even produce valid JSON. The way POJO to JSON (and vice versa) conversion is done is through MessageBodyReaders and MessageBodyWriters. Basically they are looking for fields with either some form of annotation known to the marshaller/unmarshaller, or Java bean style getters and setters. That's how the data/properties for the JSON will be discovered.
For example, if your Student looked like
public class Student {
private String firstName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
it would give you JSON like {"firstName":"MyFirstName"}. You just need to set the property
public Student inferFile() throws URISyntaxException, IOException {
Student s = new Student();
s.setFirstName("MyFirstNaem");
return s;
}
Another thing, this is not needed
setInitParameter("com.sun.jersey.api.json.POJOMappingFeature","true");
That is a Jersey 1 feature. You are using Jersey 2. See also
jersey.config.server.provider.packages
So you don't have to configure each class individually. The value should be a package. It will scan the package and sub packages for annotated classes.

Categories