Receiving a POST Request on Spring From Another Site - java

I'm a little new in Java Spring. What I want to do is as follows:
Some 3rd party is asking a "return URL" from me and I set it as follows:
https://localhost:9002/my-account/order-history
Then they send me a POST request and I'm supposed to handle it within my controller. The request has both url parameters and a form data. The request is
Request URL:https://localhost:9002/my-account/order-history?responseCode=0000&token=E0ECFC1214B19E5D11B9B587920FC5F164C5CB17E7DC67F083E0EC6676F79467DFBDF4B6CCF3C39BF47F0232D1AA42F1FA112F29B0157DDF98EE3997F781CCB1FEB070A44E530691BA36674BEA4CF56A4A43F2B9746D9C3591CF288D745A6694
Request Method:POST
Status Code:403 Bad or missing CSRF token
Remote Address:127.0.0.1:9002
Referrer Policy:no-referrer-when-downgrade
A part of the form data is:
I added the whole form data and other request info as attachment.
The controller I'm desperately trying to use is as follows:
#Controller
#RequestMapping(value = "/my-account")
public class MaviAccountPageController extends MaviAbstractController
{
#RequestMapping(value = "/order-history", method = RequestMethod.POST)
public ModelAndView process(#RequestBody final String req)
{
//consumes = "text/plain"
System.out.println(req);
System.out.println(req);
return new ModelAndView("deneme");
}
....
}
And I keep getting 403 - Bad or missing CSRF token error.
How should I implement my controller? I have checked below links and they did not work out unfortunately:
How to retrieve FORM/POST Parameters in Spring Controller?
How to explicitly obtain post data in Spring MVC?
I tried, but failed to regenerate issue on postman.
Can anyone, please, advise me about how to move on?

you can annotate your method with #CrossOrigin
#CrossOrigin
#RequestMapping(value = "/order-history", method = RequestMethod.POST)
public ModelAndView process(#RequestBody final String req)
{
//consumes = "text/plain"
System.out.println(req);
System.out.println(req);
return new ModelAndView("deneme");
}
https://spring.io/guides/gs/rest-service-cors/

Related

getting http 400 bad request spring mvc

I am working on Spring mvc project.
I have to implement rest apis, one api which i am implementing now return http 400 bad request. in it get method request. another api working properly for same json but this api returns HTTP 400
controller function
#RequestMapping(value = "/getSlotsByDateRange/hosid/{hos_id}/from/{start_date}/to/{end_date}" , method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<SlotByDateRange> getSlotsByDateRange(#PathVariable String hos_id,#PathVariable String start_date,#PathVariable String end_date) {
return new ResponseEntity<SlotByDateRange>(hospitalService.getSlotsByDateRange(hos_id,start_date,end_date),HttpStatus.ACCEPTED);
}
Request url:
getSlotsByDateRange/hosid/0009/from/2021-04-15/to/2021-04-22
I have just implemented it and it is working for me.
#GetMapping(value = "/getSlotsByDateRange/hosid/{hos_id}/from/{start_date}/to/{end_date}" , produces = "application/json")
For get requests you can use #GetMapping instead of #RequestMapping. As #GetMapping itself contains
#RequestMapping(
method = {RequestMethod.GET}
)
Hope this helps someone else as well.

How to get http request and query string params in the controller

I'm new in the play framework. I'm using play 2.8.x framework and I need to get from the controller session object and params from request. But I don't realize how to do that.
My routes file looks like the following:
POST /api/verifyToken/:token controllers.UserController.verifyToken(token: String, request: Request)
and my controller looks like this:
public class UserController extends Controller {
public Result verifyToken(String token, Http.Request request) {
...
}
}
and when I try to send a request to the server I had had an error but if I remove token parameter all is working fine. How can I pass the request and params to the controller?
Your handler is given the Http.Request when it is called:
java.util.Map<java.lang.String,java.lang.String[]> queryParams = request.queryString();
for the session:
Http.Session session = request.session();

the jattempting to forward a get request to a post request returns not supported error Spring

