users@jersey.java.net

[Jersey] Re: Question about consuming TEXT_PLAN and JSON via POST

From: Jakub Podlesak <jakub.podlesak_at_oracle.com>
Date: Fri, 18 Mar 2011 10:59:19 +0100

Hi Noah,

what about:

@POST @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType_JSON)
public Response postJson(FooBean fooBeanJsonDO) {
   return postForm(fooBeanJsonDO.foo);
}

@POST @Consumes("application/x-www-form-urlencoded")
@Produces(MediaType_JSON)
public Response postForm(@FormParam("foo") String foo) {
   // TODO: your business logic comes here...
}

Regarding the javadoc question, we only publish javadoc
for Jersey APIs. To study the source code in more detail, i suggest
you see/checkout [1].

HTH,

~Jakub

[1]http://java.net/projects/jersey/sources/svn/show/

On 03/18/2011 04:49 AM, NBW wrote:
> It doesn't look like Jersey supports consuming both JSON and
> TEXT_PLAIN at the same resource out of the box but perhaps I am
> missing something. I have Jackson POJO support configured. So for
> example:
>
> @POST
> @Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
> @Produces(MediaType.APPLICATION_JSON)
> public Response test1(@FormParam("foo") String foo, FooBean
> fooBeanJsonDO) {
> ...
> }
>
> public FooBean {
> public String foo;
> }
>
> When I mix @FormParam and the bean and try to POST JSON I get an
> IllegalStateException: The @FormParam is utilized when the content
> type of the request is not application/x-www-form-urlencoded.
> If I remove the @FormParam and simply leave the bean and POST JSON it
> works. However, then I try to POST form data it pukes. If I remove the
> bean and leave the @FormParam it works when I POST form data but pukes
> when I POST JSON.
>
> I suppose I could break this into two resources, one consuming JSON
> and one consuming FormData but I'd rather not - seems like it defeats
> the purpose. I would think I should simply be able to have my bean be
> the method parameter and Jersey would try to unmarshall the data from
> the consumed type(s) into it. eg.
>
> @POST
> @Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN})
> @Produces(MediaType.APPLICATION_JSON)
> public Response test1(FooBean fooBeanJsonDO) {
> ...
> }
>
> Also, where can I find javadoc on com.sun.jersey.core.impl.* ?
>
> Thanks,
>
> -Noah
>


-- 
Jakub Podles(ák
CZJUG co-lead,
Web Services Research And Development
Oracle, Czech s r.o.
Praha 4, V Parku 8