1

我尝试了以下工作正常的方式

package test;

import static org.junit.Assert.assertEquals;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Application;

    import org.glassfish.jersey.server.ResourceConfig;
    import org.glassfish.jersey.test.DeploymentContext;
    import org.glassfish.jersey.test.JerseyTest;
    import org.glassfish.jersey.test.ServletDeploymentContext;
    import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory;
    import org.glassfish.jersey.test.spi.TestContainerException;
    import org.glassfish.jersey.test.spi.TestContainerFactory;
    import org.junit.Test;

    import com.mkyong.rest.HelloWorldService;

    public class HelloWorldServiceTest extends JerseyTest{

        @Path("hello")
        public static class HelloResource {
            @GET
            public String getHello() {
                return "Hello World!";
            }
        }

        @Override
        protected Application configure() {
            return new ResourceConfig(HelloResource.class);
        }

        @Override
        protected TestContainerFactory getTestContainerFactory() throws TestContainerException {
            return new GrizzlyWebTestContainerFactory();
        }

        @Override
        protected DeploymentContext configureDeployment() {
            return ServletDeploymentContext.forPackages(
                    getClass().getPackage().getName()).build();
        }

        @Test
        public void testSingleNode() throws Exception {
            final String hello = target("hello").request().get(String.class);
            assertEquals("Hello World!", hello);
        }
        }

当我试图用这个测试类和包之外的资源替换资源时,如下所示

   package test;

import static org.junit.Assert.assertEquals;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Application;

import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.DeploymentContext;
import org.glassfish.jersey.test.JerseyTest;
import org.glassfish.jersey.test.ServletDeploymentContext;
import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory;
import org.glassfish.jersey.test.spi.TestContainerException;
import org.glassfish.jersey.test.spi.TestContainerFactory;
import org.junit.Test;

import com.mkyong.rest.HelloWorldService;

public class HelloWorldServiceTest extends JerseyTest{

    @Path("hello")
    public static class HelloResource {
        @GET
        public String getHello() {
            return "Hello World!";
        }
    }

    @Override
    protected Application configure() {
        return new ResourceConfig(HelloWorldService.class);
    }

    @Override
    protected TestContainerFactory getTestContainerFactory() throws TestContainerException {
        return new GrizzlyWebTestContainerFactory();
    }

    @Override
    protected DeploymentContext configureDeployment() {
        return ServletDeploymentContext.forPackages(
                getClass().getPackage().getName()).build();
    }

    @Test
    public void testSingleNode() throws Exception {
        final String hello = target("hello").path("Test").request().get(String.class);
        assertEquals("Jersey say : Test", hello);
    }
    }

HelloWorldService.java

package com.mkyong.rest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/hello")
public class HelloWorldService {

    @GET
    @Path("/{param}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getMsg(@PathParam("param") String msg) {

        String output = "Jersey say : " + msg;

        return Response.status(200).entity(output).build();

    }

}

RestApplication.java

      package rest;

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.core.Application;

import com.mkyong.rest.HelloWorldService;

public class RestApplication extends Application{

      @Override
        public Set<Class<?>> getClasses() {
            Set<Class<?>> s = new HashSet<Class<?>>();

            s.add(HelloWorldService.class);
            return s;
        }


}

web.xml

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>Restful Web Application</display-name>

     <servlet>
    <servlet-name>rs-servlet</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
    <param-name>javax.ws.rs.Application</param-name>
    <param-value>rest.RestApplication</param-value>
</init-param>
  </servlet>
  <servlet-mapping>
  <servlet-name>rs-servlet</servlet-name>
  <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

</web-app>

我有一个例外

   javax.ws.rs.NotFoundException: HTTP 404 Not Found
at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:917)
at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:770)
at org.glassfish.jersey.client.JerseyInvocation.access$500(JerseyInvocation.java:90)
at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:671)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:423)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:667)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:396)
at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:296)
at test.HelloWorldServiceTest.testSingleNode(HelloWorldServiceTest.java:48) 

哪位大神能纠正一下我哪里出错了...

4

1 回答 1

2

看这个

@Override
protected DeploymentContext configureDeployment() {
    return ServletDeploymentContext.forPackages(
            getClass().getPackage().getName()).build();
}

forPackages就是说要扫描什么包@Path@Provider注释类,并自动注册它们。它在第一个示例中起作用的原因是资源类(带有注释的类@PathgetClass().getPackage().getName()包中。

这种方法

@Override
protected Application configure() {
    return new ResourceConfig(HelloWorldService.class);
}

永远不会被调用,因为你覆盖了getDeploymentContext(). 通常,如果您不覆盖该方法,它将调用该configure方法以将应用程序注册到部署上下文。但你永远不会这样做。

因此forPackages,您应该使用ServletDeploymentContext.builder接受Application类的方法之一,而不是 ,

return ServletDeploymentContext.builder(configure()).build()

return ServletDeploymentContext.builder(RestApplication.class).build();

或者

@Override
public ResourceConfig configure() {
    return new ResourceConfig(HelloWorldService.class);
}

@Override
public ServletDeploymentContext configureDeployment() {
    return SerlvetDeploymentContext.forServlet(new ServletContainer(configure));
}

需要注意的一点是,您只需要覆盖getTestContainerFactoryconfigureDeployment如果您想设置 servlet 环境。这意味着,您需要在应用程序中使用 servlet API,例如HttpServletRequestServletContext等。如果您不需要 servlet 环境,只需覆盖configure就足够了。

于 2016-06-19T01:36:42.517 回答