users@jersey.java.net

Re: [Jersey] Post with httpClient a map to a jersey service

From: Paul Sandoz <Paul.Sandoz_at_oracle.com>
Date: Wed, 13 Oct 2010 18:07:32 +0200

On Oct 13, 2010, at 5:52 PM, lio wrote:

>
> hello, I'm facing some difficulty to post a map to a jersey service
> I'm successfully do the same with a list
>
> first tthe client code
>
> @Test
> public void testPutHash() throws Exception {
> System.out.println("testPutList --> START");
>
> Map <String,String> hashPerso=new HashMap <String,String> ();
> hashPerso.put("gadille1", "lionel");
> hashPerso.put("gadille2", "luna");
> for (String key : hashPerso.keySet()) {
> System.out.println("key:"+key+"val:"+hashPerso.get(key));
> }
>
> HashPersos hp= new HashPersos();
> hp.setHashPerso(hashPerso);
>
> JAXBContext ctx = JAXBContext.newInstance(new Class
> []{com.gadille.Dao.HashPersos.class});
> Marshaller m = ctx.createMarshaller();
> ByteArrayOutputStream baos = new ByteArrayOutputStream();
> m.marshal(hp, baos);
> m.marshal(hp, System.out);
>
> PutMethod method = new PutMethod(URL_PREFIX + "putObject/
> hash");
> RequestEntity request = new
> ByteArrayRequestEntity(baos.toByteArray(),
> "application/xml; charset=utf-8");
> method.setRequestEntity(request);
>
> HttpClient client = new HttpClient();
> String rep=null;
> try {
> client.executeMethod(method);
> assertEquals("Is Alive status code not valid",
> HttpServletResponse.SC_OK, method.getStatusCode());
> rep=method.getResponseBodyAsString();
> } finally {
> method.releaseConnection();
> }
> assertEquals("Object return","lionel<br>luna<br>", rep);
> System.out.println("testPutList --> PASS");
> }
>
> //jersey serveur utility class embended my Map
>
> package com.gadille.Dao;
>
> import java.util.HashMap;
> import java.util.Map;
> import javax.xml.bind.annotation.XmlRootElement;
> @XmlRootElement
> public class HashPersos {
> Map <String,String> hashPerso=new HashMap <String,String> ();
> public HashPersos(){
> }
> public Map<String, String> getHashPerso() {
> return hashPerso;
> }
> public void setHashPerso(Map<String, String> hashPerso) {
> this.hashPerso = hashPerso;
> }
> }
>
> @PUT
> @Path("/hash")
> @Consumes(MediaType.APPLICATION_XML)
> @Produces(MediaType.TEXT_HTML)
> public String putHash(HashMap<String,String> hashPerso) { //
> JAXBElement

Use the type HashPersos instead.

Jersey does not currently support Map<String, T> where T is String,
boxed primiative, or another JAXB bean/type.

If Jersey were to support Map<String, T> i would expect there a root
element then elements for each entry e.g.

<root>
   <entry><key>xxx</key><value>....</value></entry>
   <entry><key>xxx</key><value>....</value></entry>
   <entry><key>xxx</key><value>....</value></entry>
</root>

Like Jersey's support for list it should not matter what the root
element name is in terms of consuming the XML document.

Paul.


> String html="";
> for (String key : hashPerso.keySet()) {
> html="key:"+key+" value:"+hashPerso.get(key)+"<br>";
> }
> return (html);
> }
>
>
> the error is :
> GRAVE: A message body reader for Java class java.util.HashMap, and
> Java type
> java.util.HashMap<java.lang.String, java.lang.String>, and MIME
> media type
> application/xml;charset=utf-8 was not found
> 13 oct. 2010 17:24:26 com.sun.jersey.spi.container.ContainerRequest
> getEntity
> GRAVE: The registered message body readers compatible with the MIME
> media
> type are:
> application/xml;charset=utf-8 ->
> com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$App
> com.sun.jersey.core.impl.provider.entity.DocumentProvider
>
>
> I take a look to the xml send :
>
> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
> <hashPersos>
> <hashPerso>
> <entry><key>gadille1</key><value>lionel</value></entry>
> <entry><key>gadille2</key><value>luna</value></entry>
> </hashPerso>
> </hashPersos>
>
> I thing error come from client but .... i'm not sure and i don't
> know the
> form of xml is hould send and how to do it
>
> Here the code under cvs view
> http://backuprsync.cvs.sourceforge.net/viewvc/backuprsync/jersey1/src/main/java/com/gadille/services/PutObject.java?view=markup
> http://backuprsync.cvs.sourceforge.net/viewvc/backuprsync/jersey1/src/main/java/com/gadille/Dao/HashPersos.java?view=markup
> http://backuprsync.cvs.sourceforge.net/viewvc/backuprsync/jersey1/src/test/java/com/gadille/TestHttClient.java?hideattic=0&view=markup&pathrev=MAIN
>
> if you want to load the full project
> http://sourceforge.net/projects/backuprsync/develop
> modul Jersey1
>
> Thanks , Lionel
> --
> View this message in context: http://jersey.576304.n2.nabble.com/Post-with-httpClient-a-map-to-a-jersey-service-tp5631495p5631495.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
>