You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomee.apache.org by chege <ch...@programmer.net> on 2020/06/15 11:08:55 UTC

JSON-B Adapter Not Invoked

Hi,

I am facing a problem where jsonb adapter is not invoked.
Here is a simplified case.

jaxrs application
---------------------------

@ApplicationPath("/app")
public class App extends Application{

}

adapter
------------------------

public class B implements JsonbAdapter<Book, JsonObject> {

     @Override
     public JsonObject adaptToJson(Book obj) throws Exception {
         return Json.createObjectBuilder()
                 .add("customfield", obj.getId() + " " + obj.getTitle())
                 .build();
     }

     @Override
     public Book adaptFromJson(JsonObject obj) throws Exception {
         throw new UnsupportedOperationException("Not supported yet.");
//To change body of generated methods, choose Tools | Templates.
     }

}

dto
------------------------
public class Book {

     private int id;
     private String title;

     public Book() {
     }

     public Book(int id, String title) {
         this.id = id;
         this.title = title;
     }

     public int getId() {
         return id;
     }

     public void setId(int id) {
         this.id = id;
     }

     public String getTitle() {
         return title;
     }

     public void setTitle(String title) {
         this.title = title;
     }

}

resource
-----------------------

@Path("/r")
public class R {

     @GET
     @Produces({MediaType.APPLICATION_JSON})
     public Response response() {
         Book book = new Book(100, "Apache Tomee");
         JsonbConfig config = new JsonbConfig()
                 .withFormatting(true)
                 .withAdapters(new B());
         Jsonb jsonb = JsonbBuilder.create(config);
         String toJson = jsonb.toJson(book);
         return Response.ok(toJson).build();
     }

}

What am i missing?

Thanks.


Re: JSON-B Adapter Not Invoked

Posted by David Blevins <da...@gmail.com>.
Hey, chege.

Are you interested in helping with this at all as a project developer?  If, so an Arquillian test in this module would be a perfect start:

 - https://github.com/apache/tomee/tree/master/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests

If not, can you drop the dev@ list from the response so we know not to send you "how to help the project" answers.  We definitely do need help, anyone is truly welcome and honestly any bit helps, but we don't want to push people if that isn't their intention.


-David

> On Jun 15, 2020, at 4:08 AM, chege <ch...@programmer.net> wrote:
> 
> Hi,
> 
> I am facing a problem where jsonb adapter is not invoked.
> Here is a simplified case.
> 
> jaxrs application
> ---------------------------
> 
> @ApplicationPath("/app")
> public class App extends Application{
> 
> }
> 
> adapter
> ------------------------
> 
> public class B implements JsonbAdapter<Book, JsonObject> {
> 
>    @Override
>    public JsonObject adaptToJson(Book obj) throws Exception {
>        return Json.createObjectBuilder()
>                .add("customfield", obj.getId() + " " + obj.getTitle())
>                .build();
>    }
> 
>    @Override
>    public Book adaptFromJson(JsonObject obj) throws Exception {
>        throw new UnsupportedOperationException("Not supported yet.");
> //To change body of generated methods, choose Tools | Templates.
>    }
> 
> }
> 
> dto
> ------------------------
> public class Book {
> 
>    private int id;
>    private String title;
> 
>    public Book() {
>    }
> 
>    public Book(int id, String title) {
>        this.id = id;
>        this.title = title;
>    }
> 
>    public int getId() {
>        return id;
>    }
> 
>    public void setId(int id) {
>        this.id = id;
>    }
> 
>    public String getTitle() {
>        return title;
>    }
> 
>    public void setTitle(String title) {
>        this.title = title;
>    }
> 
> }
> 
> resource
> -----------------------
> 
> @Path("/r")
> public class R {
> 
>    @GET
>    @Produces({MediaType.APPLICATION_JSON})
>    public Response response() {
>        Book book = new Book(100, "Apache Tomee");
>        JsonbConfig config = new JsonbConfig()
>                .withFormatting(true)
>                .withAdapters(new B());
>        Jsonb jsonb = JsonbBuilder.create(config);
>        String toJson = jsonb.toJson(book);
>        return Response.ok(toJson).build();
>    }
> 
> }
> 
> What am i missing?
> 
> Thanks.
> 


Re: JSON-B Adapter Not Invoked

Posted by David Blevins <da...@gmail.com>.
Hey, chege.

Are you interested in helping with this at all as a project developer?  If, so an Arquillian test in this module would be a perfect start:

 - https://github.com/apache/tomee/tree/master/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests

If not, can you drop the dev@ list from the response so we know not to send you "how to help the project" answers.  We definitely do need help, anyone is truly welcome and honestly any bit helps, but we don't want to push people if that isn't their intention.


-David

> On Jun 15, 2020, at 4:08 AM, chege <ch...@programmer.net> wrote:
> 
> Hi,
> 
> I am facing a problem where jsonb adapter is not invoked.
> Here is a simplified case.
> 
> jaxrs application
> ---------------------------
> 
> @ApplicationPath("/app")
> public class App extends Application{
> 
> }
> 
> adapter
> ------------------------
> 
> public class B implements JsonbAdapter<Book, JsonObject> {
> 
>    @Override
>    public JsonObject adaptToJson(Book obj) throws Exception {
>        return Json.createObjectBuilder()
>                .add("customfield", obj.getId() + " " + obj.getTitle())
>                .build();
>    }
> 
>    @Override
>    public Book adaptFromJson(JsonObject obj) throws Exception {
>        throw new UnsupportedOperationException("Not supported yet.");
> //To change body of generated methods, choose Tools | Templates.
>    }
> 
> }
> 
> dto
> ------------------------
> public class Book {
> 
>    private int id;
>    private String title;
> 
>    public Book() {
>    }
> 
>    public Book(int id, String title) {
>        this.id = id;
>        this.title = title;
>    }
> 
>    public int getId() {
>        return id;
>    }
> 
>    public void setId(int id) {
>        this.id = id;
>    }
> 
>    public String getTitle() {
>        return title;
>    }
> 
>    public void setTitle(String title) {
>        this.title = title;
>    }
> 
> }
> 
> resource
> -----------------------
> 
> @Path("/r")
> public class R {
> 
>    @GET
>    @Produces({MediaType.APPLICATION_JSON})
>    public Response response() {
>        Book book = new Book(100, "Apache Tomee");
>        JsonbConfig config = new JsonbConfig()
>                .withFormatting(true)
>                .withAdapters(new B());
>        Jsonb jsonb = JsonbBuilder.create(config);
>        String toJson = jsonb.toJson(book);
>        return Response.ok(toJson).build();
>    }
> 
> }
> 
> What am i missing?
> 
> Thanks.
>