4

I am working through the Spring Guides Tutorial: Building Rest services with Spring.

I have followed along the text and entered the code in the tutorial.

I get to the part where I start the service (on local machine) and test using CURL commands.

GET works fine:

Curl -v localhost:8080/employees

returns the expected list

[{"id":1,"name":"Bilbo Baggins","role":"burglar"},{"id":2,"name":"Frodo Baggins","role":"thief"}]

However when I execute:

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

I get:

{"timestamp":"2018-11-08T20:55:49.844+0000","status":415,"error":"Unsupported Media Type","message":"Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported","path":"/employees"}

Here is the Controller Code

package com.mainsworth.payroll;

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;
    }

@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);
}

}

I followed Karol's and Jesper's advice. Thanks to both for the quick response. My new Curl is:

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

and my new response is:

{"timestamp":"2018-11-08T22:49:01.900+0000","status":415,"error":"Unsupported Media Type","message":"Content type 'application/x-www-form-urlencoded;charset=UTF-8' 
not supported","path":"/employees"}application
curl: (6) Could not resolve host: application

curl: (6) Could not resolve host: Samwise Gamgee,role

curl: (3) [globbing] unmatched close brace/bracket in column 9
2

3 Answers 3

3

It was happening also on my side during the tutorial: https://spring.io/guides/tutorials/rest/

What was strange that on Linux OS it not happened. I double check on Linux and everything was perfectly fine.

Solution (example): Instead:

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

Use:

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

Summarizing: Change all ' to " and in body add \ before each "

I hope it helps!

Funfell

0
1

Specify both Content-Type: application/json and Accept: application/json request headers as your endpoint is both consuming and producing the data.

curl -H 'Content-Type: application/json' -H 'Accept: application/json' ...
1
  • Thanks for the quick response, Karol. curl -X POST localhost:8080/employees -H 'Content-Type: application/json' -H 'Accept: application/json' -d '{"name": "Samwise Gamgee","role": "gardener"}' returns "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: application curl: (6) Could not resolve host: application curl: (6) Could not resolve host: Samwise Gamgee,role curl: (3) [globbing] unmatched close brace/bracket in column 9 Commented Nov 8, 2018 at 22:52
0

In addition to the above answers that do work, I'd like to make people aware of a slight error in that tutorial code when it relates to Springboot 2.

I was getting a 405 after following the curl advisories described here.

I discovered that a tiny tweak to the annotation was needed:

@PostMapping(value="/employees",produces = "application/json")
Employee newEmployee(@RequestBody Employee newEmployee) {
return repository.save(newEmployee);
}

Note that the produced flag is needed to make it react properly to not be given the 405. When combined with the curl command:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.