You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by fulltruth <dl...@gmail.com> on 2014/03/15 19:51:56 UTC

Trying to read JSON using Jackson in TomEE

I am having a problem with Jackson and JAXB under Apache TomEE 1.6.

The Service class implements a JAX-RS resource with path /test.

The following method works as expected:

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public UserBean read() {
        UserBean userBean = new UserBean();
        userBean.setFirstName("first");
        userBean.setLastName("last");
        return userBean;
    }

Producing the following output:

    {"firstName":"first","lastName":"last"}

However, this method never executes:

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public String create(UserBean userBean) {
    	System.out.println("userBean="+userBean);
        return "success";
    }

The following JS code in createUser.html is used to pass JSON to the Service
class.

    user = {};
    user.firstName = document.getElementById('firstName').value;
    user.lastName = document.getElementById('lastName').value;
            
    // Submit the form in the background
    var oReq = new XMLHttpRequest();
    oReq.onload = reqListener;

    url = "http://localhost:8080/tomee-jackson-test/test";
    oReq.open("post", url, true);
    oReq.setRequestHeader("Content-type", "application/json");
    oReq.send(JSON.stringify(user));
		
The JS execution completes, but the println in the Service class does not
execute, nor are any exceptions thrown.

I have uploaded my test project here:
https://github.com/fulltruth/tomee-jackson-test

I read this post:
http://openejb.979440.n4.nabble.com/JSON-serialization-td4662163.html, but
that only covers writing JSON, not reading like I am attempting to do here.

Am I doing something wrong, or is this not supported in TomEE?



--
View this message in context: http://openejb.979440.n4.nabble.com/Trying-to-read-JSON-using-Jackson-in-TomEE-tp4668215.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: Trying to read JSON using Jackson in TomEE

Posted by Romain Manni-Bucau <rm...@gmail.com>.
form data uses @FormParam no?

btw the error you got should really have printed an error in logs, you
can check it on trunk if you have doubts on 1.6.0 but maybe something
is not well configured elsewhere
Romain Manni-Bucau
Twitter: @rmannibucau
Blog: http://rmannibucau.wordpress.com/
LinkedIn: http://fr.linkedin.com/in/rmannibucau
Github: https://github.com/rmannibucau



2014-03-16 13:24 GMT+01:00 Thiago Veronezi <th...@veronezi.org>:
> form data should work...
>
> https://developer.mozilla.org/en-US/docs/Web/Guide/Using_FormData_Objects
>
> []s,
> Thiago.
> On Mar 15, 2014 9:01 PM, "fulltruth" <dl...@gmail.com> wrote:
>
>> OK, turns out it was my testing that was the problem.  The Javascript I was
>> using just didn't work for post, at all.  Not sure why.  Anyway, I
>> downloaded a REST client and you're right, everything works fine.  Thanks
>> for testing my code, that was a big help!
>>
>>
>>
>> --
>> View this message in context:
>> http://openejb.979440.n4.nabble.com/Trying-to-read-JSON-using-Jackson-in-TomEE-tp4668215p4668221.html
>> Sent from the OpenEJB User mailing list archive at Nabble.com.
>>

Re: Trying to read JSON using Jackson in TomEE

Posted by Thiago Veronezi <th...@veronezi.org>.
form data should work...

https://developer.mozilla.org/en-US/docs/Web/Guide/Using_FormData_Objects

[]s,
Thiago.
On Mar 15, 2014 9:01 PM, "fulltruth" <dl...@gmail.com> wrote:

> OK, turns out it was my testing that was the problem.  The Javascript I was
> using just didn't work for post, at all.  Not sure why.  Anyway, I
> downloaded a REST client and you're right, everything works fine.  Thanks
> for testing my code, that was a big help!
>
>
>
> --
> View this message in context:
> http://openejb.979440.n4.nabble.com/Trying-to-read-JSON-using-Jackson-in-TomEE-tp4668215p4668221.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>

Re: Trying to read JSON using Jackson in TomEE

Posted by fulltruth <dl...@gmail.com>.
OK, turns out it was my testing that was the problem.  The Javascript I was
using just didn't work for post, at all.  Not sure why.  Anyway, I
downloaded a REST client and you're right, everything works fine.  Thanks
for testing my code, that was a big help!



--
View this message in context: http://openejb.979440.n4.nabble.com/Trying-to-read-JSON-using-Jackson-in-TomEE-tp4668215p4668221.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: Trying to read JSON using Jackson in TomEE

Posted by fulltruth <dl...@gmail.com>.
Romain,

Thanks for trying it out.  I built the snapshot from svn, started it up, and
deployed my test, but got the same result as before.

I am using Eclipse to deploy my project into TomEE.  Do you have any ideas
of what I might be doing wrong?

Also, what might I put into logging.properties to see what is happening?



--
View this message in context: http://openejb.979440.n4.nabble.com/Trying-to-read-JSON-using-Jackson-in-TomEE-tp4668215p4668220.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: Trying to read JSON using Jackson in TomEE

Posted by Romain Manni-Bucau <rm...@gmail.com>.
PS: just tested your sample on trunk (should be the same on 1.6.0) and
it works very well ;)

