2015-05-08 38 views
0

我是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); 
    } 

} 

回答

0

喲u必須定義路徑/api/greetings(帶參數)映射爲consumes = MediaType.APPLICATION_JSON_VALUE所以編譯器正在等待JSON輸入,但它越來越text/plain類型(它提到在堆棧跟蹤)的請求,以便隨時隨地可以收發請求確認內容類型爲「應用/ JSON」。

由於現時針對/api/greetingstext/plain沒有映射關係,這個異常被拋出!

希望它有幫助!

祝你好運!

+0

@KunalMalhotra對它有幫助嗎? – Vihar

0

該服務期望application/jsonContent-Type但它收到text/plain輸入。

你在做什麼?如果您使用Postman進行測試,請務必在標頭部分添加Content-Typeapplication/json值。