Reputation: 757
I'm trying to build a Java web-app in Intellij, using Wildfly as Application server. In my web app, i'm trying to configure a module for restful webservices (with RestEasy library) but when I try to test my restful webservice (as post method), i receive the message "HTTP method POST is not supported by this URL". I don't understand why I see this message...
I launch my rest service as follow:
http://localhost:8080/rest/email/myName/[email protected]/myMessage
In my pom.xml I added the needed library:
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>javax.ejb</groupId>
<artifactId>ejb-api</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.19.Final</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
In my web.xml I added this:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Smoney-RS</display-name>
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.myapp.rs.api</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
Finally, my java class for rest api:
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/email")
public class EmailRestfulImpl
{
@POST
@Path("/{name}/{address}/{message}")
@Produces(MediaType.APPLICATION_JSON)
public Response sendContactUs(@PathParam("name") String name, @PathParam("address") String address, @PathParam("message") String message)
{
EmailRequest emailRequest = new EmailRequest();
emailRequest.setName(name);
emailRequest.setEmail(address);
emailRequest.setMessage(message);
return Response.status(Response.Status.OK).entity(emailRequest).build();
}
}
What am i doing wrong?
Upvotes: 2
Views: 10413
Reputation: 757
I solved the issue. The context path was not correct. I was trying to call this:
http://localhost:8080/rest/email/myName/[email protected]/myMessage
The correct path was this:
http://localhost:8080/MYAPP-SNAPSHOT-1.0/rest/email/myName/[email protected]/myMessage
I solved adding the context-path tag in my jboss-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 5.0//EN"
"http://www.jboss.org/j2ee/dtd/jboss-web_5_0.dtd">
<jboss-web>
<context-root>/rest</context-root>
</jboss-web>
And finally, I added the context-param in my web.xml as follow:
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/</param-value>
</context-param>
I hope can be helpful for you!
Upvotes: 3