2

I have created a web service function:

@GET
@Path("getusers/")
@Produces({"application/xml","application/json"})
public Object getUsers()
{
    String distanceQuery="SELECT UGL.user_id,UP.fname,UGL.mlatitude,UGL.mlogitude,UGL.mlogitude from user_gps_location UGL,user_profile UP where UGL.user_id=UP.user_id";
    Query queryResult=em.createNativeQuery(distanceQuery);

    userList=queryResult.getResultList();
    return userList;
}

The function returns a list of results: It works fine if I test it with my browser while I select the application/json option:

Result:

[[1,"Ankit",37.334542,-121.890821,-121.890821],
[1,"Ankit",37.337749,-121.886702,-121.886702],
[1,"Ankit",37.336453,-121.884985,-121.884985],
[1,"Ankit",37.336453,-121.884985,-121.884985],
[1,"Ankit",37.336453,-121.884985,-121.884985],
[1,"Ankit",32.727798,-117.15683,-117.15683],
[1,"Ankit",37.334541666666674,-121.89081999999999,-121.89081999999999],
[1,"Ankit",37.33774833333334,-121.88670166666667,-121.88670166666667],
[1,"Ankit",37.33774833333334,-121.88670166666667,-121.88670166666667],
[1,"Ankit",37.334541666666674,-121.89081999999999,-121.89081999999999],
[1,"Ankit",37.334541666666674,-121.89081999999999,-121.89081999999999],
[1,"Ankit",37.33774833333334,-121.88670166666667,-121.88670166666667],
[2,"Niharika",37.334541666666674,-121.88670166666667,-121.88670166666667],
[2,"Niharika",37.334541666666674,-121.88670166666667,-121.88670166666667]]

but when I select "application/xml" it gives me an error:

javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class java.util.Vector, and Java type class java.util.Vector, and MIME media type application/xml was not found

Can anyone help me and tell me what's wrong with it?

Also How I can return a well formed result like below?

<id>1</id>
<name>Ankit</name>

I know I need to use a class for it but as this is a native query and I use two tables and also return a result list, I don't know how to do that.

siebz0r
  • 18,867
  • 14
  • 64
  • 107
user1522804
  • 149
  • 2
  • 11

2 Answers2

2

Try changing

public Object getUsers() 

to

public List<MyObjectType> getUsers()

to match type userList.

Also, make sure @XMLRootElement annotation is present on the domain class

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Ya @XMLRootElement is there on the domain class. Can you please tell me for public List which class object will I mention in list as I mention above my query has two tables user_gps_location and user_profile i dont know how to map them with return results. – user1522804 Aug 02 '12 at 00:30
  • Type is List for selects across multiple tables. – Reimeus Aug 02 '12 at 00:43
  • No still its not working after change it to List and gives me following error: java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.GenericArrayTypeImpl cannot be cast to java.lang.Class while in browser it gives me below error msg:Response: { HTTP Status 406 - Not Acceptable } – user1522804 Aug 02 '12 at 00:55
  • Try the List without the generics. – Reimeus Aug 02 '12 at 00:59
  • No it still gives me error: java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType. As I understand I think I am using nativequery and it has no connection with any domain class. Does any thing wrong with that? If yes then how can I do it with this scenario. – user1522804 Aug 02 '12 at 01:23
  • Should be no problem with native query. Think problem is that u need to get the return object mapped properly for marshalling. – Reimeus Aug 02 '12 at 09:40
  • Can you please suggest me how can I do marshalling? I have no idea about it. – user1522804 Aug 03 '12 at 16:57
  • jersey will handle this for you - http://www.mkyong.com/webservices/jax-rs/download-xml-with-jersey-jaxb/ – Reimeus Aug 04 '12 at 01:20
0

I once had a similar problem. The cause of the problem was that I was missing some libraries. (I couldn't produce JSON, I was missing the jersey-json dependency). By adding the dependency the problem was solved.

Check if you can produce XML with a simple method like this:

@GET
@Path("test")
@Produces(MediaType.APPLICATION_XML)
public List<String> test()
{
    List<String> test = new ArrayList<String>();
    test.add("string 1");
    test.add("string 2");
    return test;
}

If you cannot produce XML with this method try adding jersey-core-1.13 as a Maven dependency:

<dependency>
    <description></description>
    <groupId>com.sun.jersey</groupId>
    <artefactId>jersey-core</artefactId>
    <version>1.13</version>
</dependency>

If you're free to choose which JAX-RS implementation is used I would strongly suggest taking a look at RESTEasy. It offers a lot of cool features like bean validation and offers a really easy to use client library.

siebz0r
  • 18,867
  • 14
  • 64
  • 107
  • Hey as you mention I added test() method and as you mention jersey-core-1.13 as a Maven dependency, I added jersey-core-1.13.jar into my netbeans project class library and then run the webservice but still I am not getting xml output. Here I just want to know that Am I right by adding jersy-core-1.13 to library or do I need further steps? If not and as you mentioned where should I write above stuff? – user1522804 Aug 03 '12 at 18:21
  • The `` is a Maven dependency. Maven is a very common and quite easy build and dependency management. Netbeans by default uses Ant (iirc). – siebz0r Aug 03 '12 at 18:29