REST WS code gives error when returning ArrayList from method

REST WS code gives error when returning ArrayList from method

I am able to create a RESTful web service which returns and XML with Name and Description of User.However when I try to return an arrayList from the return value of web method instead of string I get error.


The below code for jersey web service works fine:

view plaincopy to clipboardprint?
Note: Text content in the code blocks is automatically word-wrapped
import javax.ws.rs.GET;  
import javax.ws.rs.Path;  
import javax.ws.rs.Produces;  
import javax.ws.rs.core.MediaType;  
 
@Path("/userInfo")  
public class User {  
  // This method is called if XMLis request  
  @GET 
  @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })  
  public Todo getUserInfo() {  
    User user=new User();  
    user.setName("Peter");  
    user.setDescription("Test description");  
    return user;  
  }  
    
    
 
}   
 
 
 
But when I return an array list of Users from the return value of the method I get error and it does not work.  
 
[code=java]  
import javax.ws.rs.GET;  
import javax.ws.rs.Path;  
import javax.ws.rs.Produces;  
import javax.ws.rs.core.MediaType;  
 
@Path("/userInfo")  
public class User {  
  // This method is called if XMLis request  
  @GET 
  @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })  
  public Todo getUserInfo() {  
ArrayList userList=new ArrayList()  
    User user=new User();  
    user.setName("Peter");  
    user.setDescription("Test description");  
User user1=new User();  
    user1.setName("Peter1111");  
    user1.setDescription("Test description111");  
 
 
userList.add(user1);  
userList.add(user2);  
userList.add(user1);  
    return userList;  
  } 
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/userInfo")
public class User {
  // This method is called if XMLis request
  @GET
  @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
  public Todo getUserInfo() {
    User user=new User();
    user.setName("Peter");
    user.setDescription("Test description");
    return user;
  }
 
 

}

 

But when I return an array list of Users from the return value of the method I get error and it does not work.

[code=java]
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/userInfo")
public class User {
  // This method is called if XMLis request
  @GET
  @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
  public Todo getUserInfo() {
ArrayList userList=new ArrayList()
    User user=new User();
    user.setName("Peter");
    user.setDescription("Test description");
User user1=new User();
    user1.setName("Peter1111");
    user1.setDescription("Test description111");


userList.add(user1);
userList.add(user2);
userList.add(user1);
    return userList;
  }
[/code]

On running this it does not work and gives error.Please let tell if I am doing anything wrong?
 am getting message "A message body writer for Java class java.util.ArrayList, and Java type class java.util.ArrayList, and MIME media type application/xml was not found"

The code which generates the above error message is :

 

view plaincopy to clipboardprint?
Note: Text content in the code blocks is automatically word-wrapped
import java.util.ArrayList;  
 
import java.util.Collection;  
 
import java.util.HashMap;  
 
import java.util.List;  
 
import java.util.Map;  
 
   
 
import javax.ws.rs.GET;  
 
import javax.ws.rs.Path;  
 
import javax.ws.rs.Produces;  
 
import javax.ws.rs.core.MediaType;  
 
   
 
import de.vogella.jersey.jaxb.model.Feed;  
 
import de.vogella.jersey.jaxb.model.Todo;  
 
   
@Path("/rest")  
 
public class TodoResource {  
 
    
   
 
   
 
  // This method is called if XMLis request  
 
  @GET 
 
  @Path("/feeds")  
 
  @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })  
 
  public ArrayList getFeeds() {  
 
   Feed feed=new Feed();  
 
    
 
    
 
    
 
    
 
feed.setDescription("asdfasdf");  
 
feed.setTitle("title1");  
 
feed.setUrl("asdfasd");  
 
   
 
Feed feed1=new Feed();  
 
feed.setDescription("jghjhgj");  
 
feed.setTitle("title2");  
 
feed.setUrl("cbnb");  
 
   
 
   
 
ArrayList <Feed> feedList=new ArrayList<Feed>();  
 
   
 
feedList.add(feed1);  
 
feedList.add(feed);  
 
    return feedList;  
 
  }  
 
   
 
   
 
   
 
   
 
   
 
   
 
}  
import java.util.ArrayList;

import java.util.Collection;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

 

import javax.ws.rs.GET;

import javax.ws.rs.Path;

import javax.ws.rs.Produces;

import javax.ws.rs.core.MediaType;

 

import de.vogella.jersey.jaxb.model.Feed;

import de.vogella.jersey.jaxb.model.Todo;

 
@Path("/rest")

public class TodoResource {

 
 

 

  // This method is called if XMLis request

  @GET

  @Path("/feeds")

  @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })

  public ArrayList getFeeds() {

   Feed feed=new Feed();

 

 

 

 

feed.setDescription("asdfasdf");

feed.setTitle("title1");

feed.setUrl("asdfasd");

 

Feed feed1=new Feed();

feed.setDescription("jghjhgj");

feed.setTitle("title2");

feed.setUrl("cbnb");

 

 

ArrayList <Feed> feedList=new ArrayList<Feed>();

 

feedList.add(feed1);

feedList.add(feed);

    return feedList;

  }

 } 

If i return a string it works fine but returning an arrayList give message "A message body writer for Java class java.util.ArrayList, and Java type class java.util.ArrayList, and MIME media type application/xml was not found"

Please tell how I can get the above code to work.
I have one doubt. I am returning Array of type Feed( Feed is my object which has feedId,feedDescription etc). This is working fine. you had mentioned returning an array of String. But how in such caSE can i return array of Strings when I need to return array of Feeds.?
I was just trying to point out that there are standard data types used by web services aimed at a wide audience.

If your service returns Java objects then only Java clients will be able to use it.

Copyright © 2007-2012 www.chuibin.com Chuibin Copyright