Uploaded image for project: 'RESTEasy'
  1. RESTEasy
  2. RESTEASY-2158

Unable to use Bean Validation with JSON-B in RESTEasy 3.x

    XMLWordPrintable

Details

    • Feature Request
    • Resolution: Unresolved
    • Critical
    • None
    • 3.6.2.Final
    • None
    • None

    Description

      Original jira: RESTEASY-2107


      When trying to use the JSON-B provider (aka json-binding) with a BV related example, I discovered it does not play well with the Bean Validation support:

      Caused by: org.jboss.resteasy.core.NoMessageBodyWriterFoundFailure: Could not find MessageBodyWriter for response object of type: org.jboss.resteasy.api.validation.ViolationReport of media type: application/json
      at org.jboss.resteasy.core.ServerResponseWriter.lambda$writeNomapResponse$2(ServerResponseWriter.java:105)
      at org.jboss.resteasy.core.interception.jaxrs.ContainerResponseContextImpl.filter(ContainerResponseContextImpl.java:405)
      at org.jboss.resteasy.core.ServerResponseWriter.executeFilters(ServerResponseWriter.java:218)

      The issue here is that ViolationReport has some JAXB annotations (as we also need it to work with XML) and, in this case, the JSON-B provider is not considered as valid:
      https://github.com/resteasy/Resteasy/blob/3.6/providers/json-binding/src/main/java/org/jboss/resteasy/plugins/providers/jsonb/JsonBindingProvider.java#L68

      I don't think these JAXB conditions is a good idea for 2 reasons:

      • having JAXB annotations don't say you won't use JSON to serialize the object;
      • JAXB is gone in stock JDK 11 and the JsonBindingProvider won't work if JAXB is not in the classpath. So you would need to add a JAXB API jar to your classpath to have JSON-B working, which sounds like a bad idea.

      I think we should get rid of these JAXB conditions (and of the condition on the presence of JSON-B annotations as they are not required and it won't make sense anymore).


      Example:
      Server side:

      @Path("/")
      public class ValidationResourceWithReturnValues {
          @POST
          @Path("/post")
          @Produces("application/json")
          public String post(@Size(min = 5, max = 10) String s) {
              return s;
          }
      }
      

      Client side:

      public class App {
          static Client client;
          public static void main(String[] args) throws Exception {
              client = ClientBuilder.newClient();
              WebTarget target = client.target("http://localhost:8080/jaxrs-wf/post");
              Response response = target.request().accept(MediaType.APPLICATION_JSON).post(Entity.text("aa"));
              ViolationReport r = response.readEntity(ViolationReport.class);
              System.out.println(r.toString());
              System.out.println(r.getParameterViolations().get(0).getValue());
          }
      }
      
              <dependency>
                  <groupId>org.jboss.resteasy</groupId>
                  <artifactId>resteasy-client</artifactId>
                  <version>${version.resteasy}</version>
              </dependency>
              <!--<dependency>-->
                  <!--<groupId>org.jboss.resteasy</groupId>-->
                  <!--<artifactId>resteasy-jackson2-provider</artifactId>-->
                  <!--<version>${version.resteasy}</version>-->
              <!--</dependency>-->
              <dependency>
                  <groupId>org.jboss.resteasy</groupId>
                  <artifactId>resteasy-json-binding-provider</artifactId>
                  <version>${version.resteasy}</version>
              </dependency>
      

      Attachments

        Issue Links

          Activity

            People

              rsigal@redhat.com Ronald Sigal
              mkopecky@redhat.com Marek Kopecky
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated: