Java JAX-RS,玻璃鱼,月食。一个简单的web服务不需要';行不通
我试图在我的机器上运行一个简单的“Hello World”RESTful web服务。我使用Eclipse开普勒和GlassFish 4.0。我能够部署该服务,并在GlassFish的管理页面上看到它,但当我尝试访问它时,我得到以下错误:“HTTP状态404-未找到” 在此,简单服务的代码为:Java JAX-RS,玻璃鱼,月食。一个简单的web服务不需要';行不通,java,eclipse,rest,glassfish,Java,Eclipse,Rest,Glassfish,我试图在我的机器上运行一个简单的“Hello World”RESTful web服务。我使用Eclipse开普勒和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.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) {
}
}
import javax.ws.rs.Consumes;
导入javax.ws.rs.GET;
导入javax.ws.rs.PUT;
导入javax.ws.rs.Path;
导入javax.ws.rs.products;
导入javax.ws.rs.core.Context;
导入javax.ws.rs.core.UriInfo;
@路径(“你好”)
公共级希腊森林{
@抑制警告(“未使用”)
@上下文
私有信息上下文;
/**
*默认构造函数。
*/
公共希腊林{
//TODO自动生成的构造函数存根
}
/**
*检索HelloRest实例的表示形式
*@返回字符串的实例
*/
@得到
@生成(“应用程序/xml”)
公共字符串getXml(){
//TODO返回正确的表示对象
返回“hellorest”;
}
/**
*更新或创建HelloRest实例的PUT方法
*@param资源的内容表示形式
*@返回包含更新或创建的资源内容的HTTP响应。
*/
@放
@使用(“应用程序/xml”)
公共void putXml(字符串内容){
}
}
为了访问该服务,我尝试以下URL:http://127.0.0.1:8080/hello-rest/hello
,其中hello rest
是Eclipse项目的名称和GlassFish的管理页面建议的根路径。@GET
@GET
@Produces("application/xml")
@path("/getxml")
public String getXml() {
// TODO return proper representation object
return "<greeting>Hello REST</greeting>";
}
Your url should be http://localhost:8080/hello-rest/hello/getxml
@生成(“应用程序/xml”)
@路径(“/getxml”)
公共字符串getXml(){
//TODO返回正确的表示对象
返回“hellorest”;
}
您的url应该是http://localhost:8080/hello-rest/hello/getxml
您应该在方法级别提到路径,以便将请求重定向到适当的方法
- 您可以使用SOAPUI测试服务。请确保您选择了正确的方法
- 方法为“GET”
- 端点为“主机名:8080”
- 资源为“hello rest/hello/getxml” 如果你面对同样的问题。请在tomcat控制台中附加更多日志/异常
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("your-base-url")
public class ApplicationConfig extends Application {
}
您可以使用@ApplicationPath注释来定义基本url。
然后,您可以通过
http://127.0.0.1:8080/hello-rest/your base url/hello
请将您的web.xml
添加到问题中。