Getting error "type=Method Not Allowed, status=405"

10,408

Your controller method supports POST requests not GET,so while sending a request to browser will result in method Not allowed

 @RequestMapping(path = "/token", method = RequestMethod.POST) // POST
            public ResponseEntity<?> getToken() throws JsonProcessingException
            {

              RestTemplate rt = new RestTemplate();

How to resolve

Remove explicit POST from your method and it will work for both GET AND POST requests

@RequestMapping(path = "/token") // Remove explicit mapping here
                public ResponseEntity<?> getToken() throws JsonProcessingException
                {
Share:
10,408

Related videos on Youtube

Oracle Monkey
Author by

Oracle Monkey

Updated on June 04, 2022

Comments

  • Oracle Monkey
    Oracle Monkey almost 2 years

    I am able to get the response when using the POSTMAN but not through code .

    Here is what i am doing through POSTMAN

    RequestMethod.POST
    In HEADERS
    Content-Type : application/x-www-form-urlencoded
    Accept    : */*
    
    In BODY 
    c_id : myname
    c_secret : mypassword
    g_type :  myvalue
    c_scope : value2
    

    This is how i am sending the request and i get the following response

    {
        "access_token": "token_value",
        "token_type": "bearer",
        "expires_in": 6000,
        "scope": "scope_vale",
        "jti": "jti_value"
    }
    

    In POSTMAN i also disabled SSL Verfication .

    This is how i am coding in my SPRING BOOT Application

    @RestController
    @RequestMapping(value="/rest/site")
    public class CSController {
    
    
    
         final String url="url name";
    
    
           @RequestMapping(path = "/token", method = RequestMethod.POST)
            public ResponseEntity<?> getToken() throws JsonProcessingException
            {
    
              RestTemplate rt = new RestTemplate();
    
    
                HttpHeaders headers = new HttpHeaders();
                headers.setAccept(Arrays.asList( MediaType.ALL) );
                headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    
    
                Map<String, String> bodyParamMap = new HashMap<String, String>();
    
              bodyParamMap.put("c_id", "myname");
              bodyParamMap.put("c_secret", "mypassword");
              bodyParamMap.put("g_type", "myvalue");
              bodyParamMap.put("c_scope", "value2");
    
              String reqBodyData = new ObjectMapper().writeValueAsString(bodyParamMap);
              HttpEntity<String> requestEntity = new HttpEntity<>(reqBodyData, headers);
    
              System.out.println(  " get token from url 2");
    
              ResponseEntity<String> result =  rt.exchange(url,HttpMethod.POST, requestEntity, String.class);
    
    
    
                return ResponseEntity.ok(result);
            }
    
    
    }
    

    But i am getting the below error

    Whitelabel Error Page
    This application has no explicit mapping for /error, so you are seeing this as a fallback.
    
    Tue Jun 18 10:49:58 IST 2019
    There was an unexpected error (type=Method Not Allowed, status=405).
    Request method 'GET' not supported
    

    How can i resolve this?

  • Oracle Monkey
    Oracle Monkey almost 5 years
    Thanks a lot i got what was the problem .I did the same what you told but i got a new certificate error Request processing failed; nested exception is org.springframework.web.client.ResourceAccessException: I/O error on POST request for "https://url-for-token":
  • Oracle Monkey
    Oracle Monkey almost 5 years
    ` sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target; nested exception is javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target`
  • Oracle Monkey
    Oracle Monkey almost 5 years
    In POSTMAN i also disabled SSL Verfication .
  • Shubham Dixit
    Shubham Dixit almost 5 years
    url you are using is not working,check for any firewall issue on server or see if you are passing correct url
  • Oracle Monkey
    Oracle Monkey almost 5 years
    URL is correct as i am able to get the response in POSTMAN. So what should i check next?
  • Shubham Dixit
    Shubham Dixit almost 5 years
    How you are passing the parameters in browser ??@OracleMonkey
  • Oracle Monkey
    Oracle Monkey almost 5 years
    not passing any parameter in the browser.There is no parameter .Only BODY and HEADER to be passed.See code