Reputation: 9692
I have my service as;
@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) {
.......
}
and have an jars activator;
@ApplicationPath("rest")
public class JaxRsActivator extends Application{
}
I do not have web.xml.
When i try to access this war from the browser; i use;
http://localhost:8080/mywar/rest/fileservice
But I'm getting;
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)
Can anybody help me what I'm doing wrong?
Upvotes: 0
Views: 350
Reputation: 4202
According to the JAX-RS documentation the @PathParam
should be used for GET
requests.
For POST
requests, one should either consume JSON or JAXB, for full entity beans, or make use of the @FormParam
when the intention is to retrieve information that conforms to the encoding specified by HTML forms. This answer, by @D.Shawley, may elucidate you more on this subject.
Taking the code you've posted as basis, the following is a simple working example:
The Base URI for all REST services
@ApplicationPath("/rest")
public class JaxRsActivator extends Application {}
The REST Service
@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();
}
}
The entity
@XmlRootElement(name="file")
public class MyFile {
@XmlElement
public String id;
@XmlElement
public String name;
public MyFile() {}
}
Your WAR pom.xml file should look like this:
<?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>
Then, in order to test your REST service, you could create a simple client that could look like this:
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());
}
}
Upvotes: 0
Reputation: 17770
You can't access that resource from a web browser because you have it set as a POST
method. You'd need to use a tool that can send POST requests and provide the /{path}/{filename}/{source}/{client}
parameters you have defined.
Upvotes: 1