2016-03-31 49 views
0

我有我的服務;如何在狂野的v 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: RESTEASY: 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) 

任何人都可以幫助我,我做錯了什麼?

+1

根據定義,您的WAR上下文根指向* http:// localhost:8080/mywar/*。你在做什麼,使用* http:// localhost:8080/mywar/rest/fileservice *通過瀏覽器訪問你的REST服務,我不認爲這就是你想要的。 – aribeiro

+0

@aribeiro如果我打電話像http:// localhost:8080/mywar我得到「禁止」的消息。所以,你說我能夠訪問我的網絡應用程序? – Ratha

+0

@aribeiro如果我嘗試訪問我的休息服務,如http:// localhost:8080/mywar/rest/fileservice/ab/swd/sadwd/asas,我也沒有在服務器控制檯發現任何異常,但瀏覽器顯示「 NotFound「 – Ratha

回答

1

您無法從Web瀏覽器訪問該資源,因爲您已將其設置爲POST方法。您需要使用可發送POST請求的工具,並提供您已定義的/{path}/{filename}/{source}/{client}參數。

+0

我嘗試了一個簡單的GET調用http:// localhost:8080/mywar/rest/fileservice/id,但仍未找到。添加GET方法爲; @GET \t @Path(「/ {id}」) \t public String getUser(@PathParam(「id」)String id){ – Ratha

0

根據JAX-RS documentation@PathParam應該用於GET請求。

對於POST請求,您應該爲完整的實體bean使用JSON或JAXB,或者在意圖檢索符合HTML表單指定的編碼的信息時使用@FormParamThis答案,由@ D.Shawley,可能闡明你在這個問題上更多。

以你張貼的基礎代碼,下面是一個簡單的工作示例:

所有REST服務

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

基礎URI 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() {} 
} 

你的戰爭的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()); 
    } 
}