2

So I'm getting an warning as

 WARN  [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] (default task-2) Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported] 

when I go to the following URL http://localhost:8080/ProjectFE/uregistration and the website shows

HTTP 405 Method not allowed

here is my controller code:

package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import model.daoimpl.UserinfoDaoImpl;
import model.dao.IUserinfoDAO;
import model.entity.Userinfo;

@Controller
public class RegistrationController {
    @RequestMapping(value="/registration",method = RequestMethod.GET)
    public String addRegistrationPage() {
        return "registrationpage";
    }
    @RequestMapping(value="/uregistration",method = RequestMethod.POST)
    public String addURegistrationPage(@ModelAttribute("User")Userinfo u) {
        IUserinfoDAO iu = new UserinfoDaoImpl();
        boolean b = iu.insertInfo(u);
        if(b)
            return "success";
        else
            return "registrationpage";
    }

}

So what should I do ? Also if any other code is required please comment I'll edit the post, Thankyou.

4
  • 1
    What do you mean by "when I go to the following URL"? Do you visit the aforementioned URL on the browser? Do you submit a form, where the action points to this URL? Do you attempt to hit the endpoint through Postman/CURL? Commented Jan 2, 2019 at 11:51
  • that's the url which gets triggered when the form is submitted Commented Jan 2, 2019 at 11:53
  • Can you manually invoke the code you want by using Postman or curl? You can use the debugger to check if the code is invoked. Commented Jan 2, 2019 at 12:19
  • How to do that ? can you explain in more brief ? Commented Jan 2, 2019 at 12:20

4 Answers 4

2

I solved mine by simply adding csrf configurations as mentioned in spring documentation when making an ajax request to the server. Remember that csrf is enabled by default in spring and you have to either disable it (bad) or enable it. Link is here

https://docs.spring.io/spring-security/site/docs/5.0.x/reference/html/csrf.html

So I added <meta name="_csrf" th:content="${_csrf.token}" /> <meta name="_csrf_header" th:content="${_csrf.headerName}" />

<script type="application/x-javascript">
   (function () {
       var token = $("meta[name='_csrf']").attr("content");
       var header = $("meta[name='_csrf_header']").attr("content");
       $(document).ajaxSend(function(e, xhr, options) {
          xhr.setRequestHeader(header, token);
       });
   });
</script>

to my html head and inside the form I am submitting, I included this little hidden input and mine worked for me. Hope it helps ya.

<input type="hidden" th:name="${_csrf.parameterName}"
                        th:value="${_csrf.token}" />
Sign up to request clarification or add additional context in comments.

Comments

0

The issue is with the annotation @Controller inside your code. change it to @RestController and it should start working. Here is the Sample code which I have created and it works well :

package com.sample;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
public class Sample {
@RequestMapping(value="/registration",method = RequestMethod.GET)
public String addRegistrationPage() {
    return "registrationpage";
}
@RequestMapping(value="/uregistration",method = RequestMethod.POST)
public String addURegistrationPage(@RequestBody String u) {

    boolean b = true;
    if (b)
        return "success";
    else
        return "registra";
}

}

Comments

0

As your GET handler mapping is /registration, change your POST handler mapping to same value:

@RequestMapping(value = "/registration", method = RequestMethod.POST)

3 Comments

Can you explain in a little more detail what do you mean by thay ? And why ?
@Mob_Abominator your get handler mapping is /registration and when the view is rendered and then a form is submitted, it will try to POST to the same URL unless you have explicitly mentioned any action in the form.
Sorry, but it doesn't work I'm getting the same warning as above, I guess the main problem lies somewhere else as this just a warning, The error that I get is error 405. So I think that has something to do with it.
0

Searching for the same error you got here, I had the same error and in my case I was making a mistake I didn't put the @PostMapping annotation in my method.

    @PostMapping
    public ResponseEntity<CategoryDTO> insert (@RequestBody CategoryDTO dto){
        dto = service.insert(dto);
        URI uri = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(dto.getId()).toUri();
        return ResponseEntity.created(uri).body(dto);
    }

1 Comment

PostMapping is a combination of @RequestMapping with method provided as POST.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.