Netbeans无法找到我的宁静网络服务

时间:2014-10-01 19:16:07

标签: java web-services rest java-ee jax-rs

我正在使用Netbeans和Glassfish。我最近创建了一个新的Maven Web应用程序。我添加了一个新的WebServlet,它运行正常。

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

现在我想添加一些RESTful Web服务。所以我又增加了一个课程。

@Path("/yeahbuddy/")
public class YeahbuddyResource {

@GET
public String getAllYeahbuddies() {

    return "WE GOT GOT";

}

}

然后我清理,构建,运行服务器,......没有。我尝试点击以下网址:

http://localhost:8080/myapp/webresources/yeahbuddy/
http://localhost:8080/myapp/yeahbuddy/
http://localhost:8080/myapp/resources/yeahbuddy/

我得到的只有404.然后我想,我一定错过了一些东西,并使用向导从模式中添加RESTful Web服务。同样的问题。

我觉得我缺少一些配置选项,比如注释不够,或者REST注释正在与WebServlet注释作斗争。

思想?

编辑:答案是添加以下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> 

它开始为以下网址工作:

http://localhost:8080/myapp/api/yeahbuddy/

1 个答案:

答案 0 :(得分:1)

由于您打算在Glassfish上使用Restful,我假设您将使用Jersey实现的Jax-RS,因此您必须在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>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> 

因此,例如,带有/ yeahbuddy / ...的网址发出的每个请求都会映射到您的Rest类。

看一下这个很好的教程:

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