PostMapping errors asking for GetMapping - spring-boot

I am trying to save data to my Postgres database. This should use a PostMapping annotation on the method so that it sends the object accordingly. However, for some reason it is expecting a "GET" method. Any thoughts and if others have run into a similar issue. I also cannot debug into that method as well.
Inventory.java
#RestController
public class InventoryController {
#Autowired
private InventoryService inventoryService;
#RequestMapping(value="/add", method=RequestMethod.POST)
public Inventory addItem(#RequestBody(required = false) Inventory item) {
System.out.print("This is a test");
return inventoryService.save(item);
}
#GetMapping(path="/test")
#ResponseBody
public String testMethod() {
return "Method works!";
}
}
Stacktrace
There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'GET' not supported
org.springframework.web.HttpRequestMethodNotSupportedException:
Request method 'GET' not supported at
org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:201)
at
org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:421)
at
org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:367)
at
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.getHandlerInternal(RequestMappingHandlerMapping.java:449)
at
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.getHandlerInternal(RequestMappingHandlerMapping.java:67)
at
org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:393)
I have tried the following annotations on the "addItem" method and it still returns the aforementioned trace.
#RequestMapping(value="/add", method=RequestMethod.POST)
#PostMapping(path="/add")
I am running Spring Boot 2.2.0.RELEASE

It's not asking for GetMapping, the error said Request method 'GET' not supported. It means you call endpoint for HttpGet Method, but your defined endpoint in the Controller is HttpPost Method !
Call using curl, should be like below :
curl -d "param1=value1&param2=value2" -H "Content-Type: application/json" -X POST http://localhost:8080/add

You do not need to add #ResponseBody as in #RestController is combination of #Controller and #ResponseBody. Also try to test the code from PostMan alike tools before handing over API end points to Frontend or mobile team.
Example:
#RestController
#RequestMapping(value = "/users")
public class UserController {
...
...
#GetMapping
#PreAuthorize("hasRole('ADMIN')")
public List<User> getAllUsers() {
return userService.getAllUsers();
}
}
Also sometime if we get any particular resource by Id so we should check if resource is null than return blank object or standard NOT_FOUND error, error we can override with #RestControllerAdvice extended from ResponseEntityExceptionHandler where we can extended all the error on controller level.
But if error comes in resource server, so in that case we have to add entrypoints in websecurity configure -> http security method overrid and add custom entrypoint as well failure and success handlers.

Related

Wrapped Response by ResponseBodyAdvice in Spring Boot not showing up in Swagger. Any idea how could it be done?

#ControllerAdvice
public class CA implements ResponseBodyAdvice<Object> { }
This class will wrap the response
#GetMapping
public Person getPerson()
{
return new Person();
}
In Swagger I want it to show WrappedResponse but instead it shows response of return type here(Person).
What changes to make so that it will show the wrapped response ?
Tried different combinations of APIReponse, APIModel, APIResponses of swagger but nothing seems to work

GetMapping and PostMapping

#RestController
public class HelloWorldController {
#GetMapping(path="/helloWorld")
public String helloWorld() {
return "Hello-World";
}
}
I am new to RestFul WebServices. I tried to annotate the helloWorld() with PostMapping but it failed. Using GetMapping, it successfully gets executed.
Can somebody tell me why PostMapping was not allowed?
PostMapping for POST request.
GetMapping for GET request.
If you want call PostMapping success, you can use Postman or SoapUI, curl for testing HTTP POST request.
Reference document:
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/PostMapping.html
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/GetMapping.html
In addition to #Do Nhu Vys answer. You will often encounter problems with CORS and CRFS while performing Post Requests.
References:
https://docs.spring.io/spring-security/site/docs/5.0.x/reference/html/csrf.html
https://spring.io/blog/2015/06/08/cors-support-in-spring-framework

404 error code when attempting to access an API endpoint but works normally on others

