How to pass @FormParam to a RESTful service from another method?

DISCLAIMER: I was looking for a comprehensive answer to this question, and yes, I found this other question: https://stackoverflow.com/questions/10315728/how-to-send-parameters-as-formparam-to-webservice , But firstly, this question asks about Javascript, while I ask about Java, and secondly, it has no answers anyway. So to the question ...

Using RESTful services, passing @QueryParamto a service is @GETquite simple, since you can simply add pairs of variable names / values ​​to the URL and use it to get to the server from the program. Is there any way to do this using @FormParam?

For example, let's say I have the following RESTful service:

@POST
@Produces("application/xml")
@Path("/processInfo")
public String processInfo(@FormParam("userId") String userId,
                          @FormParam("deviceId") String deviceId,
                          @FormParam("comments") String comments) {
    /*
     * Process stuff and return
     */
}

... , - :

public void updateValues(String comments) {

    String userId = getUserId();
    String deviceId = getDeviceId();

    /*
     * Send the information to the /processInfo service
     */

}

?

.. , . , RESTful , , . , RESTful .

!

+5
1

@FormParam, . .

-, , Java-, jersey client.Example code .

, .

ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(UriBuilder.fromUri("http://localhost:8080/api").build());

Form f = new Form();    
f.add("userId", "foo");    
f.add("deviceId", "bar");    
f.add("comments", "Device");  

Restful.

service.path("processInfo").accept(MediaType.APPLICATION_XML).post(String.class,f);

+6

All Articles