Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring教程-CURL命令发布JSON数据无效_Java_Json_Spring_Spring Boot_Curl - Fatal编程技术网

Java Spring教程-CURL命令发布JSON数据无效

Java Spring教程-CURL命令发布JSON数据无效,java,json,spring,spring-boot,curl,Java,Json,Spring,Spring Boot,Curl,我目前正在学习关于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.springfram

我目前正在学习关于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);
    }
}
我得到以下错误:

{"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命令


你知道怎么回事吗?

显然,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
实际上,它应该是:

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

-H'Content-Type:application/json'没有解决这个问题。你使用windows吗?我使用的是windows,但我在Spring工具套件的终端使用Bash shell。Spring的教程令人反感