jamsesso
jamsesso

Reputation: 1918

Spring @RequestBody causes HTTP 405 Method Not Allowed when mapping POJO

I've been building a RESTful API using Spring recently and I'm stuck at the point of mapping request bodies into POJOs.

To be clear, I have probably read 50 other StackOverflow questions stating similar symptoms but to no avail.

Using the @RequestBody annotation, my controller can successfully map the request body to a String. When using POJOs however, Spring complains that o.s.web.servlet.PageNotFound : Request method 'POST' not supported.

Controller:

package api.controller;

import api.domain.SimplePOJO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class TestController {
  @RequestMapping(value = "/test", method = RequestMethod.POST)
  public SimplePOJO testing(@RequestBody SimplePOJO simplePOJO) {
    log.info("Made it into the testing method.");
    return simplePOJO;
  }
}

SimplePOJO:

package api.domain;

import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class SimplePOJO {
  private String name;

  private Integer age;
}

My application configuration is not all that complicated, either. Perhaps it is missing something?

package api;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.hateoas.config.EnableHypermediaSupport;

@ComponentScan
@EnableAutoConfiguration
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
public class Application {
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}

I may also be missing something from my application dependencies, so my pom.xml is attached:

<?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">
    <properties>
    </properties>

    <modelVersion>4.0.0</modelVersion>
    <packaging>war</packaging>
    <groupId>api</groupId>
    <artifactId>API</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.0.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.plugin</groupId>
            <artifactId>spring-plugin-core</artifactId>
            <version>1.1.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.hateoas</groupId>
            <artifactId>spring-hateoas</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.14.8</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.34</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.5.Final</version>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava-collections</artifactId>
            <version>r03</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

I cannot POST data of any content type and have it succeed. I have tried the forms JSON, form-data, and x-www-form-urlencoded. All lead to the 405 Method Not Allowed error. If needed, I can post the request details here. I am using Postman for testing so I am fairly certain that the request is well formed.

When I start the application, I see s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/test],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public api.domain.SimplePOJO api.controller.TestController.testing(api.domain.SimplePOJO) in the console.

Upvotes: 2

Views: 5765

Answers (1)

shazin
shazin

Reputation: 21913

Try this in the request.

{ "name":"shazin", "age":28 }

Upvotes: 2

Related Questions