Using XMLRootElement with an Interface REST service using jersey
I am trying to write a REST service using Jersey.
It works fine when I use the @XMLRootElement as the top element of my resource class (Todo.java).
I am trying to make an interface as a root element and it throws a 500 (internal server error)

  	package de.vogella.jersey.jaxb.model;

	import javax.ws.rs.Produces;
	import javax.ws.rs.core.MediaType;
	import javax.xml.bind.annotation.XmlRootElement;
	import javax.xml.bind.annotation.XmlSeeAlso;
	import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;


	import com.sun.xml.internal.bind.AnyTypeAdapter;

	@XmlJavaTypeAdapter(AnyTypeAdapter.class) 
	@XmlRootElement
	@XmlSeeAlso({Todo.class})
	public interface TestTodo  {
		@Produces(MediaType.TEXT_PLAIN)
		public String getSummary();
		
		public void setSummary(String summary);
		
		@Produces(MediaType.TEXT_PLAIN)
		public String getDescription();
		
		public void setDescription(String description);
	}


The Todo class
	package de.vogella.jersey.jaxb.model;

	import javax.ws.rs.Produces;
	import javax.ws.rs.core.MediaType;


	public class Todo implements TestTodo {
		
		private String summary;
		private String description;
		
		@Produces({MediaType.TEXT_PLAIN})
		public String getSummary() {
			return summary;
		}

		public void setSummary(String summary) {
			this.summary = summary;
		}

		public String getDescription() {
			return description;
		}

		public void setDescription(String description) {
			this.description = description;
		}

	}

This is the error :
SEVERE: A message body writer for Java class de.vogella.jersey.jaxb.model.Todo, and Java type class de.vogella.jersey.jaxb.model.Todo, and MIME media type application/xml was not found
Mar 9, 2012 3:17:07 PM com.sun.jersey.spi.container.ContainerResponse write

I did write a MessageReader class, but even that doesnot help. I get the same error

	package de.vogella.jersey.jaxb.model;

	import java.io.IOException;
	import java.io.InputStream;
	import java.lang.annotation.Annotation;
	import java.lang.reflect.Type;

	import javax.ws.rs.Produces;
	import javax.ws.rs.WebApplicationException;
	import javax.ws.rs.core.MediaType;
	import javax.ws.rs.core.MultivaluedMap;
	import javax.ws.rs.ext.MessageBodyReader;
	import javax.ws.rs.ext.Provider;
	import javax.xml.bind.JAXBContext;
	import javax.xml.bind.JAXBException;
	import javax.xml.bind.annotation.XmlRootElement;

	@Provider
	public class TestMessageBodyReader implements MessageBodyReader {

		@Override
		public boolean isReadable(Class type, Type genericType,
				Annotation[] annotations, MediaType mediaType) {
			return type.isAnnotationPresent(XmlRootElement.class);
		}

		@Override
		public Object readFrom(Class type, Type genericType,
				Annotation[] annotations, MediaType mediaType,
				MultivaluedMap httpHeaders, InputStream entityStream)
				throws IOException, WebApplicationException {
			try{
				JAXBContext context = JAXBContext.newInstance(type);
				return context.createUnmarshaller().unmarshal(entityStream);
				
			}catch(JAXBException ex){
				throw new RuntimeException();
			}
		}

	}


Two questions :
1. Is it possible to use @XMLRootElement on an interface
2. What am I doing wrong here ? I am very new to Jersey, so any help is greatly appreciated

Thanks !

Using XMLRootElement with an Interface REST service using jersey
===

I am trying to write a REST service using Jersey.
It works fine when I use the @XMLRootElement as the top element of my resource class (Todo.java).
I am trying to make an interface as a root element and it throws a 500 (internal server error)

  	package de.vogella.jersey.jaxb.model;

	import javax.ws.rs.Produces;
	import javax.ws.rs.core.MediaType;
	import javax.xml.bind.annotation.XmlRootElement;
	import javax.xml.bind.annotation.XmlSeeAlso;
	import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;


	import com.sun.xml.internal.bind.AnyTypeAdapter;

	@XmlJavaTypeAdapter(AnyTypeAdapter.class) 
	@XmlRootElement
	@XmlSeeAlso({Todo.class})
	public interface TestTodo  {
		@Produces(MediaType.TEXT_PLAIN)
		public String getSummary();
		
		public void setSummary(String summary);
		
		@Produces(MediaType.TEXT_PLAIN)
		public String getDescription();
		
		public void setDescription(String description);
	}


The Todo class
	package de.vogella.jersey.jaxb.model;

	import javax.ws.rs.Produces;
	import javax.ws.rs.core.MediaType;


	public class Todo implements TestTodo {
		
		private String summary;
		private String description;
		
		@Produces({MediaType.TEXT_PLAIN})
		public String getSummary() {
			return summary;
		}

		public void setSummary(String summary) {
			this.summary = summary;
		}

		public String getDescription() {
			return description;
		}

		public void setDescription(String description) {
			this.description = description;
		}

	}

This is the error :
SEVERE: A message body writer for Java class de.vogella.jersey.jaxb.model.Todo, and Java type class de.vogella.jersey.jaxb.model.Todo, and MIME media type application/xml was not found
Mar 9, 2012 3:17:07 PM com.sun.jersey.spi.container.ContainerResponse write

I did write a MessageReader class, but even that doesnot help. I get the same error

	package de.vogella.jersey.jaxb.model;

	import java.io.IOException;
	import java.io.InputStream;
	import java.lang.annotation.Annotation;
	import java.lang.reflect.Type;

	import javax.ws.rs.Produces;
	import javax.ws.rs.WebApplicationException;
	import javax.ws.rs.core.MediaType;
	import javax.ws.rs.core.MultivaluedMap;
	import javax.ws.rs.ext.MessageBodyReader;
	import javax.ws.rs.ext.Provider;
	import javax.xml.bind.JAXBContext;
	import javax.xml.bind.JAXBException;
	import javax.xml.bind.annotation.XmlRootElement;

	@Provider
	public class TestMessageBodyReader implements MessageBodyReader {

		@Override
		public boolean isReadable(Class type, Type genericType,
				Annotation[] annotations, MediaType mediaType) {
			return type.isAnnotationPresent(XmlRootElement.class);
		}

		@Override
		public Object readFrom(Class type, Type genericType,
				Annotation[] annotations, MediaType mediaType,
				MultivaluedMap httpHeaders, InputStream entityStream)
				throws IOException, WebApplicationException {
			try{
				JAXBContext context = JAXBContext.newInstance(type);
				return context.createUnmarshaller().unmarshal(entityStream);
				
			}catch(JAXBException ex){
				throw new RuntimeException();
			}
		}

	}


Two questions :
1. Is it possible to use @XMLRootElement on an interface
2. What am I doing wrong here ? I am very new to Jersey, so any help is greatly appreciated

Thanks !
