简体   繁体   English

用glassfish服务器实现宁静的Web服务

[英]restful webservice with glassfish server

I have created the restful web service with netbeans and glassfish, but when I am trying to hit the rest service my browser shows 404 not found exception. 我已经使用netbeans和glassfish创建了宁静的Web服务,但是当我尝试点击其余服务时,我的浏览器显示404找不到异常。

here is my web.xml: 这是我的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com.pps.rest.service</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>TestServlet</servlet-name>
        <servlet-class>com.pps.servlet.TestServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>TestServlet</servlet-name>
        <url-pattern>/TestServlet</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

and here is the rest service: 这是其余的服务:

@Path("/todo")
public class TodoResource {

    // This method is called if XMLis request
    @GET
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public Todo getXML() {
        Todo todo = new Todo();
        todo.setSummary("This is my first todo");
        todo.setDescription("This is my first todo");
        return todo;
    }

    // This can be used to test the integration with the browser
    @Path("/todo1")
    @GET
    @Produces({MediaType.TEXT_XML})
    public Todo getHTML() {
        Todo todo = new Todo();
        todo.setSummary("This is my first todo");
        todo.setDescription("This is my first todo");
        return todo;
    }

    @Path("greet")
    @GET
    public String doGreet() {
        return "Hello Stranger, the time is " + new Date();
    }

}

when I am hitting the url : 当我点击网址时:

http://localhost:8080/pps/rest/todo/greet 

I am getting 404 on glassfish, I have ensured my glassfish is running by hitting the url of the testservlet. 我在glassfish上收到404,通过点击testservlet的URL确保我的glassfish正在运行。

Probably your problem is related to the Jersey version that is bundled with GAS 4. 您的问题可能与GAS 4随附的Jersey版本有关。

Depends on exact version of your GAS you use some of Jersey 2.x version. 取决于您的GAS的确切版本,您可以使用某些Jersey 2.x版本。 As I can assume from your code you are initializing jersey the way it should be done for version 1.x. 正如我可以从您的代码中假设的那样,您正在初始化jersey的版本1.x。 Take a look at Arun's example here . 这里看看Arun的例子。 You can download sources for the example on that page. 您可以在该页面上下载示例的源代码。 His example is too basic though and does not even have web.xml file at all(because it is a very basic example and web.xml is now optional). 他的示例太基础了,甚至根本没有web.xml文件(因为这是一个非常基本的示例,而web.xml现在是可选的)。 That's why you might also want to take a look at jersey sample applications code. 因此,您可能还想看看jersey示例应用程序代码。 For instance bookstore could be a good example. 例如书店可能是一个很好的例子。

In two words you have to extend org.glassfish.jersey.server.ResourceConfig 用两个词必须扩展org.glassfish.jersey.server.ResourceConfig

@ApplicationPath("/")
public class MyApplication extends ResourceConfig {
       public MyApplication() {
          registerClasses(UsersResource.class);
          register(new JettisonFeature());
       }
}

and provide it as javax.ws.rs.Application parameter for Jersey Servlet or Filter based on your choice. 并根据您的选择将其作为javax.ws.rs.Application参数提供给Jersey Servlet或Filter。 It is going to be filter in bookstore example. 在书店示例中将被过滤。 You will see in the code. 您将在代码中看到。 There are plenty examples in the maven repo explaining more advanced stuff Maven回购中有很多示例 ,它们解释了更高级的内容

    <filter>
        <filter-name>org.glassfish.jersey.examples.bookstore.webapp.MyApplication</filter-name>
        <filter-class>org.glassfish.jersey.servlet.ServletContainer</filter-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>org.glassfish.jersey.examples.bookstore.webapp.MyApplication</param-value>
        </init-param>
        <!-- pass to next filter if Jersey/App returns 404 -->
        <init-param>
            <param-name>jersey.config.servlet.filter.forwardOn404</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>org.glassfish.jersey.examples.bookstore.webapp.MyApplication</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

I think the reason for this is that you have not specified the path correctly in your code. 我认为这是因为您没有在代码中正确指定路径。 All I see is paths like /todo , /todo1, /greet. 我所看到的只是/ todo,/ todo1,/ greet之类的路径。 What you are looking for is /todo/greet, so this should be specified in the WebService method @Path annotation. 您要查找的是/ todo / greet,因此应在WebService方法@Path注释中指定。

Annotate the method that you want to use as webservice like this : @Path("/todo/greet") 像这样注释要用作Web服务的方法:@Path(“ / todo / greet”)

The reason you are getting 404 is that there is no method in your class with the path /todo/greet. 得到404的原因是您的类中没有路径/ todo / greet的方法。

Hope this helps! 希望这可以帮助!

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM