I am using Netbeans and Glassfish. I recently created a new Maven Web Application. I added a new WebServlet, it worked fine.
@WebServlet(name = "DbTest", urlPatterns = {"/DbTest"})
public class DbTest extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException {
response.setContentType("text/html;charset=UTF-8");
Session session = StartupBean.sessionFactory.openSession();
session.beginTransaction();
.....
Now I want to add some RESTful web services. So I added another class.
@Path("/yeahbuddy/")
public class YeahbuddyResource {
@GET
public String getAllYeahbuddies() {
return "WE GOT GOT";
}
}
Then I clean, built, ran the server, and....nothing. I tried hitting the following URLs:
http://localhost:8080/myapp/webresources/yeahbuddy/
http://localhost:8080/myapp/yeahbuddy/
http://localhost:8080/myapp/resources/yeahbuddy/
All I ever get is 404. Then I thought, I must have missed something and used the wizard to add a RESTful web service from a pattern. Same problem.
I feel like there is some config option I am missing, like the annotations aren't enough, or the REST annotations are fighting the WebServlet annotations.
Thoughts?
EDIT: The answer was adding the following web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>test</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- Register resources and providers under com.vogella.jersey.first package. -->
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.myapp.resources</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
And it started working for the following URL:
http://localhost:8080/myapp/api/yeahbuddy/