WildFly中的此URL不支持HTTP方法POST

时间:2016-09-20 21:48:11

标签: java rest maven intellij-idea wildfly-10

我正在尝试使用Wildfly作为应用程序服务器在Intellij中构建Java Web应用程序。 在我的网络应用程序中,我正在尝试为restful webservices(使用RestEasy库)配置模块,但是当我尝试测试我的restful webservice(作为post方法)时,我收到消息“此方法不支持HTTP方法POST ”。 我不明白为什么我看到这个消息......

我按照以下方式启动我的休息服务:

http://localhost:8080/rest/email/myName/my@address.com/myMessage

在我的pom.xml中,我添加了所需的库:

<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>

在我的web.xml中,我添加了这个:

<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>

最后,我的用于rest api的java类:

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();

    }
}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。上下文路径不正确。 我试图称之为:

http://localhost:8080/rest/email/myName/my@address.com/myMessage

正确的道路是:

http://localhost:8080/MYAPP-SNAPSHOT-1.0/rest/email/myName/my@address.com/myMessage

我解决了在jboss-web.xml

中添加context-path标签的问题
<?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>

最后,我在web.xml中添加了context-param,如下所示:

<context-param>
    <param-name>resteasy.servlet.mapping.prefix</param-name>
    <param-value>/</param-value>
</context-param>

我希望对你有所帮助!