简体   繁体   English

RESTEasy 和 Wildfly 在请求时返回 405 或 404

[英]RESTEasy and Wildfly returns 405 or 404 on requests

I recently rebuilt my Maven WAR Project because the HTTP requests were received, but no dependencies were to be found in the lib folder.我最近重建了我的 Maven WAR 项目,因为收到了 HTTP 请求,但在 lib 文件夹中找不到任何依赖项。
I then moved all the files I had created (classes, web.xml, ...) from the old to the new project.然后我将我创建的所有文件(类、web.xml、...)从旧项目移动到新项目。 Now all dependencies are loaded, but I always get "HTTP method POST is not supported by this URL" (status 405) as a response to POST requests (on GET I get 404 etc.).现在所有依赖项都已加载,但我总是收到“此 URL 不支持 HTTP 方法 POST”(状态 405)作为对 POST 请求的响应(在 GET 上我得到 404 等)。
In the meantime I've gone through many tutorials again to see if I've forgotten something, but I just can't find what my mistake is.与此同时,我又看了很多教程,看看我是否忘记了什么,但我就是找不到我的错误是什么。
I use Wildfly, RESTEasy and Eclipse.我使用 Wildfly、RESTEasy 和 Eclipse。
Below are a few files that could possibly contain my error以下是一些可能包含我的错误的文件

Web.xml网页.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="4.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_4_0.xsd">
</web-app>

jboss-web.xml jboss-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<jboss-web xmlns="http://www.jboss.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee http://www.jboss.org/j2ee/schema/jboss-web_5_1.xsd">
    <context-root>/xyz</context-root>
</jboss-web>

MyApplication.java我的应用程序

package com.xyz.util.rest;

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

import com.xyz.employee.rest.EmployeeRest;

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

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> classes = new HashSet<>();
        classes.add(EmployeeRest.class);
        return classes;
    }
}

EmployeeRest.java员工休息.java

package com.xyz.employee.rest;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.xyz.employee.EmployeeAndPassword;
import com.xyz.employee.service.EmployeeService;
import com.xyz.employee.service.dto.EmployeeDTO;
import com.xyz.servicelocator.ServiceLocator;

@Path("/employee")
public class EmployeeRest {
    EmployeeService emplServ = ServiceLocator.locateService(EmployeeService.class);

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @Path("/create")
    public Response createEmployee(EmployeeAndPassword emplAndPwd) {

        return Response.ok(emplServ.createEmployee((EmployeeDTO) emplAndPwd, emplAndPwd.getPassword())).build();
    }
}

pom.xml 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>com</groupId>
    <artifactId>xyz</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>XYZ</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>12</maven.compiler.source>
        <maven.compiler.target>12</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.22.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.arquillian</groupId>
            <artifactId>arquillian-bom</artifactId>
            <version>1.6.0.Final</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.jboss.arquillian.junit</groupId>
            <artifactId>arquillian-junit-container</artifactId>
            <version>1.6.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.11</version>
        </dependency>
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>3.17.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.21</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20200518</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-core</artifactId>
            <version>4.5.6.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxrs</artifactId>
            <version>3.13.2.Final</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>8.0.1</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>xyz</finalName>
        <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

I tried to send a request to this url via postman: http://localhost:8080/xyz/rest/employee/create我试图通过邮递员向这个 url 发送请求:http://localhost:8080/xyz/rest/employee/create

I've replaced RESTEasy and Wildfly with Jersey and Tomcat.我已经用 Jersey 和 Tomcat 替换了 RESTEasy 和 Wildfly。 Now it works without problems现在它可以正常工作

But I don't know why RESTEasy and Wildfly didn't worked...但我不知道为什么 RESTEasy 和 Wildfly 不起作用......

The following dependency will clash with wildfly:以下依赖项将与 wildfly 发生冲突:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.4.22.Final</version>
</dependency>

Add the scope as provided to resolve the problem, or better remove it.添加提供的范围以解决问题,或者更好地将其删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM