Java Reference
In-Depth Information
public
public static
static < T > Entity < T > xml ( final
final T entity ) { }
public
public static
static < T > Entity < T > json ( final
final T entity ) { }
public
public static
static Entity < Form > form ( final
final Form form ) { }
...
}
The xml() method takes a Java object as a parameter. It sets the MediaType to applica-
tion/xml . The json() method acts similarly, except with JSON. The form() method deals
with form parameters and application/x-www-form-urlencoded , and requires using the
Form type. There's a few other helper methods, but for brevity we won't cover them here.
Let's look at two different examples that use the POST create pattern to create two different
customer resources on the server. One will use JSON, while the other will use form paramet-
ers:
Customer customer = new
new Customer ( "Bill" , "Burke" );
Response response = client . target ( "http://commerce.com/customers" )
. request ().
. post ( Entity . json ( customer ));
response . close ();
Here we pass in an Entity instance to the post() method using the Entity.json() meth-
od. This method will automatically set the Content-Type header to application/json .
To submit form parameters, we must use the Form class:
package
package javax . ws . rs . core ;
public
public class
class Form
Form {
public
public Form () { }
public
public Form ( final
final String parameterName , final
final String parameterValue ) { }
public
public Form ( final
final MultivaluedMap < String , String > store ) { }
public
public Form param ( final
final String name , final
final String value ) { }
public
public MultivaluedMap < String , String > asMap () { }
}
This class represents application/x-www-form-urlencoded in a request. Here's an ex-
ample of it in use:
Form form = new
new Form (). param ( "first" , "Bill" )
. param ( "last" , "Burke);
response = client.target(" http: //commerce.com/customers")
. request ().
. post ( Entity . form ( form ));
response . close ();
Search WWH ::




Custom Search