繁体   English   中英

java rest API post方法提供了不允许的HTTP 405方法

[英]java rest API post method gives HTTP 405 method not allowed

我正在尝试使用JPA和RESTful api将对象持久保存到数据库中。 在postman中测试post方法时,它可以工作并将行成功插入数据库中。 但是,在chrome中进行测试时,会给我一个HTTP错误-不允许使用405方法吗?

这是我的方法

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Path("{description}/{name}")
public Response postHobby(
        @PathParam("description") String description,
        @PathParam("name") String name) {
    fHobby.postHobby(description, name);
    return Response.ok("success").build();
}

我的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.mycompany</groupId>
<artifactId>Krak</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>Krak</name>

<properties>
    <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.5.2</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
        <version>2.5.2</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.5</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.12</version>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-bundle</artifactId>
        <version>1.19.4</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <compilerArguments>
                    <endorseddirs>${endorsed.dir}</endorseddirs>
                </compilerArguments>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${endorsed.dir}</outputDirectory>
                        <silent>true</silent>
                        <artifactItems>
                            <artifactItem>
                                <groupId>javax</groupId>
                                <artifactId>javaee-endorsed-api</artifactId>
                                <version>7.0</version>
                                <type>jar</type>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

如果您愿意,我可以提供任何其他信息

这很可能是因为您只是通过在浏览器的地址栏中输入URL并@PathParam@PathParam它(我得出了这个结论,因为您使用的是@PathParam ,这意味着您可以将所需的数据直接嵌入URL中) 。 这将导致HTTP GET 您的方法声明它是一个接受HTTP POST请求的HTTP端点。 因此,应用服务器将返回405 Method not allowed405 Method not allowed因为它在该端点不接受HTTP GET请求。

几天前,我试图用Spring做类似的事情,但我意识到自己在浏览器中输入了错误的端点。 如果这是MVC应用程序,请检查您的控制器参数。 您是否可以启用任何日志记录?

暂无
暂无

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

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