如何在狂野的v 10中访问已部署的战争?

时间:2016-03-31 05:18:07

标签: java jboss wildfly wildfly-10

我的服务是;

@Path("/fileservice/")
public class FileService {
@POST
    @Path("/{path}/{filename}/{source}/{client}")
    public Response getFilePath(
            @PathParam("path") String filePath,
            @PathParam("filename") String fileName,
            @PathParam("source") String fileSource,
            @PathParam("client") String clientId) {

.......

}

并有一个罐子活化剂;

@ApplicationPath("rest")
public class JaxRsActivator extends Application{

}

我没有web.xml。

当我尝试从浏览器访问此战争时;  我用;

http://localhost:8080/mywar/rest/fileservice

但我得到了;

 6:09:28,215 ERROR [org.jboss.resteasy.resteasy_jaxrs.i18n] (default task-15) RESTEASY002010: Failed to execute: javax.ws.rs.NotFoundException: RESTEASY003210: Could not find resource for full path: http://localhost:8080/mywar/rest/fileservice
        at org.jboss.resteasy.core.registry.SegmentNode.match(SegmentNode.java:114)
        at org.jboss.resteasy.core.registry.RootNode.match(RootNode.java:43)
        at org.jboss.resteasy.core.registry.RootClassNode.match(RootClassNode.java:48)
        at 

org.jboss.resteasy.core.ResourceMethodRegistry.getResourceInvoker(ResourceMethodRegistry.java:445)
        at org.jboss.resteasy.core.SynchronousDispatcher.getInvoker(SynchronousDispatcher.java:257)
        at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:194)
        at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:221)
        at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
        at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
        at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
        at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
        at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
        at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
        at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
        at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
        at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
        at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
        at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
        at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
        at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
        at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
        at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
        at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
        at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
        at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
        at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
        at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
        at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:284)
        at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:263)
        at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
        at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:174)
        at io.undertow.server.Connectors.executeRootHandler(Connectors.java:202)
        at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:793)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)

有人可以帮我解决我做错的事吗?

2 个答案:

答案 0 :(得分:1)

您无法从Web浏览器访问该资源,因为您已将其设置为POST方法。您需要使用可以发送POST请求并提供您已定义的/{path}/{filename}/{source}/{client}参数的工具。

答案 1 :(得分:0)

根据JAX-RS documentation@PathParam应该用于GET次请求。

对于POST个请求,应该使用JSON或JAXB(对于完整的实体bean),或者在意图检索符合HTML表单指定的编码的信息时使用@FormParam 。 @ {.Shawley的This回答可以在这个问题上更多地阐明你。

以您发布的代码为基础,以下是一个简单的工作示例:

所有REST服务的基本URI

@ApplicationPath("/rest")
public class JaxRsActivator extends Application {}

REST服务

@Path("/fileservice")
public class FileService {

    @GET
    @Path("/{id}")
    @Produces("text/html")
    public Response getFileId(@PathParam("id") String fileId) {
        System.out.println(fileId);

        return Response.ok().entity(fileId).build();
    }

    @POST
    @Path("/file")
    @Consumes(MediaType.APPLICATION_XML)
    public Response createFile(MyFile myFile) {
        System.out.println(myFile.id);

        return Response.ok().build();
    }
}

实体

@XmlRootElement(name="file")
public class MyFile {

    @XmlElement
    public String id;

    @XmlElement
    public String name;

    public MyFile() {}
}

您的WAR pom.xml 文件应如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>war-group</groupId>
    <artifactId>war-artifact</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

然后,为了测试您的REST服务,您可以创建一个如下所示的简单客户端:

public class FileServiceClient {

    public static void main(String[] args) throws Exception {
        callRestServiceGET();

        callRestServicePOST();
    }

    protected static void callRestServiceGET() {
        URI uri = null;

        try {
            uri = new URI("http://localhost:8080/war-artifact-0.0.1-SNAPSHOT/rest/fileservice/98765");
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

        Client client = ClientBuilder.newClient();

        Response response = client.target(uri).request().get();
        System.out.println("GET sended, response status: " + response.getStatus());
    }

    protected static void callRestServicePOST() {
        URI uri = null;

        try {
            uri = new URI("http://localhost:8080/war-artifact-0.0.1-SNAPSHOT/rest/fileservice/file");
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

        Client client = ClientBuilder.newClient();

        String xml = "<file><id>1234567</id><name>abc</name></file>";

        Response response = client.target(uri).request().post(Entity.xml(xml));
        System.out.println("POST sended, response status: " + response.getStatus());
    }
}