0

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)

koD
  • 53
  • 7
  • It may be getting initialized to `null` and since `number` has initialized to `null`, it must be ignored when converted to a `JSON`. Have you tried checking this option? – Aniket V Aug 24 '17 at 12:47
  • Yes indeed, "number" is initialized to null if I put a break-point on the return. But this is not the expected behaviour I think... I should receive a 400 Bad request instead – koD Aug 24 '17 at 13:05
  • So basically, it is trying to convert the json value "NotANumber" to a Java Integer, of course it cannot but instead of throwing an Exception, it continues... So I'm not able to know if my client doesn't send me the number or if he sends me something wrong... – koD Aug 24 '17 at 13:08
  • FYI, in tomcat server, I got bad request in response in similar case. So, it seems weblogic specific behavior. – Vikas Sachdeva Aug 25 '17 at 03:03
  • Yes that's exactly what I was thinking @VikasSachdeva :-( – koD Aug 25 '17 at 05:51
  • I think you should try to disable jersey implementation in weblogic and add explicit JAR of jersey for overriding weblogic jersey implementation. Check this link for disabling jersey from weblogic - https://stackoverflow.com/questions/39323977/how-to-disable-jersey-from-weblogic-12-2-1 – Vikas Sachdeva Aug 25 '17 at 06:34

1 Answers1

0

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?

koD
  • 53
  • 7