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

标签 json spring rest-client

我正在使用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/

相关文章:

java - Gdx运行时异常: Asset not loaded

javascript - JSON 到 JavaScript,语法错误 : Unexpected token &

java - Hibernate一对一导致: org. hibernate.MappingException:无法确定类型

grails - 使用rest-client-builder插件发布自定义 header 值的问题

javascript - $.each 为 value json,怎么样?

java - 从 json 获取键名和键值

java - 如何将对象类列表添加到数组列表?

java - 在 Spring REST Controller 中读取 HTTP header

Elasticsearch RestHighLevelClient (6.0.0) -- 运行时超时异常

Mozilla 休息客户端发布 json 对象