Home > Software design >  Springboot put method is not working (Console shows a get call)
Springboot put method is not working (Console shows a get call)

Time:10-22

I want to add a name to my strings ArrayList, but when I'm calling the put- method the console.log shows a get method.

When I'm calling the url http://localhost:8081/strings/add/Test I got

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Fri Oct 22 10:49:01 CEST 2021
There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'GET' not supported

On the console I can see this enter image description here

But it should be a put method and not a get method, what is wrong?

import java.util.ArrayList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class BasicController {

    private Logger logger = LoggerFactory.getLogger(BasicController.class);
    ArrayList<String> basicList = new ArrayList<String>();


    @GetMapping("/strings/get")
    @ResponseBody
    public String getAllStrings() {
        return basicList.toString();
    }


    @PutMapping("/strings/add/{newString}")
    @ResponseBody
    public String addNewString(@PathVariable String newString) {
        logger.debug(newString);
        basicList.add(newString);
        return newString   " added";
    }
    

}

Main

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class Application {

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

    @RequestMapping("/")
    public String hello() {
        return "Hello World!";
    }

}

Springboot I'm using 2.5.6

CodePudding user response:

The problem is that you are running the call in a browser, by default this will trigger a get request instead of a put, please check this documentation to learn about Http methods Http Methods explanation

As you pointed out in one of your comments, you are gonna need Postman or other similar tools to make it work properly.

  • Related