java – Spring boot – Request method POST not supported. Tried everything

java – Spring boot – Request method POST not supported. Tried everything

Following might help.

  1. While you deploy the application, spring boot displays all the
    available services on the console. Check the POST method to create
    is being displayed or not. Check that there shouldnt be any contradiction with other services. GET/PUT/POST
  2. If there are services not deployed, try adding / before other services – GET, PUT, POST.
  3. Adding exception handler(link) to check the client input request to check the POJO structure.
  4. Check the URL – If any global path for app/name added with
    configuration.(link)
  5. Try to remove headers – Content-Type in the request header and post
    the request again

Following are different things that can happen. Need not to be followed in order.

EDIT:

Run the following to check whether your controller is being enabled and considered by the spring application or not.

import java.util.Arrays;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);

        System.out.println(Lets inspect the beans provided by Spring Boot:);

        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        for (String beanName : beanNames) {
            System.out.println(beanName);
        }
    }

    }

If your @SpringBootApplication annotated class and controller are in different packages it does not pick it by default class scan.

It works in almost all demos because bootstrapping class (@SpringBootApplication annotated class) and rest controller class reside in the same package and loaded to spring context.

// Make your class accessible to spring like this

@SpringBootApplication(scanBasePackageClasses = {GreetingController.class})
public class BootPocApplication {

OR
// give the base package name like this

@SpringBootApplication(scanBasePackages = {com.tryout})
public class BootPocApplication {

java – Spring boot – Request method POST not supported. Tried everything

For me the issue was that the @RequestBody class was not accessible since it was an internal class.

Making it public/static made it accessible and solved this issue

public static class MyClassDto {
        public String   name;
        public String   comment;
        public String   key;
}

Leave a Reply

Your email address will not be published.