I have an endpoint in the com.project.users package that will fetch the information of the logged in user:
#RestController
#RequestMapping(path = "/api")
public class UserController {
#Autowired
private UserRepository repository;
#GetMapping("me")
public User me() {
Optional<User> ouser = repository.findById(1);
return ouser.get();
}
}
I have another controller within com.project.beneficiary and I am trying to access a method through a POST request but instead, I receive a 404 error message:
Request URL:http://localhost:8080/api/beneficiaries
Request method:POST
Remote address:127.0.0.1:8080
Status code:
404
Version:HTTP/1.1
Referrer Policy:no-referrer-when-downgrade
{"timestamp":"2019-04-16T01:46:37.395+0000","status":404,"error":"Not Found","message":"No message available","path":"/api/beneficiaries"}
#RestController
#RequestMapping(path = "/api/beneficiaries")
public class BeneficiaryController {
#Autowired
private BeneficiaryRepository repository;
#PostMapping("/")
public Beneficiary addBeneficiary(#Valid #RequestBody Beneficiary beneficiary) {
return repository.save(beneficiary);
}
}
I've dealt with CORS, and I think it worked because I see no message about it anywhere. All these packages are on the same level as the application's starting point, but it is weird why one is seen and the other is not. Something to do with the POST request?
I found something about setting up the context within application.properties, but whatever I put there will cause the 404 error even from Insomnia software. I tried adding the /beneficiaries, /api/beneficiares, and just /api, but I don't think it is anything to do with it. No error messages in the console are visible.
It's a typo :). I ran into a similar problem some time back. Took me hours to resolve. Just remove ("/") from your #PostMapping.

Spring Autowiring error: java.lang.IllegalStateException: Method [name] can only contain 1 method field. Found: [PUT, POST]

I have a method in one of my webservices that accepts both PUT and POST. This is because we started using PUT but later we needed to support POST too (for a new service).
#RequestMapping(
value = "/endpointURL",
method = {RequestMethod.PUT, RequestMethod.POST})
I am trying to create a test application that calls this method, but Spring throws an Autowiring error during startup with the following error:
java.lang.IllegalStateException: Method [name] can only contain 1
method field. Found: [PUT, POST]
Both the Spring and Feign versions are the same in both applications (webservice with this endpoint, and testing application).
Any ideas on how to fix it please?
Thank you!
Method supports various HTTP method as below. Could you post your class source code. I think you should have another problem. Maybe duplicated path or else.
#RequestMapping("/v1/echo")
#RestController
public class EchoApi {
#RequestMapping(value = "/", method = { RequestMethod.PUT, RequestMethod.POST })
public ResponseEntity<String> echo(#RequestBody String body){
System.err.println(body);
return new ResponseEntity<String>(body, HttpStatus.OK);
}
}
In the end it was due to the Feign version we were using. It's fixed after version 10. Will close this topic. Thanks!

Spring boot Integration test RestController validation

I create one #RestController, so I need to test my validation, so I create one model with and dont set any value, so my app will throw a lot of errors, so now I need to test this validation.
I create this method to validate:
#Test
public void selarAberturaMatriculaFail() throws Exception {
AberturaMatricula aberturaMatricula = new AberturaMatricula();
MockHttpServletRequestBuilder requ = post("/ri/selo/abertura/").contentType(contentType).content(this.jsonWithClass(aberturaMatricula));
mockMvc.perform(requ)
.andExpect(model().attributeHasErrors("cns"));
}
but I got this error:
No ModelAndView found java.lang.AssertionError: No ModelAndView found
this is my Rest method:
#RestController
#RequestMapping("/ri")
public class RIController {
#RequestMapping(value = "/selo/abertura/", method = RequestMethod.POST)
public AberturaMatricula selarAbertura(#RequestBody #Valid AberturaMatricula aberturaMatricula){
...
}
}
In my model I have cns property and more..
You are not going to want to use a model test with a REST controller as REST simply returns the object and not a model and view. See https://docs.spring.io/spring/docs/current/spring-framework-reference/html/integration-testing.html#spring-mvc-test-framework as an example. This tutorial http://spring.io/guides/tutorials/bookmarks/ also shows how to build some tests for REST services.

Resources