CDI Input Validation with JAX-RS and Java EE 8

A CDI bean with parameters validated by Bean Validation:


import javax.validation.constraints.Size;
public class WorkshopCatalog {

    public void save(@Size(min = 2, max = 5) String input) {
        //...
    }
}

and exposed with a JAX-RS resource:

@Path("workshops")
public class WorkshopsResource {

    @Inject
    WorkshopCatalog facade;

    @POST
    public void save(String input) {
        this.facade.save(input);
    }
}    

throws on every violation an instance of ValidationException which is automatically converted by JAX-RS into 400 Bad Request.

An invalid request with too short payload, like e.g.:

curl -i -XPOST -d'd' http://localhost:8080/beanvalidation/resources/workshops

leads to:


HTTP/1.1 400 Bad Request
Server: Payara Server  5.184 #badassfish
(...)

See you at Web, MicroProfile and Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.


Cloudy Jakarta EE and MicroProfile: Microservices, Clouds and Beyond Jakarta EE / MicroProfile airhacks workshops at MUC airport, Winter Edition

airhacks.fm the podcast:

Stay in touch: airhacks.news.

Comments:

Hi,

I tried the code and it works marvelous. I just have a little question.

How do I retrieve the messages from the validations constraints? The service just responds with a generic 400 error page.

I want to get the validations messages in the Response object.

I got this:

// -----------------------------------------

package test_validations;

import javax.validation.Valid;
import javax.validation.constraints.Size;

public class Facade {
public void save (@Size(min = 2, max = 5, message = "Wrong Length") String input) {}
public void save (@Valid ValidObject vo) {}
}

// -----------------------------------------

package test_validations;

import javax.inject.Inject;
import javax.validation.Valid;
import javax.validation.constraints.Size;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

@Path("test")
public class Service {

@Inject
Facade facade;

@POST
@Path("facade")
public Response onFacade (String input) {
this.facade.save(input);
return Response.ok(input).build();
}

@POST
@Path("service")
public Response onService (@Size(min = 2, max = 5, message = "Wrong Length")String input) {
return Response.ok(input).build();
}

@POST
@Path("obj/facade")
public Response objFacade (ValidObject input) {
this.facade.save(input);
return Response.ok(input).build();
}

@POST
@Path("obj/service")
public Response objService (@Valid ValidObject input) {
return Response.ok(input).build();
}

}

// -----------------------------------------

package test_validations;

import javax.validation.constraints.NotBlank;
import lombok.Data;

@Data
public class ValidObject {
@NotBlank(message = "Is Blank")
private String string;
}

Posted by Ralph on December 28, 2018 at 01:24 AM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
realworldpatterns.com
...the last 150 posts
...the last 10 comments
License