2

I'm trying to create a restful service in my Java Application. So far i got a POJO, a Entitymanagement Class, a "Wrapper" and a Class to access the Data via Rest. I'm using a wildfly 10 and didn't add any dependencies (except for the hibernate).

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;
    }



}

Entity Management

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();
    }

Wrapper

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- Class

@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();
    }

Am I missing something? Any help would be apreciated since it's my first time working on a Restful service.

Maybe it's a configuration Error...?

Edit:

Rest Services are already active but dont work: enter image description here

Zanidd
  • 133
  • 13

2 Answers2

6

You need to add JAX-RS activator class or configure web app in web.xml descriptor. App Server needs to know that should look for JAX-RS annotaded classes and for which urls should bind them.

Activator is the simplest solution. Just add the following class to your app:

@ApplicationPath("/rest")
public class JaxRsActivator extends Application {
/* class body intentionally left blank */
}

Second solution is to activate RESTful endpoints in your web.xml. You need to add following to your web.xml:

<servlet-mapping>
   <servlet-name>javax.ws.rs.core.Application</servlet-name>
   <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

More on this for example here: https://docs.jboss.org/author/display/WFLY8/JAX-RS+Reference+Guide

And one more tip: be sure that you are using proper URL to your service. In config above will look like this: WAR_NAME/rest/appmgr/....

Piotr Kucia
  • 381
  • 2
  • 9
  • I guess that they are detected by your IDE thanks to annotations, but still You have to configure it to be available on server – Piotr Kucia Feb 21 '16 at 02:02
0

Solution:

Edit web.xml and add dependencies in 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>
Zanidd
  • 133
  • 13
  • Above solution is ok, but note that Wildfly has already built-in restful framework (RESTEasy). You don't have to use another one (Jersey). By doing this you relying on additional dependencies. With build-in solution in Wildfly you don't have to add anything except javaee-api. I edited my answer above and added second way of exposing rest endpoints using plain web.xml instead of Activator class. – Piotr Kucia Feb 21 '16 at 12:47