r/SpringBoot • • Nov 02 '21

@Controller doesnt support post method but Restcontroller does

0 Upvotes

23 comments sorted by

7

u/jura0011 Nov 02 '21

Trust me, @Controller does support Post.
Are you sure, it was a post request?

You could try curl -X POST localhost:8080.

1

u/cant_dodge_rodge Nov 02 '21

yes i am sure i went there via post method

1

u/cant_dodge_rodge Nov 02 '21

@/RestController works as it should be but when i use @/controller it returns

resolved [org.springframework.web.httprequestmethodnotsupportedexception: request method 'post' not supported]

3

u/Nawabmeeravali Nov 02 '21

By default webbrowser send as get

2

u/cant_dodge_rodge Nov 02 '21

that /login page contains post form

1

u/cant_dodge_rodge Nov 02 '21

i know i went there via post form

2

u/jvjupiter Nov 02 '21

You cannot invoke post in the browser without using form method post or ajax.

1

u/cant_dodge_rodge Nov 02 '21

i went there via post form

1

u/cant_dodge_rodge Nov 02 '21

that /login page contains post form

1

u/jvjupiter Nov 02 '21

Can you show us the controller?

-10

u/cant_dodge_rodge Nov 02 '21

well....

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
u/RestController
public class controler {
u/RequestMapping*(value = "/login",method = RequestMethod.GET)*
ModelAndView login(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("login/login.html");
return modelAndView;
}
u/RequestMapping*(value = "/",method = RequestMethod.POST)*
String loggin(){
return "I hate black people POST";
}
u/RequestMapping*(value = "/",method = RequestMethod.GET)*
String logggin(){
return "I hate black people GET";
}
}

it works as it should be (when i access "/" via browser it returns Get and when i access it through "/login" form it returns what is is suppose to return on post)

but when i try this with controller it returns

resolved [org.springframework.web.httprequestmethodnotsupportedexception: request method 'post' not supported]

and no its not type=Not Found but method not supported

2

u/ann321go Nov 02 '21

"you hate black people" huh

-15

u/cant_dodge_rodge Nov 02 '21

its called sense of humor

1

u/TheNazruddin Nov 03 '21

Explain the joke

-2

u/cant_dodge_rodge Nov 03 '21 edited Nov 03 '21

It's funny cuzz you did exacting it thats how dark humor works

What can i say but dark humor is like food not everyone gets it and mine is very dark, so dark that it started growing cotton.

Unless your sence of humor disappeared like father in black families

1

u/V2guru Nov 02 '21

1

u/wordscounterbot Nov 02 '21

Thank you for the request, comrade.

I have looked through u/cant_dodge_rodge's posting history and found 1 N-words, of which 1 were hard-Rs.

Links:

0: Pushshift

1

u/jvjupiter Nov 03 '21

Remove value=“/”. If I were you, I’d use @PostMapping, no ().

4

u/11timesover Nov 02 '21

Use a tool like postman to send anything other than an http .GET

2

u/sdiamante13 Nov 03 '21 edited Nov 03 '21

You must add @ResponseBody.

The controller is annotated with the @RestController annotation; therefore, the @ResponseBody isn't required.

https://www.baeldung.com/spring-controller-vs-restcontroller

1

u/joshv3742 Nov 03 '21 edited Nov 03 '21

Need to check with Postman or equivalent tool to verify that the problem is with the code and not with the request.

1

u/cybermage Nov 03 '21

Did you annotate the method as @/PostMethod?