Spring Boot REST method not allowed
up vote
0
down vote
favorite
I'm trying to implement a method that will allow users (including anonymous) to read reviews about certain items. I want to use two path variables so I don't repeat myself with the code. That's how the Controller method looks like:
@RestController("/reviews")
@AllArgsConstructor
public class ReviewController {
@Autowired
private ReviewService reviewService;
@GetMapping("/{type}/{id}")
public ReviewDTO readReview(@PathVariable String type, @PathVariable long id) {
return reviewService.readReview(type, id);
}
Each type has its own entity, so I want to use the type variable to determine, which repository should be used. Here's the implementation of the ReviewService.readReview method:
public ReviewDTO readReview(String type, long id) {
switch (type) {
case "rides":
return new RideReviewDTO(findRideReview(id));
case "beacons":
return new TransceiverReviewDTO(findTransceiverReview(id));
case "backpacks":
return null;
default:
return null;
}
}
When I'm running tests, the request looks exacly as it should:
MockHttpServletRequest:
HTTP Method = GET
Request URI = /reviews/rides/1
Parameters = {}
Headers = {}
Body = null
I'm getting an error though. This is the response I'm getting when trying to perform this request in Postman:
{
"timestamp": "2018-11-20T07:08:39.145+0000",
"status": 405,
"error": "Method Not Allowed",
"message": "Request method 'GET' not supported",
"path": "/api/reviews/rides/1"
}
And this is the full reponse I get from test MockMvc:
MockHttpServletResponse:
Status = 405
Error message = Request method 'GET' not supported
Headers = {Allow=[POST], X-Content-Type-Options=[nosniff], X-XSS-Protection=[1; mode=block], Cache-Control=[no-cache, no-store, max-age=0, must-revalidate], Pragma=[no-cache], Expires=[0], X-Frame-Options=[DENY]}
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies =
I'm not sure what's wrong, when trying to debug, the method in the controller isn't even reached. That's how I configured my security configuration:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(jsr250Enabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private PowderizeUserDetailsService userDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.cors().and()
.headers().and()
.authorizeRequests()
.antMatchers("/users", "/users/confirm/**", "/users/forgotPassword").anonymous()
.antMatchers(HttpMethod.GET, "/reviews/*").permitAll()
.anyRequest().authenticated().and()
.httpBasic().and()
.logout().permitAll().and()
.csrf().disable();
}
EDIT
The answer given by @Alien was correct one, but there was also one more issue - in the security configuration method, the pattern .antMatchers(HttpMethod.GET, "/reviews/*").permitAll()
was also bad. there should be /reviews/**
, otherwise the requests would not include the below directories.
rest http spring-boot servlets
add a comment |
up vote
0
down vote
favorite
I'm trying to implement a method that will allow users (including anonymous) to read reviews about certain items. I want to use two path variables so I don't repeat myself with the code. That's how the Controller method looks like:
@RestController("/reviews")
@AllArgsConstructor
public class ReviewController {
@Autowired
private ReviewService reviewService;
@GetMapping("/{type}/{id}")
public ReviewDTO readReview(@PathVariable String type, @PathVariable long id) {
return reviewService.readReview(type, id);
}
Each type has its own entity, so I want to use the type variable to determine, which repository should be used. Here's the implementation of the ReviewService.readReview method:
public ReviewDTO readReview(String type, long id) {
switch (type) {
case "rides":
return new RideReviewDTO(findRideReview(id));
case "beacons":
return new TransceiverReviewDTO(findTransceiverReview(id));
case "backpacks":
return null;
default:
return null;
}
}
When I'm running tests, the request looks exacly as it should:
MockHttpServletRequest:
HTTP Method = GET
Request URI = /reviews/rides/1
Parameters = {}
Headers = {}
Body = null
I'm getting an error though. This is the response I'm getting when trying to perform this request in Postman:
{
"timestamp": "2018-11-20T07:08:39.145+0000",
"status": 405,
"error": "Method Not Allowed",
"message": "Request method 'GET' not supported",
"path": "/api/reviews/rides/1"
}
And this is the full reponse I get from test MockMvc:
MockHttpServletResponse:
Status = 405
Error message = Request method 'GET' not supported
Headers = {Allow=[POST], X-Content-Type-Options=[nosniff], X-XSS-Protection=[1; mode=block], Cache-Control=[no-cache, no-store, max-age=0, must-revalidate], Pragma=[no-cache], Expires=[0], X-Frame-Options=[DENY]}
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies =
I'm not sure what's wrong, when trying to debug, the method in the controller isn't even reached. That's how I configured my security configuration:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(jsr250Enabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private PowderizeUserDetailsService userDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.cors().and()
.headers().and()
.authorizeRequests()
.antMatchers("/users", "/users/confirm/**", "/users/forgotPassword").anonymous()
.antMatchers(HttpMethod.GET, "/reviews/*").permitAll()
.anyRequest().authenticated().and()
.httpBasic().and()
.logout().permitAll().and()
.csrf().disable();
}
EDIT
The answer given by @Alien was correct one, but there was also one more issue - in the security configuration method, the pattern .antMatchers(HttpMethod.GET, "/reviews/*").permitAll()
was also bad. there should be /reviews/**
, otherwise the requests would not include the below directories.
rest http spring-boot servlets
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to implement a method that will allow users (including anonymous) to read reviews about certain items. I want to use two path variables so I don't repeat myself with the code. That's how the Controller method looks like:
@RestController("/reviews")
@AllArgsConstructor
public class ReviewController {
@Autowired
private ReviewService reviewService;
@GetMapping("/{type}/{id}")
public ReviewDTO readReview(@PathVariable String type, @PathVariable long id) {
return reviewService.readReview(type, id);
}
Each type has its own entity, so I want to use the type variable to determine, which repository should be used. Here's the implementation of the ReviewService.readReview method:
public ReviewDTO readReview(String type, long id) {
switch (type) {
case "rides":
return new RideReviewDTO(findRideReview(id));
case "beacons":
return new TransceiverReviewDTO(findTransceiverReview(id));
case "backpacks":
return null;
default:
return null;
}
}
When I'm running tests, the request looks exacly as it should:
MockHttpServletRequest:
HTTP Method = GET
Request URI = /reviews/rides/1
Parameters = {}
Headers = {}
Body = null
I'm getting an error though. This is the response I'm getting when trying to perform this request in Postman:
{
"timestamp": "2018-11-20T07:08:39.145+0000",
"status": 405,
"error": "Method Not Allowed",
"message": "Request method 'GET' not supported",
"path": "/api/reviews/rides/1"
}
And this is the full reponse I get from test MockMvc:
MockHttpServletResponse:
Status = 405
Error message = Request method 'GET' not supported
Headers = {Allow=[POST], X-Content-Type-Options=[nosniff], X-XSS-Protection=[1; mode=block], Cache-Control=[no-cache, no-store, max-age=0, must-revalidate], Pragma=[no-cache], Expires=[0], X-Frame-Options=[DENY]}
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies =
I'm not sure what's wrong, when trying to debug, the method in the controller isn't even reached. That's how I configured my security configuration:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(jsr250Enabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private PowderizeUserDetailsService userDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.cors().and()
.headers().and()
.authorizeRequests()
.antMatchers("/users", "/users/confirm/**", "/users/forgotPassword").anonymous()
.antMatchers(HttpMethod.GET, "/reviews/*").permitAll()
.anyRequest().authenticated().and()
.httpBasic().and()
.logout().permitAll().and()
.csrf().disable();
}
EDIT
The answer given by @Alien was correct one, but there was also one more issue - in the security configuration method, the pattern .antMatchers(HttpMethod.GET, "/reviews/*").permitAll()
was also bad. there should be /reviews/**
, otherwise the requests would not include the below directories.
rest http spring-boot servlets
I'm trying to implement a method that will allow users (including anonymous) to read reviews about certain items. I want to use two path variables so I don't repeat myself with the code. That's how the Controller method looks like:
@RestController("/reviews")
@AllArgsConstructor
public class ReviewController {
@Autowired
private ReviewService reviewService;
@GetMapping("/{type}/{id}")
public ReviewDTO readReview(@PathVariable String type, @PathVariable long id) {
return reviewService.readReview(type, id);
}
Each type has its own entity, so I want to use the type variable to determine, which repository should be used. Here's the implementation of the ReviewService.readReview method:
public ReviewDTO readReview(String type, long id) {
switch (type) {
case "rides":
return new RideReviewDTO(findRideReview(id));
case "beacons":
return new TransceiverReviewDTO(findTransceiverReview(id));
case "backpacks":
return null;
default:
return null;
}
}
When I'm running tests, the request looks exacly as it should:
MockHttpServletRequest:
HTTP Method = GET
Request URI = /reviews/rides/1
Parameters = {}
Headers = {}
Body = null
I'm getting an error though. This is the response I'm getting when trying to perform this request in Postman:
{
"timestamp": "2018-11-20T07:08:39.145+0000",
"status": 405,
"error": "Method Not Allowed",
"message": "Request method 'GET' not supported",
"path": "/api/reviews/rides/1"
}
And this is the full reponse I get from test MockMvc:
MockHttpServletResponse:
Status = 405
Error message = Request method 'GET' not supported
Headers = {Allow=[POST], X-Content-Type-Options=[nosniff], X-XSS-Protection=[1; mode=block], Cache-Control=[no-cache, no-store, max-age=0, must-revalidate], Pragma=[no-cache], Expires=[0], X-Frame-Options=[DENY]}
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies =
I'm not sure what's wrong, when trying to debug, the method in the controller isn't even reached. That's how I configured my security configuration:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(jsr250Enabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private PowderizeUserDetailsService userDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.cors().and()
.headers().and()
.authorizeRequests()
.antMatchers("/users", "/users/confirm/**", "/users/forgotPassword").anonymous()
.antMatchers(HttpMethod.GET, "/reviews/*").permitAll()
.anyRequest().authenticated().and()
.httpBasic().and()
.logout().permitAll().and()
.csrf().disable();
}
EDIT
The answer given by @Alien was correct one, but there was also one more issue - in the security configuration method, the pattern .antMatchers(HttpMethod.GET, "/reviews/*").permitAll()
was also bad. there should be /reviews/**
, otherwise the requests would not include the below directories.
rest http spring-boot servlets
rest http spring-boot servlets
edited Nov 20 at 8:26
asked Nov 20 at 7:22
Jack_Russell
9411
9411
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Try to Change
@RestController("/reviews")
@AllArgsConstructor
public class ReviewController
to
@RestController
@RequestMapping("/reviews")
@AllArgsConstructor
public class ReviewController
by specifying the base url in @RequestMapping instead of @RestController.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Try to Change
@RestController("/reviews")
@AllArgsConstructor
public class ReviewController
to
@RestController
@RequestMapping("/reviews")
@AllArgsConstructor
public class ReviewController
by specifying the base url in @RequestMapping instead of @RestController.
add a comment |
up vote
1
down vote
accepted
Try to Change
@RestController("/reviews")
@AllArgsConstructor
public class ReviewController
to
@RestController
@RequestMapping("/reviews")
@AllArgsConstructor
public class ReviewController
by specifying the base url in @RequestMapping instead of @RestController.
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Try to Change
@RestController("/reviews")
@AllArgsConstructor
public class ReviewController
to
@RestController
@RequestMapping("/reviews")
@AllArgsConstructor
public class ReviewController
by specifying the base url in @RequestMapping instead of @RestController.
Try to Change
@RestController("/reviews")
@AllArgsConstructor
public class ReviewController
to
@RestController
@RequestMapping("/reviews")
@AllArgsConstructor
public class ReviewController
by specifying the base url in @RequestMapping instead of @RestController.
edited Nov 20 at 7:34
answered Nov 20 at 7:27
Alien
4,41521023
4,41521023
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388085%2fspring-boot-rest-method-not-allowed%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown