Search code examples
javaspringjerseyjax-rs

How to check body at jax-rs


I'm making and API Rest back end, to be complemented with angular later, i have been thinking if use Jersey or Spring Web, i went for Jersey.

My actual Profesor.class has 3 parameters; name, age and subject. But when i'm sending the next JSON with Postman:

 {
    "name":"bob",
    "age":"21",
    "asd":"qwe"
}

It doesn't give error, is working and adding the professor with null subject at MySQL, i want the Response from jersey to told me something like "wrong model" or some like that, and to not hit the DB. This is the answer im getting:

{
    "name": "bob",
    "age": 21,
    "subject": null
}

Any idea? This is how my controller is actually working (I'm using Spring Boot)

@Path("/create")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class CreaterController {

    @Autowired
    CreateService createService;

    @POST
    @Path("/profesor")
    public Response createProfesor(Profesor profesor) {
        return createService.crearProfesor(profesor);
        }
    ....

And this is my CreateServiceImpl

@Service
public class CreateServiceImpl implements CreateService {

    @Autowired
    private ProfesorDao profesorDao;
    @Autowired
    private AlumnoDao alumnoDao;
    @Autowired
    private MateriaDao materiaDao;

    public Response crearProfesor(Profesor profesor) {
        System.out.println("Creando nuevo profesor...");
        return Response.ok(profesorDao.save(profesor)).build();
    }
    ...

Solution

  • By default there is no validation done on the request bean (Professor) in both spring and jersey.

    If you require request bean validation you should be adding additional validation login in you bean class (Professor) in your case. Adding bean validation varies for jersey and Spring, you can refer the below links for more details on how to