2

I'm trying to implement a REST Server API using Java-ee following this tutorial. Instead of Glassfish, I use Tomcat.

I could develop a servlet

@WebServlet(name = "hello", urlPatterns = "/")
public class HelloWorld extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("=)");
    }
}

And join http://localhost:9080/ I can see the smiling face. But when I try to access to the api path (http://localhost:9080/api/recommend/all) I also get the face. If I remove the servlet class, I get a 404 error. I suppose I need something else to build automatically the api but I don't know what.

Could someone tell my what is missing? What should I do?

Update: In Intellij's Java Enterprise View I see:

Web > HelloWorld
RESTful WS > recommend > all

These are my api classes:

@ApplicationPath("/api")
public class REST_Config extends Application {
}

And the specific method

@Path("recommend")
public class RecommenderController {

    @Path("/all")
    @GET
    @Produces("application/json")
    public JsonArray getAll(){
        JsonArrayBuilder builder = Json.createArrayBuilder();

        builder.add(Json.createObjectBuilder().add("1", "2.5"));

        return builder.build();
    }
}

And the pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>TestREST</groupId>
    <artifactId>TestREST</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>TestREST</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
            </plugin>

        </plugins>
    </build>
</project>
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Daniel Argüelles
  • 2,229
  • 1
  • 33
  • 56

1 Answers1

6

"Instead of Glassfish, I use Tomcat."

Look at this

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>7.0</version>
</dependency>

This is nothing more than basically a bunch of interfaces for the EE spec. There is no implementation. Java EE servers will have the implementation. Tomcat is not an EE server. The only part of the EE spec it will definitely implements is the Servlet Specification. You are trying to work with the JAX-RS spec, where Tomcat for sure by default does not have an implementation for. So you need to add that implementation.

The easiest IMO to get started with, is Jersey. You can simple add this dependency

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.17</version>
</dependency>

And it will get you up and running. Keep the Jersey User Guide handy. It will come in use.

Also I don't know what JsonArray is, but what will happen when you run this is you will get some error similar to "No MessageBodyWriter found for JsonArray and media type application/json". You need to provider. If you are going to use the Java EE JSONP API, then you should add the this provider

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-processing</artifactId>
    <version>2.17</version>
</dependency>

As you get to working alot with JSON, you wil find this API to be difficult to maintain. I'd recommend using Jackson. If you don't already know it, I'd suggest learning it. It offers simple POJO to JSON mapping. For Jackson, you can add this dependency

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.17</version>
</dependency>
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720