gpt4 book ai didi

json - 客户端发送的请求在语法上不正确()。+ Spring,RESTClient

转载 作者:行者123 更新时间:2023-12-04 05:13:33 25 4
gpt4 key购买 nike

我正在使用JSON对象使用Spring MVC。当我尝试从RESTClient发送JSON对象时,

HTTP Status 400 - The request sent by the client was syntactically incorrect ().



这是我的 Controller
ObjectMapper mapper=new ObjectMapper();
@RequestMapping(value = "/addTask", method = RequestMethod.GET)
public ModelAndView addTask(@RequestParam("json") String json) throws JsonParseException, JsonMappingException, IOException
{
System.out.println("Json object from REST : "+json);
Task task=(Task) mapper.readValue(json, Task);
service.addService(task);
return new ModelAndView("Result");
}

我的要求网址: http://localhost:8080/Prime/addTask
我的Json对象:

{"taskName":"nothing","taskId":1234,"taskDesc":"nothing doing"}



我也尝试在RESTClient中指定“Content-Type:application/json”,但仍然收到相同的错误

最佳答案

试试这个

改变

@RequestParam("json") String json


 @RequestBody Task task

如果您对POST方法不感兴趣,可以尝试一下

从更改您的Controller方法
@RequestMapping(value = "/addTask", method = RequestMethod.GET)
public ModelAndView addTask(@RequestParam("json") String json)


@RequestMapping(value = "/addTask/{taskName}/{taskId}/{taskDesc}", method = RequestMethod.GET)
public ModelAndView addTask(@RequestParam("taskName") String taskName,
@RequestParam("taskId") String taskId,@RequestParam("taskDesc") String taskDesc)

并将您的URL更改为
http://localhost:8080/Prime/addTask/mytask/233/testDesc

关于json - 客户端发送的请求在语法上不正确()。+ Spring,RESTClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14208682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com