json 客户端发送的请求语法错误 ().+Spring , RESTClient

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14208682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:42:06  来源:igfitidea点击:

The request sent by the client was syntactically incorrect ().+Spring , RESTClient

jsonspringrest-client

提问by Chandrasekar

I am working with Spring MVC using JSON objects. while I am tring to send JSON Object from RESTClient, I am getting

我正在使用 JSON 对象使用 Spring MVC。当我试图从 RESTClient 发送 JSON 对象时,我得到了

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

HTTP 状态 400 - 客户端发送的请求在语法上不正确 ()。

This is my 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");
       }

My request URL : http://localhost:8080/Prime/addTask

我的请求网址: http://localhost:8080/Prime/addTask

My Json Object :

我的 Json 对象:

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

{"taskName":"nothing","taskId":1234,"taskDesc":"什么都不做"}

Also i tried specifying "Content-Type: application/json" in RESTClient but still am getting the same error

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

采纳答案by Kris

Try this

尝试这个

Change

改变

@RequestParam("json") String json

To

 @RequestBody Task task

If you are not interested in POST method you can try this

如果你对 POST 方法不感兴趣,你可以试试这个

change your Controller method from

改变你的控制器方法

@RequestMapping(value = "/addTask", method = RequestMethod.GET)
   public ModelAndView addTask(@RequestParam("json") String json)

to

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

and change your URL to

并将您的网址更改为

http://localhost:8080/Prime/addTask/mytask/233/testDesc

回答by Ben Iggulden

I ran into a similar situation using a JSON string in the request body recently, and using a very similar Spring setup as yours. In my case I wasn't specifying a String parameter and deserialising it myself though, I was letting Spring do that:

我最近在请求正文中使用 JSON 字符串遇到了类似的情况,并使用了与您非常相似的 Spring 设置。在我的情况下,我没有指定 String 参数并自己反序列化它,但我让 Spring 这样做:

   @RequestMapping(value = "/myService/{id}", method = RequestMethod.POST)
   @ResponseBody
   public void myService(@PathVariable(value = "id") Long id, @RequestBody MyJsonValueObject request) {
   ..
   }

I was getting an HTTP error 400 "The request sent by the client was syntactically incorrect" response. Until I realised that there wasn't a default constructor on the @RequestBody MyJsonValueObjectso there were problems deserialising it. That problem presented in this way though.

我收到 HTTP 错误 400“客户端发送的请求在语法上不正确”响应。直到我意识到上面没有默认构造函数,@RequestBody MyJsonValueObject所以反序列化它有问题。但是,这个问题以这种方式提出。

So if you are using POST and objects, and getting errors like this, make sure you have a default constructor! Add some JUnit to be sure you can deserialise that object.

因此,如果您正在使用 POST 和对象,并收到这样的错误,请确保您有一个默认构造函数!添加一些 JUnit 以确保您可以反序列化该对象。

Note: I'm not saying this is the only reason you get this error. The original case used just String (which does have a default constructor !) so it's a little different. But in both cases it appears the request URI appears to have been mapped to the right method, and something has gone wrong trying to extract parameters from the HTTP request.

注意:我并不是说这是您收到此错误的唯一原因。原始案例只使用 String (它确实有一个默认构造函数!)所以它有点不同。但在这两种情况下,请求 URI 似乎已映射到正确的方法,并且在尝试从 HTTP 请求中提取参数时出现问题。

回答by Anthony Nguyen

My problem was due to the incorrect mapping of the @RequestBody object.

我的问题是由于 @RequestBody 对象的映射不正确。

My Request Body looks like this

我的请求正文看起来像这样

{data: ["1","2","3"]}

I had the following code in my controller

我的控制器中有以下代码

@RequestMapping(value = "/mentee", method = RequestMethod.POST)
public @ResponseBody boolean updateData(@RequestBody List<Integer> objDTO, HttpSession session) {
        ...
    }

This give me HTTP 400 because Spring doesn't know how to bind my Json data to a List.

这给了我 HTTP 400,因为 Spring 不知道如何将我的 Json 数据绑定到列表。

I changed the RequestBody object to the following

我将 RequestBody 对象更改为以下内容

@RequestMapping(value = "/mentee", method = RequestMethod.POST)
public @ResponseBody boolean updateData(@RequestBody ObjectiveDto objDTO, HttpSession session) {
        ...
    }

and defined ObjectiveDto as followed

并定义 ObjectiveDto 如下

@ToString
public class ObjectiveDto {

    @Getter @Setter
    private List<Integer> data;

}

This resolved the HTTP 400 error.

这解决了 HTTP 400 错误。