Json Problem in Seam using Resteasy
tracycc Dec 29, 2009 10:25 AMI am playing with jboss seam, resteasy to expose REST interface, I can get data in form of XML and text, but there is a problem when I'm trying to get the data in form of Json.
I follow the document and example but still I have this problem.
Any insight would be appreciated!
List of Version that I use
jboss-seam-2.1.2
jboss-4.2.2.GA
resteasy-jaxrs-1.2.1.GA
I leave the web.xml and component.xml as default
When I try to browse the URL:
http://localhost:8080/cuichao/seam/resource/rest/customer/book
it turns out HTTP Status 403 - Access to the requested resource has been denied
And in the server.log,it seems it can't find the org.codehaus.jettison.mapped.MappedNamespaceConvention,
but I have already included that in my project.
Here is my code:
package com.mydomain.cuichao.model;
import javax.persistence.Entity;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlAttribute;
@XmlRootElement(name = "book")
public class Book implements java.io.Serializable
{
private String author;
private String ISBN;
private String title;
public Book()
{
}
public Book(String author, String ISBN, String title)
{
this.author = author;
this.ISBN = ISBN;
this.title = title;
}
@XmlElement
public String getAuthor()
{
return author;
}
public void setAuthor(String author)
{
this.author = author;
}
@XmlElement
public String getISBN()
{
return ISBN;
}
public void setISBN(String ISBN)
{
this.ISBN = ISBN;
}
@XmlAttribute
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
}
package com.mydomain.cuichao.action;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.ws.rs.*;
import org.jboss.resteasy.annotations.providers.jaxb.json.BadgerFish;
import org.jboss.resteasy.annotations.providers.jaxb.json.Mapped;
import com.mydomain.cuichao.model.Book;
@Path("/customer")
public class MyCustomerResource {
private HashMap<String, Book> books = new HashMap<String, Book>();
public MyCustomerResource(){
books.put("596529260", new Book("Leonard Richardson", "596529260", "RESTful Web Services"));
books.put("333333333", new Book("Bill Burke", "596529260", "EJB 3.0"));
}
@GET
@Path("/{customerId}")
@Produces("text/plain")
public String getCustomer(@PathParam("customerId") int id) {
return "afd"+id;
}
@GET
@Path("/book")
@Mapped
@Produces("application/json") //Problem here
public Book getOneBook(){
Collection<Book> list = books.values();
Iterator<Book> iter = list.iterator();
Book b = iter.next();
System.out.println("*****BookListing getBooksMapped()******"+b.getTitle());
return b;
}
@GET
@Path("/bookxml") //it works fine
@Produces("application/xml")
public Book getOneBookXML(){
Collection<Book> list = books.values();
Iterator<Book> iter = list.iterator();
Book b = iter.next();
System.out.println("*****BookListing getBooksMapped()******"+b.getTitle());
return b;
}
}
I follow the document and example but still I have this problem.
Any insight would be appreciated!
List of Version that I use
jboss-seam-2.1.2
jboss-4.2.2.GA
resteasy-jaxrs-1.2.1.GA
I leave the web.xml and component.xml as default
When I try to browse the URL:
http://localhost:8080/cuichao/seam/resource/rest/customer/book
it turns out HTTP Status 403 - Access to the requested resource has been denied
And in the server.log,it seems it can't find the org.codehaus.jettison.mapped.MappedNamespaceConvention,
but I have already included that in my project.
Here is my code:
package com.mydomain.cuichao.model;
import javax.persistence.Entity;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlAttribute;
@XmlRootElement(name = "book")
public class Book implements java.io.Serializable
{
private String author;
private String ISBN;
private String title;
public Book()
{
}
public Book(String author, String ISBN, String title)
{
this.author = author;
this.ISBN = ISBN;
this.title = title;
}
@XmlElement
public String getAuthor()
{
return author;
}
public void setAuthor(String author)
{
this.author = author;
}
@XmlElement
public String getISBN()
{
return ISBN;
}
public void setISBN(String ISBN)
{
this.ISBN = ISBN;
}
@XmlAttribute
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
}
package com.mydomain.cuichao.action;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.ws.rs.*;
import org.jboss.resteasy.annotations.providers.jaxb.json.BadgerFish;
import org.jboss.resteasy.annotations.providers.jaxb.json.Mapped;
import com.mydomain.cuichao.model.Book;
@Path("/customer")
public class MyCustomerResource {
private HashMap<String, Book> books = new HashMap<String, Book>();
public MyCustomerResource(){
books.put("596529260", new Book("Leonard Richardson", "596529260", "RESTful Web Services"));
books.put("333333333", new Book("Bill Burke", "596529260", "EJB 3.0"));
}
@GET
@Path("/{customerId}")
@Produces("text/plain")
public String getCustomer(@PathParam("customerId") int id) {
return "afd"+id;
}
@GET
@Path("/book")
@Mapped
@Produces("application/json") //Problem here
public Book getOneBook(){
Collection<Book> list = books.values();
Iterator<Book> iter = list.iterator();
Book b = iter.next();
System.out.println("*****BookListing getBooksMapped()******"+b.getTitle());
return b;
}
@GET
@Path("/bookxml") //it works fine
@Produces("application/xml")
public Book getOneBookXML(){
Collection<Book> list = books.values();
Iterator<Book> iter = list.iterator();
Book b = iter.next();
System.out.println("*****BookListing getBooksMapped()******"+b.getTitle());
return b;
}
}