2016-04-25 73 views
0

我知道這已經被問過,但經歷了多個答案並沒有幫助我解決我的問題。所以這裏是:我是一個嘗試使用Apache CXF創建REST服務的新手。我試圖編寫一個POST方法,並在請求正文中使用JSON發送數據(使用Google Chrome中的POSTMAN來執行此操作)。在Apache CXF REST服務中提交JSON數據作爲請求主體

我的界面看起來是這樣的:

 @Path("/") 
    @Produces("application/json") 
    public interface MyService{ 

     @POST 
     @Path("/addNote/{id}") 
     @Consumes("application/json") 
     NoteResponse addNote(@PathParam("id") Long id, @QueryParam("note")Note note); 

     // OTHER @GET METHODS THAT WORK WELL 
    } 

我implmentation:

@WebService(name = "testservice") 
    public class MyServiceImpl implements MyService{ 

     @Override 
     public NoteResponse addNote(Long id, Note note){ 
      // SOME IMPLEMENTATION 
     } 
     // OTHER @GET METHOD IMPLEMENTATIONS THAT WORK 
    } 

我在,我不需要在我的筆記註釋的@QueryParam一些答案閱讀,而不是隻put和@XMLRootElement放在我的Note類上,但是這樣做並試圖在localhost:8080/rest/addNote/1上調用我的addNote方法。

我現在面臨的問題是note參數爲null。

下面是我通過郵差送來的JSON:

{ 
     "note":{ 
      "id": 4958, 
      "anotherId": 7886, 
      "comment": "salut", 
      "mail": "[email protected]", 
      "gregorianDate": "01-01-2016", 
      "type": "INFO" 
     } 
    } 

回答

2

請嘗試更改API addNote你的接口定義如下:

NoteResponse addNote(@PathParam("id") Long id, Note note); 

,並通過郵差發送此JSON字符串:

 { 
      "id": 4958, 
      "anotherId": 7886, 
      "comment": "salut", 
      "mail": "[email protected]", 
      "gregorianDate": "01-01-2016", 
      "type": "INFO" 
     } 

這應該有效。

+0

我試過你的解決方案,但沒有奏效。 addNote方法根本不被調用。 – silviugarlea

+0

試試這個URL - localhost:8080/rest/MyService/addNote/1 – Sampada

+0

我試過了,但有「找不到服務」。 CXF Servlet映射到/ rest,並且jaxrs:服務器地址指向/ testservice。我去過的URL是localhost:8080/rest/testservice/addNote/1,但addNote方法沒有被調用。只有在提供@QueryParam時纔會調用它。注意總是空的。 – silviugarlea

相關問題