问题
I am new to Seam and want to develop Webservice using Seam.I have an aggressive deadline Where i can find the details to develop the Webservice using Seam.Any good document, book, website etc.
回答1:
As mentioned in the other question you could use JAX-WS. However if you'd rather use REST services (JAX-RS) then either:
- Read up on JAX-WS in the Seam Docs
- Check out Stéphane Épardaud's article here; or
- Have a look at Sun Jersey which is an implementation of JAX-RS.
EDIT: Norman Richards from the Seam team has just posted a blog article about Seam and JAX-RS. Looks fantastic and probably more what you are after than Jersey.
I had a look at Jersey last week and was amazed at how little code you need. Here's a little guide:
- Download the Jersey Jars and the JAXB Jars (so you can output XML and/or JSON) and add them to your classpath
- Add the Jersey servlet to your
web.xml (in the example below with a
url-pattern
ofservice/*
) - Annotate the Bean that holds your data with JAXB annotations
- Create a Service class with the Jersey annotations.
Here's an example of a Service:
@Path("/users")
public class UsersService {
@GET
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Users getUsers() {
return UserQuery.getUsers();
}
}
Where this is the Users class:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "users")
public class Users {
@XmlElement(name="users")
private List<User> users = new ArrayList<User>();
public List<User> getUsers() {
return this.users;
}
public void setUsers(List<User> users) {
this.users = users;
}
}
And this is the User class:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "user")
public class User implements Serializable {
@XmlElement(name="id")
private long userId;
@XmlElement(name="firstName")
private String firstName;
@XmlElement(name="lastName")
private String lastName;
@XmlElement(name="email")
private String email;
public User() {}
public User(long userId, String firstName, String lastName, String email) {
this.userId = userId;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
//And the getter/setters
}
Then you can access the service at http://yourhost/service/users
It will produce XML or JSON depending on what your client has for it's HTTP Accepts header. Pretty cool huh?
来源:https://stackoverflow.com/questions/961836/webservice-using-seam