2012-10-17 83 views
2

我想將JSON請求體傳遞給在我的Apache Camel應用程序中使用CXFRS製作的REST Webservice。如何訪問Apache Camel/CXF REST webservice中POST/PUT請求的JSON請求體

我想訪問我的處理器中傳遞的請求JSON。

REST URL:

http://localhost:8181/mywebservice/Hello/name/{request_param} 

雖然我在我的處理器在發佈請求體一個JSON,還是exchange.getIn().getBody()總是返回{request_param}不是請求JSON。

我的REST Web服務如下:

@Path("/name/") 
@Consumes({"application/json" ,"application/xml"}) 
public class HelloRest { 
    @POST 
    @Path("/{name}") 
    public TestPojo sayHi(@PathParam("name") String name) { 
     return new TestPojo(name); 
    } 
} 

回答

0

服務器部分:

@POST 
@Path("/") 
@Produces(MediaType.TEXT_PLAIN) 
@Consumes(MediaType.APPLICATION_JSON) 
public String add(MappingUser newUser){ 
    UserEntity userEntity = new UserEntity(newUser.getNickname(), newUser.getPassword()); 
    boolean ret = myDB.addUser(userEntity); 

    //sends the return value (primitive type) as plain text over network 
    return String.valueOf(ret); 
} 

客戶端部分:

public boolean addUser(User user){ 
    WebResource resource = client.resource(url).path("/"); 

    String response = resource 
      //type of response 
      .accept(MediaType.TEXT_PLAIN_TYPE) 
      //type of request 
      .type(MediaType.APPLICATION_JSON_TYPE) 
      //method 
      .post(String.class, user); 

    return Boolean.valueOf(response); 
} 
+0

來源:http://cw.felk.cvut.cz /doku.php/courses/a4m36aos – Milos

+0

請您詳細說明一下。 – schhajed