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