0

This is my web service code

@Path("/totti")
public class KK {
    @GET
    @Produces(MediaType.TEXT_XML)
    public List<FoodItem> getRestarantsFordBrowser() {
        return FoodItemImpl.getAllFoodItems();
    }

    @GET
    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    public List<FoodItem> getXML() {
        return FoodItemImpl.getAllFoodItems();
    }
}

The foodItem is an interface.

The foodItemImpl is a class which implements the FoodItem interface. Also this class begins with @XmlRootElement(name = "FoodItem")

the getAllFoodItems() is a static method, its code is

    @XmlElement(name="Item")
        public static List<FoodItem> getAllFoodItems() {
return new LinkedList<FoodItem>();
    }

when I run the web service I got this exception

: A message body writer for Java class java.util.LinkedList, and Java type java.util.List<com.myP.eattel.food.FoodItem>, and MIME media type application/xml was not found
    ... 24 more

I have been trying to solving this exception for 5 hours, I googled a lot all the threads and solutions didn't help me

Edit

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>myP.webservices</display-name>
    <servlet>
        <servlet-name>Eattel REST Service</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.myP.eattel</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Eattel REST Service</servlet-name>
        <url-pattern>/eattel/*</url-pattern>
    </servlet-mapping>
</web-app>
user2387331
  • 99
  • 1
  • 3
  • 11
  • http://stackoverflow.com/questions/13108161/a-message-body-writer-for-java-class-not-found – Thihara May 16 '13 at 08:51
  • @Thihara in that thread, It is returned an object, not a list of objects, I already can return an object, just one object – user2387331 May 16 '13 at 08:53
  • Try creating a wrapper class for your list. Take a look here: http://theopentutorials.com/tutorials/java/jaxb/jaxb-marshalling-and-unmarshalling-list-of-objects/ – darijan May 16 '13 at 09:05
  • have to you tried this: http://stackoverflow.com/questions/5161466/how-do-i-use-the-jersey-json-pojo-support, also show us relevant parts of your web.xml – harsh May 16 '13 at 09:16

2 Answers2

1

Can you add following in <servlet> tag:

         <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>
harsh
  • 7,502
  • 3
  • 31
  • 32
  • I already added it, put I gave u the code before I added it. now it works, I just added a wraper enum but I have new problem, is that I just can return a list from the class not from the interface, – user2387331 May 16 '13 at 09:52
0

I think it makes sense to wrap your List in a simple object:

  • Solves your problem simply and easily.
  • Allows you to extend the response format (eg add metadata to your list like paging, sorting) without a breaking change.
  • Avoids security problems if you also marshal to JSON (ref).
Community
  • 1
  • 1
Matthew Gilliard
  • 9,298
  • 3
  • 33
  • 48