2013-02-18 21 views

risposta

6

Jersey viene fornito con un provider per la mappatura di oggetti JSON in Java. Per mappare il corpo della richiesta a un oggetto, è sufficiente specificare tale oggetto come argomento per il metodo della risorsa. Se si desidera il JSON grezzo, specificare l'oggetto in modo che sia di tipo java.lang.String.

@Path("/mypath") 
public class MyResource { 

    /** 
    * @param pojo Incoming request data will be deserialized into this object 
    */ 
    @POST 
    @Path("/aspojo") 
    @Consumes(MediaType.APPLICATION_JSON) 
    public Response myResourceMethod(MyPojo pojo) { 
     // .... 
    } 

    /** 
    * @param json Incoming request data will be deserialized directly into 
    * this string 
    */ 
    @POST 
    @Path("/asjson") 
    @Consumes(MediaType.APPLICATION_JSON) 
    public Response myResourceMethod(String json) { 
     // .... 
    } 
} 
1
@POST 
public String handleRequest(String requestBody) { 
    logger.info(requestBody); 
    return "ok"; 
} 
Problemi correlati