2016-04-25 72 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

相关问题