0

I am trying to persist a object to my database with JPA and through a RESTful api. When testing the post methods in postman, it works and it inserts the row succesfully in the DB. However when testing in a chrome it gives me a HTTP error - 405 method not allowed?

This is my method

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

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

I can provide any additional info if you'd like

lealceldeiro
  • 14,342
  • 6
  • 49
  • 80
Jonas Grønbek
  • 1,709
  • 2
  • 22
  • 49
  • What is shown for this request in the Network tab of the devtools in your browser? Specifically, what's the method in the headers section? – lealceldeiro Oct 05 '18 at 18:37
  • I know this sounds dumb, but are you sure you're doing a POST request and not a GET on chrome? If you can provide how you make the request on chrome as lealceldeiro said, it'd be helpful. – Amin Oct 05 '18 at 18:45
  • How are you doing `POST` in chrome browser? – nabster Oct 05 '18 at 18:47
  • Are you getting a CORS error? Check your devtools. Is it an OPTIONS preflight request. Check out [this post](https://stackoverflow.com/a/28067653/2587435) – Paul Samsotha Oct 05 '18 at 18:48
  • I have trouble inspecting anything in devtools, since chrome redirects to its default error page before I get to do anything – Jonas Grønbek Oct 05 '18 at 18:55

2 Answers2

3

This is most likely because you're invoking this simply by inputting the URL in the browser's address bar and hitting enter (I arrived at this conclusion since you're using @PathParam which means that you can embed your desired data directly in the URL). This will result in an HTTP GET. Your method states that it is an HTTP endpoint that accepts HTTP POST requests. Hence the application server will return a 405 Method not allowed as it does not accept HTTP GET requests at that endpoint.

Janus Varmarken
  • 2,306
  • 3
  • 20
  • 42
  • When commenting the @PathParam out and hardcoding the values I still get a 405 – Jonas Grønbek Oct 05 '18 at 18:52
  • @JonasGrønbek that's really besides the point. I was just explaining how I concluded that what you're probably doing is simply inserting the URL into the browser's address bar and hitting enter. If that is correct then it explains the behavior as that will result in an `HTTP GET` being fired, not an `HTTP POST`. – Janus Varmarken Oct 05 '18 at 18:53
  • I should have read it more carefully. Wait, so you're telling me that you cannot run post methods from the browser directly in the URL? – Jonas Grønbek Oct 05 '18 at 18:57
  • @JonasGrønbek All I'm saying is that the default behavior when you enter a URL into the address bar and hit enter is that an `HTTP GET` is fired. [This](https://stackoverflow.com/questions/42107983/is-it-possible-to-do-a-post-request-from-browser-url) will probably be helpful to you. – Janus Varmarken Oct 05 '18 at 19:03
  • I was able to look at the request in IE, and you are right it does not recognize the post. Thanks for your time! Upvoted and marked as accepted the answer :) – Jonas Grønbek Oct 05 '18 at 19:11
0

I was trying to do something similar with Spring a couple of days ago and I realized that I was typing the endpoint wrong into my browser. If this is an MVC application, check your controller param. Do you have any logging that you can enable?