我有一个休息端点,它以RequestParam和RequestBody作为参数。在客户端,我正在使用javax客户端来调用此其余端点,但是由于响应代码405来自服务器,因此出现了问题。
这是springBoot的restEndpoint代码:
@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);
}
这就是我从客户端调用此端点的方式:
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() + " ");
}
我不明白为什么服务器发送响应405我需要在Entity.json(parameters))中将Map(parameters)转换为json字符串吗? ?
最佳答案
状态代码405
告诉您Method Not Allowed
,因此您的HTTP-Method可能有问题。
您将WebTarget
用作reportName
的RequestParam
中的代码中也存在故障,但是REST服务希望将clientName
用作RequestParam
。
所以改变
@RequestParam(name = "clientName", required = true)
至
@RequestParam(name = "reportName", required = true)