I am trying to implement a REST server API using Java-ee after this . Instead of Glassfish, I use Tomcat.
I could create 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 see a smiling face. But when I try to access the api path ( http: // localhost: 9080 / api / recommend / all ), I also get the face. If I delete the servlet class, I get 404 error. I suppose I need something else to automatically create the api, but I don't know what.
Can someone tell me what is missing? What should I do?
Update: In Intellij 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 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 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>
source