我在 REST 和 Spring MVC 中有一个客户端-服务器架构。 Tghe 客户端正在访问服务器 URL 并发送两个参数。服务器应该使用响应值回复客户端。我正在使用 RestTemplate 访问服务器 URL 的客户端之一。运行时,服务器 URL 被成功访问(服务器端逻辑正在执行),但随后我在客户端收到以下错误:
HTTP Status 400 -
--------------------------------------------------------------------------------
type Status report
message
description The request sent by the client was syntactically incorrect ().
客户端代码为:
rresult = restTemplate.getForObject("http://localhost:8081/Merchant/api/movieTheater"+params.toString(), ResponseText.class);
服务器端代码是:
@ResponseBody
@RequestMapping(value="movieTheater")
public ResponseText getCustomerInput(@RequestParam("name") String name, @RequestParam("price") Double price) {
System.out.println("Requst URL got accessed here");
ResponseText result = new ResponseText();
Transaction transaction = new Transaction();
transaction.setMovieName(name);
transaction.setTicketPrice(price);
transaction.setDatetime(new Date());
if(transactionService.addTransaction(transaction))
result.setMessage(ResponseStatus.SUCCESS.getStatus());
else
result.setMessage(ResponseStatus.FAILED.getStatus());
return result;
}
当我单独访问 URL 时,它工作正常。我不确定我做错了什么。请帮忙!提前致谢!
最佳答案
我知道了!!!我有一个用于在客户端创建 JSON 结构的内部类。解决方案是使该类静态化!由于它不是静态的,它试图实例化,因此收到错误消息:“无法读取 JSON:找不到适合类型的构造函数”
关于java - 获取错误400客户端发送的请求在语法上不正确(): Using RestTemplate, Spring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18203717/