spring can't receive post param send by postman - spring-boot

And My Controller is like
#RequestMapping(value = "/auth/company/delete", method = RequestMethod.POST,
produces = {"application/json", "application/xml"})
#ResponseBody
public ResponseMessage deleteCompany(#RequestParam("companyId") Integer companyId) {
return companyManageService.deleteCompany(companyId);
}
But when I type code in chrome console using
$.post( "http://ltc_dev.leapstack.cn/gw/security/auth/company/delete", { companyId: 1 })
.done(function( data ) {
alert( data.success);
alert( data.message);
});
I got correct response, so.....
I'm not sure if it is a postman's bug, or I cofig the controller wrong

In your question, your controller method try to take companyId as request param. In postman you are sending companyId in request body. Like I said in comment you can send request param in url section directly like that: /auth/company/delete?companyId=2. Spring boot can detect companyId request parameter and assign it to method's companyId variable directly.
If you want to send companyId in request body (You said that in comment) you have to change your method's signature like below.
#RequestMapping(value = "/auth/company/delete", method = RequestMethod.POST, produces = {"application/json", "application/xml"})
#ResponseBody
public ResponseMessage deleteCompany(#RequestBody Map<String, Integer> map) {
return companyManageService.deleteCompany(map.get("companyId"));
}
Or:
#RequestMapping(value = "/auth/company/delete", method = RequestMethod.POST, produces = {"application/json", "application/xml"})
#ResponseBody
public ResponseMessage deleteCompany(#RequestBody CompanyDTO company) {
return companyManageService.deleteCompany(company.getCompanyId);
}
public class CompanyDTO {
private Integer companyId;
//getter setter
}
If you want to use request body and want to catch integer value directly in controller method's variable as integer your request body should be like:
{2}
And controller method should be like:
#RequestMapping(value = "/auth/company/delete", method = RequestMethod.POST, produces = {"application/json", "application/xml"})
#ResponseBody
public ResponseMessage deleteCompany(#RequestBody Integer companyId) {
return companyManageService.deleteCompany(companyId);
}

Related

How do I enter path variable as UUID in postman request?

I have path variable parameter as a UUID, with path as id.
#RequestMapping(method = RequestMethod.GET, path = "/{id}", produces = "application/json")
public ResponseEntity<T> getId(#PathVariable("id") final UUID id) {
}
when I add this as a String4df34f48-33ce-4da2-8eba-a682e2d1e698 or as String in brackets{4df34f48-33ce-4da2-8eba-a682e2d1e698} in my postman url, I get a 400 Bad Request error.
What can I do to add it here?
Thanks.
You need to have #PathVariable as part of the function itself:
#RequestMapping(method = RequestMethod.GET, path = "/{id}", produces = "application/json")
public ResponseEntity<UUID> getUID(#PathVariable("id") final UUID id) {
log.info("id is {}", id);
return new ResponseEntity<>(id, HttpStatus.OK);
}
That should then allow you to query it through Postman:
http://localhost:8080/4df34f48-33ce-4da2-8eba-a682e2d1e698

How to get a param from URL Rest Service that is after a question mark (?)

I have the following String that I receive in my rest service:
.6.75:5050/pretups/C2SReceiver?REQUEST_GATEWAY_CODE=8050122997&REQUEST_GATEWAY_TYPE=EXTGW&LOGIN=CO8050122997_EXTGW&PASSWORD=89b87741ca3f73b0b282ae165bad7501&SOURCE_TYPE=XML&SERVICE_PORT=190
I have the following code:
#Controller
public class servicioscontroller {
#RequestMapping(value = "/pretups/{p1}?{trama}", method = RequestMethod.GET)
#ResponseBody
public String enviarTrama(#PathVariable("p1") String p1,#PathVariable("trama") String trama){
return p1+trama;
}
}
And im getting this result:
C2SReceive
I need also the string after that ?, what im doing wrong or how do I get that? thanks
You should use #RequestParam:
#Controller
public class servicioscontroller {
#RequestMapping(value = "/pretups/{p1}", method = RequestMethod.GET, params = ["trama"])
#ResponseBody
public String enviarTrama(
#PathVariable("p1") String p1,
#RequestParam(name = "trama", required = "true") String trama
){
return p1+trama;
}
}
Note that you have to put request params in the #RequestMapping's params attribute instead of the path string.
The difference between #RequestParam and #PathParam is described in this answer.

