JAX-RS,GlassFish,Eclipse。一个简单的Web服务不起作用 [英] JAX-RS, GlassFish, Eclipse. A simple web service doesn't work

查看:137
本文介绍了JAX-RS,GlassFish,Eclipse。一个简单的Web服务不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的机器上运行一个简单的Hello WorldRESTful Web服务。我使用Eclipse Kepler和GlassFish 4.0。我可以部署该服务,并且在GlassFish的管理页面上看到它,但是当我尝试访问它时,我收到以下错误:HTTP状态404 - 未找到。



这里的简单服务的代码:

  import javax.ws.rs.Consumes; 
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;

@Path(hello)
public class HelloRest {
@SuppressWarnings(unused)
@Context
private UriInfo context;

/ **
*默认构造函数。
* /
public HelloRest(){
// TODO自动生成的构造函数存根
}

/ **
*检索表示的一个实例的HelloRest
* @返回一个String
* /
@GET
@Produces(application / xml)
public String getXml() {
// TODO返回正确的表示对象
return< greeting> Hello REST< / greeting>;
}

/ **
*用于更新或创建HelloRest的实例的PUT方法
* @param资源的内容表示
* @return具有更新或创建资源内容的HTTP响应。
* /
@PUT
@Consumes(application / xml)
public void putXml(String content){
}

}

为了访问该服务,我尝试以下URL: http ://127.0.0.1:8080 / hello-rest / hello ,其中 hello-rest 是Eclipse项目的名称和根路径建议由GlassFish的管理页面。

解决方案

你的代码似乎没问题,所以问题最有可能是你没有定义了您的服务的基本URL。你需要告诉JAX-RS(Jersey是GlassFish中的实现),它必须拦截哪个url模式作为你的端点(base url)。



实现这一点的一种方法是使用可以添加到项目中的任何包的应用程序类(您可以另外在web.xml文件中定义必需的,我将不会介绍)。以下是代码:

  import javax.ws.rs.ApplicationPath; 
import javax.ws.rs.core.Application;

@ApplicationPath(your-base-url)
public class ApplicationConfig extends Application {

}
/ pre>

您可以使用@ApplicationPath注释来定义您的基本URL。
然后您可以访问您的服务 http://127.0.0.1:8080/hello-rest/your-base-url/hello


I am trying to run a simple "Hello World" RESTful web service on my machine. I use Eclipse Kepler and GlassFish 4.0. I am able to deploy the service and I see it on the admin pages of GlassFish but when I try to access to it I get the following error: "HTTP Status 404 - Not Found".

Herein the code of the simple service:

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;

@Path("hello")
public class HelloRest {
    @SuppressWarnings("unused")
    @Context
    private UriInfo context;

    /**
     * Default constructor. 
     */
    public HelloRest() {
        // TODO Auto-generated constructor stub
    }

    /**
     * Retrieves representation of an instance of HelloRest
     * @return an instance of String
     */
    @GET
    @Produces("application/xml")
    public String getXml() {
        // TODO return proper representation object
        return "<greeting>Hello REST</greeting>";
    }

    /**
     * PUT method for updating or creating an instance of HelloRest
     * @param content representation for the resource
     * @return an HTTP response with content of the updated or created resource.
     */
    @PUT
    @Consumes("application/xml")
    public void putXml(String content) {
    }

}

In order to access to the service I try the following URL: http://127.0.0.1:8080/hello-rest/hello, where hello-rest is the name of the Eclipse project and the root path suggested by the admin page of GlassFish.

解决方案

Your code seems to be ok, so the problem most likely is that you have not defined the base url for your service. You need to tell JAX-RS (Jersey is the implementation in GlassFish) which url pattern it must intercept as your endpoint (base url).

One way of achieving this is using an Application Class which can be added to any package in the project (you could alternatively define the necessary in a web.xml file which I won't cover). The following is the code:

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("your-base-url")
public class ApplicationConfig extends Application {

}

You use the @ApplicationPath annotation to define your base url. You can then access your service at http://127.0.0.1:8080/hello-rest/your-base-url/hello

这篇关于JAX-RS,GlassFish,Eclipse。一个简单的Web服务不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