【问题标题】:How can i get params in spring from curl -X request?我如何从 curl -X 请求中获取春季参数?
【发布时间】:2019-07-20 19:24:45
【问题描述】:

我在处理从 curl 请求到我的 spring-boot 应用程序的参数时遇到问题。

我的控制器发布方法:

@RequestMapping(value = "/cat", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public ResponseEntity addCat(@Valid @ModelAttribute NewCatRequest newCatRequest, BindingResult bindingResult) {
    System.out.println( newCatRequest);
    if (bindingResult.hasErrors()) {
        return ResponseEntity.badRequest().body(bindingResult.getFieldError().getDefaultMessage());
    }

    int rowsAffected = catsRepository.saveCat(newCatRequest.getName(), newCatRequest.getColor(), newCatRequest.getTail_length(), newCatRequest.getWhiskers_length());
    if (rowsAffected == 1) {
        return ResponseEntity.ok().body(newCatRequest);
    } else {
        return ResponseEntity.badRequest().body("There was an unexpected error while trying to create cat for you :(");
    }
}

问题是:当我试图用 curl 发送这个时:

curl -X 发布http://localhost:8080/cat\ -d "{\"name\": \"Tihon\", \"color\": \"red & white\", \"tail_length\": 15, \"whiskers_length\": 12}"

我在“newCatRequest”中有所有空参数: NewCatRequest{name='null',color='null',tail_length=0,whiskers_length=0}

但是当我尝试对 Postman 做同样的事情时(POST 方法,x-www-form-urlencoded 在正文中使用我的参数)我有一个有效的结果: Result from Postman

请帮助我了解问题所在。

【问题讨论】:

    标签: java rest spring-boot http curl


    【解决方案1】:
    curl -X POST http://localhost:8080/cat \
         -d "{\"name\": \"Tihon\", \"color\": \"red & white\", \"tail_length\": 15, \"whiskers_length\": 12}"
    

    上面的curl请求有一个JSON体,而你的请求处理方法

    @RequestMapping(value = "/cat", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    

    消费/接受:application/x-www-form-urlencoded。因此,您应该将您的方法转换为使用/接受application/json 或将您的curl 请求更改为:

    curl -X POST http://localhost:8080/cat \
         -d 'name=Tihon&color=red%20%26%20white&tail_length=15&whiskers_length= 12' \
         -H 'Content-Type: application/x-www-form-urlencoded'
    

    编辑 1

    请注意,curl 的默认 Content-Typeapplication/x-www-form-urlencoded。要使用 JSON,请将您的请求更改为:

    curl -X POST http://localhost:8080/cat \
         -d "{\"name\": \"Tihon\", \"color\": \"red & white\", \"tail_length\": 15, \"whiskers_length\": 12}" \
         -H 'Content-Type: application/json'
    

    【讨论】:

    • 在将 MediaType 更改为 MediaType.Application_JSON_VALUE 后,我的控制器对我说已解决 [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded' not supported]。这次发 curl 和我第一个问题一样
    • 即使我在 curl 请求中添加 -H 'Content-Type: application/json' 并在我的控制器中使用 = MediaType.APPLICATION_JSON_VALUE - 我也有同样的问题(空字段 NewCatRequest{name= 'null',color='null',tail_length=0,whiskers_length=0)
    • 在有效注释后添加 RequestBody 帮助了我。此外,我将 MediaType 保存为 application/json/ 而 curl 请求的问题是没有在标头中指定内容类型,因为默认内容类型设置为 x-www-form-urlecoded。非常感谢。
    【解决方案2】:

    试试这个:

    curl -X POST \
      http://localhost:8080/cat \
      -H 'Content-Type: application/x-www-form-urlencoded' \
      -d 'name=Thidon&color=red%20%26%20white&tail_length=15&whiskers_length=12'
    

    您忘记了标题application/x-www-form-urlencoded,并且正文不应该是json格式。

    【讨论】:

      【解决方案3】:

      您可以使用邮递员中发送按钮下方的代码选项来生成适合您的确切 curl 请求https://i.stack.imgur.com/hbk8M.png 在代码下拉菜单中搜索 curl ,它将为您生成一个整洁干净的 curl 请求。 希望这会有所帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-06-04
        • 1970-01-01
        • 2021-12-03
        • 1970-01-01
        • 1970-01-01
        • 2013-08-03
        相关资源
        最近更新 更多