Search code examples
jax-rsjersey-2.0weblogic12cjava-ee-7

JAX-RS 2.0 doesn't throw exception when there is invalid attribute type in JSON request


I have the following REST endpoint:

@Stateless
@Path("test")
public class TestResource {

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public TestDTO test(TestDTO dto) {
        return dto;
    }
}

The TestDTO bean is really simple:

public class TestDTO {
    private String id;
    private Integer number;
// + getter/setter
}

If I post the following JSON, the response will be exactly the same (as expected in this trivial sample):

{
"id": "abc",
"number": 123
}

But if I send a string value for "number":

{
"id": "abc",
"number": "NotANumber"
}

the server will simply not initialize the number variable of my TestDTO (value = null) and my response will be the following:

{
    "id": "abc"
}

I don't understand... why the server doesn't respond with a "400 - Bad request"? :/

This code is running in Weblogic application server version 12.2.1.1.0 with provided Jersey implementation (I only have one dependency in my pom.xml: javaee-api version 7.0)


Solution

  • Ok so... not throwing any exception is the default choice of MOXy, the new default JAX-RS Json Provider for all Oracle products (Glassfish and WebLogic) (instead of Jackson). That seems strange to me... but yeah we have to deal with it :/

    We have to register a ValidationErrorHandler for that purpose.

    I found a complete (and working) solution right there: Custom MOXyJsonProvider in Jersey 2 not working?