简体   繁体   中英

How to send a post request with multiple parameters

I have a project where we call a GET method from Java Script : the method called is below :

public
@ResponseBody
Map run( @RequestParam Map map, HttpServletRequest request,   HttpServletResponse response) throws Exception {

}

Now I am writing a Micro service to do the work which is done inside the above method . And the requirement is this method will call the micro-service .The only thing I am stuck with is passing the (HttpServletRequest request, HttpServletResponse response) to the method in micro-service I am calling as they have some data in there which will be used.

My method definition in micro-service looks like :

@POST
@Path("/myPostCall")
public String myPostCall(Map map,HttpServletRequest request,       HttpServletResponse response) throws Exception{
}

Now I am trying to call this by

 RestTemplate abc = new RestTemplate();
abc.postForObject("http://localhost:8008/report/run", report1, String.class);

It gives me 422 error . But If I remove the request,response parameters in micro-service method . it runs good . But I need them too . Because they have data in it . Is there something out of my understanding .

You can use them as private variables in class as :

@Context 
private HttpServletRequest request;
@Context 
private HttpServletResponse response;

OR

@POST
@Path("/myPostCall")
public String myPostCall(..., @Context HttpServletRequest request, @Context HttpServletResponse response) throws Exception{
    //do some stuff
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM