Spring教程-用于发布JSON数据的CURL命令不起作用 [英] Spring tutorial - CURL command to post JSON data not working

查看:40
本文介绍了Spring教程-用于发布JSON数据的CURL命令不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在阅读有关REST的Spring教程(整个教程在spring.io/guides/tutorials/rest/).我敢肯定,我已经正确地遵循了指南.我为EmployeeController使用以下代码:

I am currently working through a Spring tutorial on REST (whole tutorial is at spring.io/guides/tutorials/rest/). I am pretty sure I have followed the guide accurately. I have the following code for the 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获取一名雇员时,我就成功了.问题是当我尝试发布新员工时.我正在使用以下来自教程的命令:

When I do a CURL command to get all employees I am successful, and when I do a CURL command to get one employee by id, I am successful. The problem is when I try to POST a new employee. I am using the following command, which comes from the tutorial:

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命令.

As far as the unmatched close brace/bracket, I have looked at the code and the CURL command up and down and cannot find it. As far as the Unsupported Media Type, I don't understand why it is stating x-www-form-urlencoded when I have been using JSON for the whole application. And I have copied the curl command straight from the tutorial.

有什么想法吗?

推荐答案

显然,Spring教程的cURL命令错误.它指出cURL命令应为:

So apparently the Spring tutorial has the cURL command wrong. It states the cURL command should be:

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教程-用于发布JSON数据的CURL命令不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