On Feb 13, 2009, at 11:52 PM, JavaGeek_Boston wrote:
>
> Hello All,
> This is probably a remedial question, but I can't get my object to  
> POST?  I
> can return my entity via a @GET call, but I can't seem to post it as  
> a form
> param.  When I execute my POST, I get either an HTTP 415 or 400 error,
> depending on what I set for @Consumes
>
> My object is a bean
> @XmlRootElement
> public class MyDocumentType implements Serializable{
> 	//some fields and getters and setters.
> }
>
> Here's my service:
> 	@POST
> 	@Path(DOC)
> 	@Consumes("application/xml")
> 	public String updateDocument(@HeaderParam(SESSION_ID_HEADER_KEY)  
> String
> sessionId,
> 			@FormParam(DOC_OBJ) MyDocumentType doc) {
> 		confirmSessionIsValidAndAuthorIsLoggedIn(sessionId, doc);
> 		storeService.updateDocument(doc);
> 		return "SUCCESS:  " + doc.getId();
> 	}
>
> Here's my client:
> 	Client client = new Client();
> 	resource = client.resource(serverURL);
> 	final WebResource finalPath = resource.path(path);
> 	Form form = new Form();
> 	form.add("doc", doc);
> 	return finalPath.type("application/ 
> xml").header(SESSION_ID_HEADER_KEY,
> sessionId).post(String.class, form);
>
>
> I get an HTTP 400 with that combination.
>
The problem is that you are first using the media type "application/ 
xml" (Jersey should not allow this or should warn you).
Second, you seem to want to add an XML document as a form parameter  
value. It is only possible to support the same rules as for say  
@QueryParam. See the following for more details on the rules:
   
http://wikis.sun.com/display/Jersey/Overview+of+JAX-RS+1.0+Features#OverviewofJAX-RS1.0Features-ExtractingRequestParameters
> I think my bean is OK as I can retrieve a MyDocumentType via @GET.
>
> When I call:
> return
> finalPath 
> .type 
> (MediaType.APPLICATION_FORM_URLENCODED).header(SESSION_ID_HEADER_KEY,
> sessionId).post(String.class, form);
> with
> 	@Consumes("application/x-www-form-urlencoded")
> on the service, I get:
> SEVERE: A message body reader for Java type, class MyDocumentType,  
> and MIME
> media type, application/x-www-form-urlencoded, was not found
>
Are you sure you had the @FormParam present in such a case? If you did  
not then i can reproduce what you observe.
>
> Is there something I'm missing?  Any help would be greatly  
> appreciated:
>
If you only ever need to POST the XML document then you do not need to  
use a form:
   @Consumes("multipart/form-param")
   @POST
   public String updateDocument(
       @HeaderParam(SESSION_ID_HEADER_KEY) String sessionId,
       DocumentType doc) {   ... }
If you do want to POST the XML document and some other values as well  
i recommend you utilize the "multipart/form-data" media type. When  
using this type you can reuse all the supported message body readers  
like those for JAXB:
   @Consumes("multipart/form-param")
   @POST
   public String updateDocument(
       @HeaderParam(SESSION_ID_HEADER_KEY) String sessionId,
       @FormParam(DOC_OBJ) MyDocumentType doc) { ... }
The reason for this difference is that the media type "application/x- 
www-form-urlencoded" defines parameters in terms of strings and there  
is no content-type associated with each parameter, thus the general  
message body readers/writers cannot, like those for JAXB, be utilized  
in such cases. Now, arguably we could improve JAX-RS to work well with  
JAXB in such cases.
When using "multipart/form-param" the parameters are encoded as bytes  
and are associated with a content type thus we can reuse all the  
message body reader functionality, such as that for JAXB.
Hope that helps,
Paul.
> Thanks,
> Steven
> Harvard Children's Hospital Informatics Program
> -- 
> View this message in context: http://n2.nabble.com/Jersey-Client%3A--How-can-I-POST-an-object---I%27m-getting-an-HTTP-415-error.-tp2324279p2324279.html
> Sent from the Jersey mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>