我是Spring boot
新。春季引導與寧靜服務
我想寫一個代碼,它使用post請求作爲輸入。
當我嘗試通過post rest client chrome插件發送發佈請求時,出現此錯誤。我使用的是Maven構建工具。
{
"timestamp": 1431079188726,
"status": 415,
"error": "Unsupported Media Type",
"exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
"message": "Content type 'text/plain;charset=UTF-8' not supported",
"path": "/api/greetings" }
這些是類。
Application.java
@SpringBootApplication
@ComponentScan(basePackages="webapi")
public class Application {
public static void main(String[] args) throws Exception{
SpringApplication.run(Application.class, args);
}
}
Greeting.java
public class Greeting {
private BigInteger id;
private String text;
public Greeting() {
}
public BigInteger getId() {
return id;
}
public void setId(BigInteger id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
GreetingController.java
@RestController
public class GreetingController {
private static BigInteger nextId;
private static Map<BigInteger, Greeting> greetingMap;
private static Greeting save(Greeting greeting) {
if (greetingMap == null) {
greetingMap = new HashMap<BigInteger, Greeting>();
nextId = BigInteger.ONE;
}
greeting.setId(nextId);
nextId = nextId.add(BigInteger.ONE);
greetingMap.put(greeting.getId(), greeting);
return greeting;
}
static {
Greeting g1 = new Greeting();
g1.setText("Hello World");
save(g1);
Greeting g2 = new Greeting();
g2.setText("Hola Mundo!");
save(g2);
}
@RequestMapping(value = "/api/greetings", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Collection<Greeting>> getGreetings() {
Collection<Greeting> greetings = greetingMap.values();
return new ResponseEntity<Collection<Greeting>>(greetings,
HttpStatus.OK);
}
@RequestMapping(value = "/api/greetings/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Greeting> getGreeting(
@PathVariable("id") BigInteger id)
{
Greeting greeting = greetingMap.get(id);
if (greeting == null) {
return new ResponseEntity<Greeting>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<Greeting>(greeting, HttpStatus.OK);
}
@RequestMapping(value = "/api/greetings", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity<Greeting> createGreeting(
@RequestBody Greeting greeting) {
Greeting savedGreeting = save(greeting);
return new ResponseEntity<Greeting>(savedGreeting, HttpStatus.CREATED);
}
}
@KunalMalhotra對它有幫助嗎? – Vihar