This is an anchor tag in jsp page calling a get url, from this url I am forwarding to a post request
Call1
call1url hit a get request in controller
#RequestMapping(value = "/call1url", method = RequestMethod.GET)
public String make(HttpServletRequest request) {
return "forward:/manctril";
}
to forward to a post request in controller
#RequestMapping(value = "/main", method = RequestMethod.POST)
public String make2(HttpServletRequest request) {
return "forward:/dash";
}
trying to perform the above returns an error similar to
There was an unexpected error (type=Method Not Allowed, status=405).
Request method 'GET' not supported
Is my attempt possible or while is it failing
I don't think GET to POST call can be done from the server by using redirect or forward, You need to redesign your Solution. You can try achieving it using below way:
a. You anchor Tag do a POST call to the controller using JS or AJAX, and then from one POST to another POST can be done, like below by setting a request attribute
request.setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, HttpStatus.TEMPORARY_REDIRECT);
#RequestMapping(value = "/call1url", method = RequestMethod.POST)
public String make(HttpServletRequest request) {
request.setAttribute(View.RESPONSE_STATUS_ATTRIBUTE, HttpStatus.TEMPORARY_REDIRECT);
return "forward:/main";
}
#RequestMapping(value = "/main", method = RequestMethod.POST)
public String make2(HttpServletRequest request) {
return "dash";
}
b. Your Anchor Tag should go to a GET call, which should render a jsp/html page and then autosubmit the jsp as POST on the page/body load like below,
<body onload="document.forms['redirectToURLForm'].submit()">
<form:form method="POST" id="redirectToURLForm"
name="redirectToURLForm" action="main">
</form:form>
</body>
This will call the POST /main method.

How to transfer data from one request to another request

I have a RestController in which the request is sent by the post method.
#RequestMapping(value = "/params", method = RequestMethod.POST)
public OneObject getParams(#RequestBody NeededObject neededObject) {
// logics
}
After the server processes this request.
Comes the answer to callback endpoint.
#RequestMapping(value = "/callback", method = RequestMethod.POST)
public DifferentObject callback(#RequestBody OtherObject otherObject) {
// processing the response otherObject and if the success
// needs to redirect the request to another endpoint
// with the parameters of the first query,
// transfer to #RequestBody NeededObject neededObject
}
How can I implement this?
The first two endpoints are on the same controller, and I need to redirect it to another controller (Controller below).
#RequestMapping(value = "/need", method = RequestMethod.POST)
public OneDto create(#RequestBody NeededObject neededObject) {
// logics
}
I'm trying something (the code below), but I do not know if it's a good way.
And I do not know where to get the response to inserting it when include.
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/need");
requestDispatcher.include(request, response);
Thank you for your help.

Spring MVC ResquestMapping 'POST' not supported

I have a method with get and post in login controller. When application is deployed from index page i am redirecting to login page it makes GET request(/login.htm) it works fine but when i make post request with JSON body it says Request method 'POST' not supported i am using postman tool to test please
Help
#RequestMapping(value="/login",method = RequestMethod.GET)
public ModelAndView redirectLoginForm()
{
System.out.println("Login Page...");
return new ModelAndView("login");
}
#RequestMapping(value="/login",method=RequestMethod.POST,
consumes=MediaType.APPLICATION_JSON_VALUE,
produces=MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Login> login(#RequestBody Login login)
{
System.out.println("Checking LoginCredentials...");
boolean isValid=loginServiceBo.checkUserLogin(login);
if(isValid)
{
return new ResponseEntity<Login>(login, HttpStatus.ACCEPTED);
}
return new ResponseEntity<>(login, HttpStatus.NOT_FOUND);
}
Taking in account your code:
#RequestMapping(value="/login",method=RequestMethod.POST,
consumes=MediaType.APPLICATION_JSON_VALUE,
produces=MediaType.APPLICATION_JSON_VALUE)
In the screenshot of Postman, is missing the correct mapping, it should be: http://localhost:8073/Spring_Hibernate_Project/login
I think it's connected with your postman query.
May be my screenshot will be helpful to check your postman, on server side I have very similar mapping.

Categories