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

Custom message body writer preference

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major Major
    • 1.0.1.GA
    • 1.0.GA
    • jaxrs
    • None
    • Release Notes

      RESTEasy does not give priority to application provided MessageBodyWriters and uses built-in providers instead.

      When I create a custom MessageBodyWriter for my entity that produces text/plain MIME type my MBW is not used. Looks like built-in toString() method-based provider is used instead. I think this is a bug since it conflicts JAX-RS specification that says that application-provided providers must be used in preference to built-in providers when either can handle the request.

      Issue can be reproduced on following trivial example:

      public class User {

      private String username;
      private String email;

      public User() {
      }

      public User(String username, String email)

      { this.username = username; this.email = email; }

      public String getUsername()

      { return username; }

      public void setUsername(String username)

      { this.username = username; }

      public String getEmail()

      { return email; }

      public void setEmail(String email)

      { this.email = email; }

      }

      @Path("/user")
      @Produces("text/plain")
      public class UserResource {

      private static final User user = new User("jharting", "email@example.com");

      @GET
      public User getUser()

      { return user; }

      }

      @Provider
      @Produces("text/plain")
      public class UserBodyWriter implements MessageBodyWriter<User> {

      public long getSize(User arg0, Class<?> arg1, Type arg2, Annotation[] arg3,
      MediaType arg4)

      { return getStringRepresentation(arg0).length(); }

      public boolean isWriteable(Class<?> arg0, Type arg1, Annotation[] arg2,
      MediaType arg3)

      { // return arg0.equals(User.class) && arg3.equals(MediaType.TEXT_PLAIN_TYPE); return true; }

      public void writeTo(User arg0, Class<?> arg1, Type arg2, Annotation[] arg3,
      MediaType arg4, MultivaluedMap<String, Object> arg5,
      OutputStream arg6) throws IOException, WebApplicationException

      { BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(arg6)); bw.write(getStringRepresentation(arg0)); bw.flush(); }

      private String getStringRepresentation(User user)

      { return user.getUsername() + ";" + user.getEmail(); }

      }

            patriot1burke@gmail.com Bill Burke (Inactive)
            rhn-engineering-jharting Jozef Hartinger
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: