我正在尝试在我的Java应用程序中创建一个restful服务。到目前为止,我有一个POJO,一个Entitymanagement类,一个“Wrapper”和一个通过Rest访问数据的类。我正在使用wildfly 10并且没有添加任何依赖项(除了休眠)。
POJO
import java.sql.Date;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;;
@Entity
public class Event {
private static final long serialVersionUID = 2L;
@Id
@GeneratedValue
private int id;
private Date eventDate;
private double price;
@ManyToMany(fetch = FetchType.LAZY)
private List<Visitor> visitors;
@OneToOne
private Organisation orga;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Organisation getOrga() {
return orga;
}
public void setOrga(Organisation orga) {
this.orga = orga;
}
public Date getEventDate() {
return eventDate;
}
public void setEventDate(Date eventDate) {
this.eventDate = eventDate;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public List<Visitor> getVisitors() {
return visitors;
}
public void setVisitors(List<Visitor> visitors) {
this.visitors = visitors;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
实体管理
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import lele.core.entities.Event;
import lele.core.entities.Organisation;
@Stateless
public class Loader {
@PersistenceContext
private EntityManager em;
public List<Event> getAllEvents()
{
TypedQuery<Event> query = em.createQuery("select e from Event e", Event.class);
return query.getResultList();
}
包装
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
import lele.core.entities.Event;
@XmlRootElement(name="events")
public class EventWrapper {
private List<Event> list;
public EventWrapper() {
}
public List<Event> getList() {
return list;
}
public void setList(List<Event> list) {
this.list = list;
}
}
API-类
@Path("/appmgr")
@Consumes({ "application/json" })
@Produces({ "application/json" })
public class Appmgr {
@EJB
private Loader ld;
@EJB
private Saver sv;
@GET
@Path("events")
@Produces("application/json")
public Response getEvents() {
EventWrapper wrapper = new EventWrapper();
wrapper.setList(ld.getAllEvents());
return Response.status(200).entity(wrapper).build();
}
我错过了什么吗?任何帮助都会被贬低,因为这是我第一次使用Restful服务。
也许是配置错误......?
编辑:
答案 0 :(得分:4)
您需要在web.xml描述符中添加JAX-RS激活器类或配置Web应用程序。 App Server需要知道应该查找JAX-RS注释类以及哪些URL应该绑定它们。
Activator是最简单的解决方案。只需将以下类添加到您的应用中:
@ApplicationPath("/rest")
public class JaxRsActivator extends Application {
/* class body intentionally left blank */
}
第二个解决方案是在web.xml中激活RESTful端点。您需要在web.xml中添加以下内容:
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
有关此内容的更多信息,例如:https://docs.jboss.org/author/display/WFLY8/JAX-RS+Reference+Guide
还有一个提示:确保使用正确的URL来处理您的服务。在上面的配置中,如下所示:WAR_NAME / rest / appmgr /....
答案 1 :(得分:0)
解决方案:
编辑web.xml并在pom.xml中添加依赖项:
的web.xml
<web-app version="3.1"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
metadata-complete="false">
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
的pom.xml
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.0.Final</version>
</dependency>
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.19</version>
</dependency>
</dependencies>