简体   繁体   中英

How to send Map as a requestBody in rest call from java

I have a rest endpoint which takes RequestParam and RequestBody as a parameter. In a client side i am using javax client to invoke this rest endpoint but getting a problem as response code 405 is coming from server.

Here is springBoot restEndpoint code:

@RequestMapping(value = "/run", method = RequestMethod.POST, consumes = "application/json")
    public ReportRunResult runBackendCall(@RequestParam(name = "clientName", required = true) String reportName,
                                     @RequestBody Map<String, ReportParameter> formParams) {
        return service.runReport(reportName, formParams);
    } 

this is how i am calling this endpoint from client:

 public ReportRunResult runBackendCall(String name, Map<String, ReportParameter> parameters) {

  ReportRunResult reportResponse = null;
        WebTarget target = RestClientBuilder.clientBuilder(RestClientBuilder.buildSSLContext(), 3000, 10000).build()
                .target(serverURL.get() + "/run?reportName=" + name);

        Invocation.Builder invocationBuilder = target.request(MediaType.APPLICATION_JSON_TYPE);
        Response response = target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.json(parameters));

        reportResponse = response.readEntity(ReportRunResult.class);
        log.info("response. " + response.getStatus() + " ");   
    }

I don't understand why server sending response 405 Do i need to convert Map(parameters) into json string in Entity.json(parameters)); ?

The status code 405 tells you Method Not Allowed so maybe there is an issue with your HTTP-Method .

There is also a failure in your code in the WebTarget you're using the reportName as RequestParam but the REST-Service wants the clientName as RequestParam .

So change

@RequestParam(name = "clientName", required = true)

to

@RequestParam(name = "reportName", required = true)

you can use JSONObject like this :

new JSONObject(map)

PS: the map should be of type Map<String,String>

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