在GlassFish服务器中找不到JAX-RS资源 [英] JAX-RS Resource not found in GlassFish Server

查看:156
本文介绍了在GlassFish服务器中找不到JAX-RS资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图使用NetBeans IDE创建一个简单的Restful WebService。
我的Java EE版本是:Java EE 7 Web。



我创建了一个新的Java Web应用程序,设置了ContexPath: / DukesAgeService

现在,运行我的应用程序,浏览器在<:code> Index.html 页面显示: > http:// localhost:8080 / DukesAgeService /


所以,一切正常。



然后,我尝试使用RESTful Web服务向导创建一个简单的restful资源。

所以,我创建了这个类:

  package firstcup.webservice; 

import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PUT;
$ b $ **
* REST Web服务
*
* @author nolanof
* /
@Path(dukesAge)
public class DukesAgeResource {

@Context
private UriInfo context;
$ b $ **
*创建DukesAgeResource的新实例
* /
public DukesAgeResource(){
}

/ **
*检索firstcup.webservice.DukesAgeResource实例的表示形式
* @返回java.lang.String实例
* /
@GET
@Produces(text / plain)
public String getText(){
returnhello world;




$ b $ p
$ b

但是运行我的应用程序,在url:
http:// localhost:8080 / DukesAgeService / dukesAge
我找到一个404找不到的页面。



我证明了任何具有/ dukesAge url的传入获取请求由 DukesAgeResource 类处理 getText 方法。什么是错的?



谢谢

解决方案

JAX-RS应用程序servlet。你可以在 web.xml 中定义它,或者如果你想去xml-less,你可以使用 Application 子类。 IMO最简单的方法就是使用 @ApplicationPath 注解的 Application 子类。一个servlet将被创建,servlet路径将被设置为注释中的值。类似于

  @ApplicationPath(/ rest)
公共类RestApplication扩展应用程序{
//所有请求范围的资源和提供者
@Override
public Set< Class<>> getClasses(){
Set< Class<?>> classes = new HashSet<>();
classes.add(DukesAgeResource.class);
返回类;
}

//所有单例资源和提供者
@Override
public Set< Object> getSingletons(){
Set< Object> singletons = new HashSet<>();
返回单身人士;


然后资源应该通过 p>


http:// localhost:8080 / DukesAgeService / rest / dukesAge


还有其他方法,但这是可移植的方式。 Glassfish使用Jersey,但在Netbeans中从头开始创建Java EE Web应用程序只会导入编译时Java EE标准类(无泽西依赖项)。所以上面的确是你的赌注。



你可以在 Jersey Documentation 。对于某些选项,您可能需要添加一些Jersey编译时的依赖关系。这就是我刚才提到上述的原因。如果你指定JAX-RS servlet路径为 / * C $ C>。这将与提供静态资源(如HTML页面)的默认servlet相冲突。这就是为什么我将它设置为 / rest






更新



在JAX-RS规范中还指出,如果在 getClasses() getSingletons(),应该发生隐式包扫描。 (提供者)使用 @Provider 注解的类将默认添加为单例,并且使用 @Path 注释的资源类将被添加是每个请求对象(意味着每个请求都会创建一个新对象)。所以你可以选择只是有

pre $ @ApplicationPath(/ rest)
public class RestApplication extends Application {
//留空
}

它的工作原理应该是一样的。 p>

I've been trying to create a simple Restful WebService, using NetBeans Ide.
My Java EE Version is: Java EE 7 Web.

I created a new Java Web Application, setting this ContexPath: /DukesAgeService.

Now, running my application, browser display my Index.html page at:

http://localhost:8080/DukesAgeService/

so, everything works fine.

Then, I tried to create a simple restful resource using the RESTful Web Service Wizard.

So, I created this class:

package firstcup.webservice;

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

/**
 * REST Web Service
*
* @author nolanof
*/
@Path("dukesAge")
public class DukesAgeResource {

@Context
private UriInfo context;

/**
 * Creates a new instance of DukesAgeResource
 */
public DukesAgeResource() {
}

/**
 * Retrieves representation of an instance of firstcup.webservice.DukesAgeResource
 * @return an instance of java.lang.String
 */
@GET
@Produces("text/plain")
public String getText() {        
    return "hello world";
}
}

But running my application, at url: http://localhost:8080/DukesAgeService/dukesAge I get a 404-not found page.

I exptected that any incoming get request that has the url of "/dukesAge" was handled by DukesAgeResource class getText method. Whats' wrong?

Thanks

解决方案

You're probably missing the JAX-RS application servlet. You can either define it in the web.xml or if you want to go xml-less, you can use a Application subclass. The easiest way IMO is just to use the Application subclass annotated with @ApplicationPath. A servlet will be created and the servlet path will be set the value in the annotation. Something like

@ApplicationPath("/rest")
public class RestApplication extends Application {
    // All request scoped resources and providers
    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> classes = new HashSet<>();
        classes.add(DukesAgeResource.class);
        return classes;
    }

    // all singleton resources and providers
    @Override
    public Set<Object> getSingletons() {
        Set<Object> singletons = new HashSet<>();
        return singletons;
    }
}

Then the resource should be access the resource via

http://localhost:8080/DukesAgeService/rest/dukesAge.

There are other ways, but this is the portable way. Glassfish uses Jersey, but creating a Java EE web application from scratch in Netbeans will only import compile time Java EE standard classes (no Jersey dependencies). So the above is really your bet to start off with.

You can see other deployment options at the Jersey Documentation. For some of the options, you may need to add some Jersey compile-time dependencies. That's why I just mentioned the above. No other jars needed.

Another thing that would cause a 404, is if you specify the JAX-RS servlet path as /*. This will conflict with the default servlet that serves the static resources like your html pages. That's why I set it to /rest.


UPDATE

It is also stated in the JAX-RS spec that if there are empty sets returned in the getClasses() and getSingletons(), implicit package scanning should occur. (provider) Classes annotated withe @Provider will by default be added as singletons and a resource classes annotated with @Path will be per-request objects (meaning a new object is created each request). So you could alternatively just have

@ApplicationPath("/rest")
public class RestApplication extends Application {
    // Left empty
}

And it should work just the same.

这篇关于在GlassFish服务器中找不到JAX-RS资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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