【问题标题】:Spring tutorial - CURL command to post JSON data not workingSpring 教程 - 用于发布 JSON 数据的 CURL 命令不起作用
【发布时间】:2020-07-01 16:12:07
【问题描述】:

我目前正在学习有关 REST 的 Spring 教程(整个教程位于 spring.io/guides/tutorials/rest/)。我很确定我已经准确地遵循了指南。我有以下 EmployeeController 代码:

package com.example.buildingrest;

import java.util.List;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
class EmployeeController {

    private final EmployeeRepository repository;

    EmployeeController(EmployeeRepository repository) {
        this.repository = repository;
    }

    // Aggregate root

    @GetMapping("/employees")
    List<Employee> all() {
        return repository.findAll();
    }

    @PostMapping("/employees")
    Employee newEmployee(@RequestBody Employee newEmployee) {
        return repository.save(newEmployee);
    }

    // Single item

    @GetMapping("/employees/{id}")
    Employee one(@PathVariable Long id) {

        return repository.findById(id)
            .orElseThrow(() -> new EmployeeNotFoundException(id));
    }

    @PutMapping("/employees/{id}")
    Employee replaceEmployee(@RequestBody Employee newEmployee, @PathVariable Long id) {

        return repository.findById(id)
                .map(employee -> {
                    employee.setName(newEmployee.getName());
                    employee.setRole(newEmployee.getRole());
                    return repository.save(employee);
                })
                .orElseGet(() -> {
                    newEmployee.setId(id);
                    return repository.save(newEmployee);
                });
    }

    @DeleteMapping("/employees/{id}")
    void deleteEmployee(@PathVariable Long id) {
        repository.deleteById(id);
    }
}

当我执行 CURL 命令以获取所有员工时,我成功了,当我执行 CURL 命令以通过 id 获取一名员工时,我成功了。问题是当我尝试发布新员工时。我正在使用来自教程的以下命令:

curl -X POST localhost:8080/employees -H 'Content-type:application/json' -d '{"name": "Samwise Gamgee", "role": "gardener"}'

我收到以下错误:

{"timestamp":"2020-03-20T13:28:56.244+0000","status":415,"error":"Unsupported Media Type","message":"Content type 'application/x-www-form-urlencoded;charset=UTF-8' not
 supported","path":"/employees"}curl: (6) Could not resolve host: Samwise Gamgee,
curl: (6) Could not resolve host: role
curl: (3) [globbing] unmatched close brace/bracket in column 9

至于不匹配的右大括号/大括号,我上下查看了代码和CURL命令,找不到。至于不支持的媒体类型,我不明白为什么当我为整个应用程序使用 JSON 时它会声明 x-www-form-urlencoded。而且我直接从教程中复制了 curl 命令。

有什么想法吗?

【问题讨论】:

  • -H 'Content-Type:application/json'
  • 这并没有解决问题
  • 你在用windows吗?
  • 我使用的是 Windows,但我在 Spring Tool Suite 的终端中使用 Bash shell。

标签: java json spring spring-boot curl


【解决方案1】:

显然 Spring 教程的 cURL 命令错误。它指出 cURL 命令应该是:

curl -X POST localhost:8080/employees -H 'Content-type:application/json' -d '{"name": "Samwise Gamgee", "role": "gardener"}'

实际上应该是:

curl -i -H "Content-Type: application/json" -X POST -d "{\"name\": \"Samwise Gamgee\", \"role\": \"gardener\"}" http://localhost:8080/employees

【讨论】:

  • Spring的教程丑爆了
猜你喜欢
  • 2017-11-05
  • 2017-03-31
  • 1970-01-01
  • 2017-09-11
  • 1970-01-01
  • 2019-10-21
  • 1970-01-01
  • 2021-11-28
  • 1970-01-01
相关资源
最近更新 更多