are you sure you have no exception? maybe you just conflict between
jaxrs servlet and html? maybe define an Application with an
ApplicationPath or configure tomee to let createUser.html be handled
by default servlet and not jaxrs one
Romain Manni-Bucau
Twitter: @rmannibucau
Blog: http://rmannibucau.wordpress.com/
LinkedIn: http://fr.linkedin.com/in/rmannibucau
Github: https://github.com/rmannibucau



2014-03-15 21:26 GMT+01:00 Romain Manni-Bucau <rm...@gmail.com>:
> Hi
>
> no logs saying there was an issue with the endpoint?
>
>
> Romain Manni-Bucau
> Twitter: @rmannibucau
> Blog: http://rmannibucau.wordpress.com/
> LinkedIn: http://fr.linkedin.com/in/rmannibucau
> Github: https://github.com/rmannibucau
>
>
>
> 2014-03-15 21:13 GMT+01:00 John D. Ament <jo...@gmail.com>:
>> It's odd that it would come over as a query param.  Can you check the
>> actual HTTP request your client is making?
>>
>> On Sat, Mar 15, 2014 at 3:59 PM, fulltruth <dl...@gmail.com> wrote:
>>> If I take out this line:
>>>
>>> @Consumes(MediaType.APPLICATION_JSON)
>>>
>>> ...don't set Content-Type on the AJAX call, and change the createUser method
>>> to this:
>>>
>>>     public String createUser(@QueryParam("user") String userJSON) {
>>>         try {
>>>                 ObjectMapper mapper = new ObjectMapper();
>>>                 User userBean = mapper.readValue(userJSON, User.class);
>>>
>>> This works just fine.  So Jackson can convert from JSON to the User class
>>> without any annotations.  What I want is for TomEE to call Jackson and do
>>> this automatically.
>>>
>>>
>>>
>>> --
>>> View this message in context: http://openejb.979440.n4.nabble.com/Trying-to-read-JSON-using-Jackson-in-TomEE-tp4668215p4668216.html
>>> Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: Trying to read JSON using Jackson in TomEE

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Hi

no logs saying there was an issue with the endpoint?


Romain Manni-Bucau
Twitter: @rmannibucau
Blog: http://rmannibucau.wordpress.com/
LinkedIn: http://fr.linkedin.com/in/rmannibucau
Github: https://github.com/rmannibucau



2014-03-15 21:13 GMT+01:00 John D. Ament <jo...@gmail.com>:
> It's odd that it would come over as a query param.  Can you check the
> actual HTTP request your client is making?
>
> On Sat, Mar 15, 2014 at 3:59 PM, fulltruth <dl...@gmail.com> wrote:
>> If I take out this line:
>>
>> @Consumes(MediaType.APPLICATION_JSON)
>>
>> ...don't set Content-Type on the AJAX call, and change the createUser method
>> to this:
>>
>>     public String createUser(@QueryParam("user") String userJSON) {
>>         try {
>>                 ObjectMapper mapper = new ObjectMapper();
>>                 User userBean = mapper.readValue(userJSON, User.class);
>>
>> This works just fine.  So Jackson can convert from JSON to the User class
>> without any annotations.  What I want is for TomEE to call Jackson and do
>> this automatically.
>>
>>
>>
>> --
>> View this message in context: http://openejb.979440.n4.nabble.com/Trying-to-read-JSON-using-Jackson-in-TomEE-tp4668215p4668216.html
>> Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: Trying to read JSON using Jackson in TomEE

Posted by "John D. Ament" <jo...@gmail.com>.
It's odd that it would come over as a query param.  Can you check the
actual HTTP request your client is making?

On Sat, Mar 15, 2014 at 3:59 PM, fulltruth <dl...@gmail.com> wrote:
> If I take out this line:
>
> @Consumes(MediaType.APPLICATION_JSON)
>
> ...don't set Content-Type on the AJAX call, and change the createUser method
> to this:
>
>     public String createUser(@QueryParam("user") String userJSON) {
>         try {
>                 ObjectMapper mapper = new ObjectMapper();
>                 User userBean = mapper.readValue(userJSON, User.class);
>
> This works just fine.  So Jackson can convert from JSON to the User class
> without any annotations.  What I want is for TomEE to call Jackson and do
> this automatically.
>
>
>
> --
> View this message in context: http://openejb.979440.n4.nabble.com/Trying-to-read-JSON-using-Jackson-in-TomEE-tp4668215p4668216.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: Trying to read JSON using Jackson in TomEE

Posted by fulltruth <dl...@gmail.com>.
If I take out this line:

@Consumes(MediaType.APPLICATION_JSON)

...don't set Content-Type on the AJAX call, and change the createUser method
to this:

    public String createUser(@QueryParam("user") String userJSON) {
    	try {
    		ObjectMapper mapper = new ObjectMapper();
    		User userBean = mapper.readValue(userJSON, User.class);

This works just fine.  So Jackson can convert from JSON to the User class
without any annotations.  What I want is for TomEE to call Jackson and do
this automatically.



--
View this message in context: http://openejb.979440.n4.nabble.com/Trying-to-read-JSON-using-Jackson-in-TomEE-tp4668215p4668216.html
Sent from the OpenEJB User mailing list archive at Nabble.com.