我正在尝试使用Glassfish 4创建REST服务。我有一个SOAP服务和Servlet正常工作,但无法使REST工作。
这是我简单的DAO:
let i = Int(s) ?? 0
这是我的REST课程
@Remote
@WebService
public interface IPersonDao {
public void createAndSave(String fn, String ln, String eid);
public void persist(Person p);
public Collection<Person> getAllPeople();
public Person getPerson(Integer id);
public void delete(Person p);
public void deleteById(Integer id);
}
@Stateless
@Remote(IPersonDao.class)
@Named("MemoryPersonDao")
@WebService(endpointInterface = "dummy.server.IPersonDao")
public class PersonDao implements IPersonDao {
Map<Integer, Person> people;
int id_seq = 1;
/**
*
*/
public PersonDao() {
people = new java.util.HashMap<Integer, Person>();
/*createAndSave("Mike", "Jones", "2234");
createAndSave("Joe", "Smith", "22314");*/
}
@PostConstruct
public void postConstruct() {
createAndSave("Mike", "Jones", "2234");
createAndSave("Joe", "Smith", "22314");
}
private int nextId() {
return id_seq++;
}
/*
* (non-Javadoc)
*
* @see dummy.server.IPersonDao#createAndSave(java.lang.String,
* java.lang.String, java.lang.String)
*/
@Override
public void createAndSave(String fn, String ln, String eid) {
Person p = new Person();
p.setFirstName(fn);
p.setLastName(ln);
p.setEmployeeId(eid);
p.setId(nextId());
persist(p);
}
@Override
public Person getPerson(Integer id) {
return people.get(id);
}
/*
* (non-Javadoc)
*
* @see dummy.server.IPersonDao#persist(dummy.server.Person)
*/
@Override
public void persist(Person p) {
if (p.getId() != null) {
p.setId(nextId());
}
people.put(p.getId(), p);
}
/*
* (non-Javadoc)
*
* @see dummy.server.IPersonDao#getAllPeople()
*/
@Override
public Collection<Person> getAllPeople() {
return people.values();
}
/* (non-Javadoc)
* @see dummy.server.IPersonDao#delete(dummy.server.Person)
*/
@Override
public void delete(Person p) {
deleteById(p.getId());
}
/* (non-Javadoc)
* @see dummy.server.IPersonDao#deleteById(java.lang.Integer)
*/
@Override
public void deleteById(Integer id) {
people.remove(id);
}
我还有一个工作的servlet和一个可以工作的SOAP服务(我可以看到?wsdl和?Tester页面)。什么是我的REST URL?我在日志中看到了这一点:
@ApplicationPath("api")
public class PersonRestService extends Application {
@EJB
IPersonDao personDao;
/**
*
*/
@Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(PersonRestService.class);
return classes;
}
public PersonRestService() {
}
@GET
@Path("people")
public String test() {
return "TEST OK";
}
public Collection<Person> getPeople(){
System.err.println("Getting all people!");
return personDao.getAllPeople();
}
}
这是我的web.xml(由于使用注释而不需要):
2015-07-07T10:17:32.052-0400|Info: Registering the Jersey servlet application, named dummy.server.PersonRestService, at the servlet mapping /api/*, with the Application class of the same name.
这是我的glassfish-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>GB</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
但是,我无法弄清楚我的REST URL。有谁知道这里给出了什么?