I need to pass two #RequestBody parameters into my REST API POST method. How can I achieve this and can I do it using DefaultHttpClient?

My REST API method is:
#RequestMapping(value = "/api/test/summary", method = RequestMethod.POST)
postSummaryData(#RequestBody String one, #RequestBody String two) { ... }
#RequestMapping(value = "/api/test/summary/{one}/{two}", method = RequestMethod.POST) postSummaryData( #RequestParam String one, #RequestParam String two) {
}
Also you can try below:
#RequestMapping(value = "/api/test/summary", method = RequestMethod.POST) postSummaryData() {
RequestAttributes attr = RequestContextHolder.getRequestAttributes();
HttpServletRequest request = ((ServletRequestAttributes)attr).getRequest();
// you can get param from this request
}

400 (Bad Request) while sending json in Spring

I'm trying to send json string to Spring controller, i'm getting 400 - bad request as response
i'm using Spring 4.0.3
This is my controller
#Controller
public class Customer{
#RequestMapping(value = "/apis/test", method = RequestMethod.GET, produces = "application/json")
public #ResponseBody String test(HttpServletRequest params) throws JsonIOException {
String json = params.getParameter("json");
JsonParser jObj = new JsonParser();
JsonArray jsonObj = (JsonArray ) jObj.parse(json);
for(int i = 0; i < jsonObj.size(); i++) {
JsonObject jsonObject = jsonObj.get(i).getAsJsonObject();
System.out.println(jsonObject.get("name").getAsString());
}
return json;
}
}
Please help me to solve this
#RequestMapping(value = "/apis/test", method = RequestMethod.GET, produces = "application/json")
The above means this is a HTTP GET method which does not normally accept data. You should be using a HTTP POST method eg:
#RequestMapping(value = "/apis/test", method = RequestMethod.POST, consumes = "application/json")
public #ResponseBody String test(#RequestParam final String param1, #RequestParam final String param2, #RequestBody final String body) throws JsonIOException {
then you can execute POST /apis/test?param1=one&param2=two and adding strings in the RequestBody of the request
I hope this helps!

Receive URL in spring

I try doing confirmation registration from email, on the email I send this code:
String token = UUID.randomUUID().toString(); //for send email
String confirmationUrl = "<a href='" +
"http://localhost:8080/registrationConfirm.html?token="
+ token+"'>Click for end Registration</a>";
helper.setText("message", confirmationUrl.toString());
I receive something like this:
http://localhost:8080/registrationConfirm.html?token=88ab5907-6ab5-40e2-89d5-d6a7e8cea3c2
How I can receive this 88ab5907-6ab5-40e2-89d5-d6a7e8cea3c2 in spring?
I want doing a new controller, he will be check if 88ab5907-6ab5-40e2-89d5-d6a7e8cea3c2 exist in DB, then he activated registration, if no - talk about misstake.
And I do not understand how the conroller will be look, I do so
#RequestMapping(value = "/token", method = RequestMethod.POST)
public #ResponseBody
String getAttr(#PathVariable(value="token") String id,
) {
System.out.println(id);
return id;
}
To complete the comment and hint Ali Dehghani has given (have a look at the answer https://stackoverflow.com/a/17935468/265043):
#RequestMapping(value = "/registrationConfirm", method = RequestMethod.POST)
public #ResponseBody
String getAttr(#RequestParam(value="token") String id) {
System.out.println(id);
return id;
}
Note that I ignored the html suffix in the request mapping annotation. You should read the docs about (default) content negotiation starting at http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-suffix-pattern-match
this another variant
#RequestMapping(value = "/registrationConfirm", method = RequestMethod.POST)
public void getMeThoseParams(HttpServletRequest request){
String goToURL = request.getParameter("token");
System.out.println(goToURL);
}

Resources