-1
Этот пример работает для кодированного URL GET! Я пытался улучшить его с помощью JSON POST, который не работает. Может ли кто-нибудь помочь мне улучшить его с помощью JSON POST, DELETE и JSON PUT и некоторых сопровождающих команд curl в комментариях, чтобы продемонстрировать эти команды?Groovy Spring Boot: как сделать JSON POST, PUT, DELETE?
/*
* Begin commands to execute this file using Groovy with bash
* spring run app.groovy &
* sleep 10
* curl -i -H "Accept: application/json" http://localhost:8080/greeting?name=siegfried -X GET
* End commands to execute this file using Groovy with bash
*
*
* $Log$
*
*/
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
public class Greeting {
public final long id;
public final String content;
public Greeting(){ this(0,"") }
public Greeting(long id, String content) {
this.id = id;
this.content = content;
}
}
@RestController
class ThisWillActuallyRun {
@RequestMapping("/")
String home() {
"Hello World! " + new java.util.Date()
}
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
return new Greeting(counter.incrementAndGet(), String.format(template, name));
}
// {"id":1,"content":"Hello, siegfried!"}
//@RequestMapping(value="/g2", method=RequestMethod.GET) public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) { return new Greeting(counter.incrementAndGet(), String.format("Hello2, %s!", name)); }
//@RequestMapping(value="/g3", method=RequestMethod.POST, headers = "Content-Type=application/json") public Greeting greeting(@RequestBody Greeting g) { return new Greeting(counter.incrementAndGet(), String.format("Hello2, %s!", g.content)); }
//{"timestamp":1439600325530,"status":415,"error":"Unsupported Media Type","exception":"org.springframework.web.HttpMediaTypeNotSupportedException","message":"Content type 'application/x-www-form-urlencoded' not supported","path":"/g3"}
}
Вы пробовали также аннотирования свои методы с '@ Post' (а также ваш' @ RequestMapping' аннотаций)? –