#ResponseBody
#RequestMapping(value = {"apiRequest"}, method = {RequestMethod.POST})
public String contestApiSignUp(HttpServletRequest req) throws JSONException {
try {
String username = req.getParameter("username");
String firstname = req.getParameter("firstname");
String lastname = req.getParameter("lastname");
String password = req.getParameter("password");
String phone = req.getParameter("phone");
Here the values I am getting all are null. That is username =null, firstname =null...
I am hitting post request with the values
http://localhost:8080/apiRequest.htm
username = Subhajit
firstname = Subha
...
like this.
But while I am using same code but,
#RequestMapping(value = {"apiRequest"}, method = {RequestMethod.GET})
using GET instead of POST then I am getting the proper values.
When you sent your HTML Form by POST to your URL /apiRequest, did you fill your data (username, firstname etc.) in fields in your form, or did you attached them to the sent URL (eg. apiRequest.htm?username=test&...) ?
Because with the second approach, you data will be accessible only through a GET request, not a POST. The first approach will work with a POST request.
Also, you do not need to call explicitly getParameter on the HttpServletRequest in a Spring Controller.
You can do this :
#ResponseBody
#RequestMapping (value = "apiRequest", method = RequestMethod.POST)
public String contestApiSignUp(
#RequestParam ("username") String username,
#RequestParam ("firstname") String firstname,
#RequestParam ("lastname") String lastname,
#RequestParam ("password") String password,
#RequestParam ("phone") String phone) {
return "Hello World";
}
From the comments, you said you are trying with postman to post the request, So are you building website or webservices? either way you should not be sending your data in the headers.
If you are building a website put together an HTML file from which
you can submit a form to this controller
If you are building webservice, change the controller and let it
accept JSON (or XML -I would discourage this though) body. Follow
this https://stackoverflow.com/a/40226580/6785908. And then from
postman (or curl), post a json instead.
Related
My environment
springboot:2.6.4
springdoc-openapi-ui:1.6.6
Probrem
I want to create an API definition to receive requests in x-www-form-urlencoded format.
Using #RequestBody will create a swagger-ui display with no Parameter notation, just the body. However, when receiving a request in x-www-form-urlencoded format, it is necessary to receive it with #RequestParam, and if this is done, the swagger-ui display is created as a query parameter.
#PostMapping(value = "/v1/hoge")
public ResponseEntity<SampleResponse> postProc(
#RequestParam("param1") int param1,
#RequestParam("param2") String param2,
#RequestParam("param3") String param3,
HttpServletRequest request) {
・・・
}
swagger-image
However, since there is no actual query parameter, I do not want to display the Parameter in the same way as when receiving a request with #RequestBody.
The display without parameters is correct, as in POST /user in the following link.
http://158.101.191.70:8081/swagger-ui/4.5.0/index.html#/user/createUser
Is there a solution to this problem?
I might have a solution for you:
#PostMapping(value = "/v1/hoge", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public ResponseEntity<SampleResponse> postProc(
#RequestPart("param1") int param1,
#RequestPart("param2") String param2,
#RequestPart("param3") String param3,
HttpServletRequest request) {
・・・
}
In a perfect world, you could stick to the #RequestParam but I believe there is currently a bug with springdoc that make it impossible.
I need to obtain an authorization token of an endpoint that is in an api1, passing a user and password as a parameter, this endpoint would call it from an api2 in spring boot, could someone help me ??
you can just provide a RestController endpoint:
#PostMapping(value = "/getToken", consumes = APPLICATION_JSON_VALUE)
public String getToken(HttpServletRequest request, #RequestBody String json)
In this example you can pass a JSON with username/password and any other extra attributes you might need. You can replace the string above with a DTO if you find easier. Another option is to pass the 2 parameters
#PostMapping(value = "/getToken", consumes = APPLICATION_JSON_VALUE)
public String getToken(HttpServletRequest request, #RequestParam String username, #RequestParam String password)
how to get both values #RequestBody and #RequestParam together??
below the code:
#RequestMapping(value = "/sign-up", method = RequestMethod.POST)
public ResponseEntity<?> addUser(#RequestBody User user,#RequestParam("location") String location,#RequestParam("deviceid") String deviceid) {
System.out.println(location);
System.out.println(deviceid);
it is possible?
for #RequestParam Content-Type application/x-www-form-urlencoded
and for #RequestBody Content-type application/json
i want both value location and deviceid if there is any other way?
#PathVariable is to obtain some placeholder from the uri (Spring
call it an URI Template) — see Spring Reference Chapter 16.3.2.2 URI
Template Patterns
#RequestParam is to obtain an parameter — see Spring Reference Chapter 16.3.3.3 Binding request parameters to method parameters with #RequestParam
If URL http://localhost:8080/MyApp/user/1234/invoices?date=12-05-2013 gets the invoices for user 1234 on December 5th, 2013, the controller method would look like:
#RequestMapping(value="/user/{userId}/invoices", method = RequestMethod.GET)
public List<Invoice> listUsersInvoices(
#PathVariable("userId") int user,
#RequestParam(value = "date", required = false) Date dateOrNull) {
...
}
Also, request parameters can be optional, but path variables cannot--if they were, it would change the URL path hierarchy and introduce request mapping conflicts. For example, would /user/invoices provide the invoices for user null or details about a user with ID "invoices"
#RequestMapping(value = "/{methodName}/{responseType}", method = RequestMethod.GET)
public ModelAndView execute(#PathVariable String methodName, #PathVariable String responseType,
HttpServletRequest request, HttpServletRequest response){
Map<String,String> yesMap = new HashMap<String,String>();
}
As shown in code above I need to get data types of parameters passed from {responseType} and set the same in yesMap against parameter values.
String param=request.getParameter("id");
is returning null values.
need to get data types of parameters passed from {responseType}
Its type will always be String as you are collecting it in responseType which is String.
get data types of parameters passed from {responseType} and set the same in yesMap against parameter values
I think yesMap.put("String",responseType); will do.
In the code you posted above, the follwign call
yourRootUrl/cheese/stilton
will result in these being true in your "execute" method:
methodName.equals("cheese");
responseType.equals("stilton");
I don't understand what you are trying to achieve. Your parameter names are confusing.
Its quite simple, you have declared two string paramters, which thanks to spring can be path variables.
This is a GET request, so getParamter won't work, try using String param=request.getAttribute("id"); which is completely unrelated to your path variables. Or the differetn annaotion RequestParam like so :
#RequestMapping(value = "/{methodName}/{responseType}", method = RequestMethod.GET)
public ModelAndView execute(#PathVariable String methodName, #PathVariable String responseType, #RequestParam int id)
For the return URL, it seem you have to define the whole URL like so:
String returnURL = "http://localhost:8080/appName/shopping/confirmorder";
Now, I have a problem with the request mapping:
#RequestMapping(value = "/shopping/confirmorder?token={token}&PayerID={payerID}", method = RequestMethod.GET)
public String doGet(#PathVariable("token") String token, #PathVariable("payerID") String payerID,
HttpServletRequest request) {
// do stuff
}
The controller is never called for some reason?
The final returnURL returned from Paypal is like this:
http://localhost:8080/appName/shopping/confirmorder?token=EC-4...G&PayerID=A...W
Note the Ids have been edited.
If you have two path variables named token and payerID, then the method signature should be
public void doGet(#PathVariable("token") String token,
#PathVariable("payerID") String token,
HttpServletRequest request,
HttpServletResponse response)
How did you expect Spring to put those two strings inside a single option parameter of type int?
See http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-uri-templates
Moreover, PathVariable is used to bind portions of the request path to method arguments. In your case, you have request parameters. You should thus use #RequestParam:
#RequestMapping(value = "/shopping/confirmorder", method = RequestMethod.GET)
public void doGet(#RequestParam("token") String token,
#RequestParam("PayerID") String token,
HttpServletRequest request,
HttpServletResponse response)
See http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestparam