0

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/
bulltorious
  • 7,769
  • 4
  • 49
  • 78

1 Answers1

1

Since you intend to use Restful on your Glassfish i assume you will use Jersey implementation of Jax-RS, so you will have to define it in your web.xml, something like this:

<?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>com.vogella.jersey.first</display-name>
 <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>paramValue</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/yeahbuddy/*</url-pattern>
  </servlet-mapping>
</web-app> 

So, every request made by urls with /yeahbuddy/... will be mapped to your Rest class, for example.

Have a look at this nice tutorial:

http://www.vogella.com/tutorials/REST/article.html

Bruno Franco
  • 2,028
  • 11
  • 20
  • is there a way to do this without using a web.xml ? Just curious, the book I have been using never mentions it, but I believe it is not using glassfish. – bulltorious Oct 01 '14 at 21:16
  • Have a look at this topic: http://stackoverflow.com/questions/9373081/how-to-set-up-jax-rs-application-using-annotations-only-no-web-xml – Bruno Franco Oct 02 '14 at 11:09