![]() |
| Woodstock Dataprovider Entity Example |
The project was updated using NetBeans 6.5.1.
The code for the project can be found on Bitbucket here: WoodstockJPAApplication
![]() |
| Woodstock Dataprovider Entity Example |
![]() |
| Multiple Selection Table |
![]() |
| Single Selection Table |
![]() |
| Collapsible Group Table Example |
![]() |
| Master-Detail Example |
| http://commons.wikimedia.org |
@Singleton.@Singleton that manages the "Widgets" that are created using the HTTP POST. It is a simple and fun example of what you can do with JAX-RS.![]() |
| JSON Response |
HttpHeaders to determine the username and password for a request. I didn't find a quick answer so I wrote a simple utility method that will return the Base 64 decoded values from the authorization header.HTTPBasicAuthFilter to allow you to authenticate. Its that simple... really.
package com.bluelotussoftware.jersey;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.LoggingFilter;
/**
* REST Basic Authentication Client Application
*
* @author John Yeary
* @version 1.0
*/
public class BasicAuthenticationClient {
public static void main(String[] args) {
ExampleResourceClient erc = new ExampleResourceClient();
erc.setUsernamePassword("jyeary", "test");
System.out.println(erc.getMessage());
erc.close();
}
static class ExampleResourceClient {
private com.sun.jersey.api.client.WebResource webResource;
private com.sun.jersey.api.client.Client client;
private static final String BASE_URI = "http://localhost:8080/secure-jdbc-rest-service/resources";
public ExampleResourceClient() {
com.sun.jersey.api.client.config.ClientConfig config = new com.sun.jersey.api.client.config.DefaultClientConfig();
client = com.sun.jersey.api.client.Client.create(config);
client.addFilter(new LoggingFilter());
webResource = client.resource(BASE_URI).path("example");
}
public String getMessage() throws com.sun.jersey.api.client.UniformInterfaceException {
WebResource resource = webResource;
return resource.accept(javax.ws.rs.core.MediaType.TEXT_PLAIN).get(String.class);
}
public void putMessage(Object requestEntity) throws com.sun.jersey.api.client.UniformInterfaceException {
webResource.type(javax.ws.rs.core.MediaType.TEXT_PLAIN).put(requestEntity);
}
public void close() {
client.destroy();
}
public void setUsernamePassword(String username, String password) {
client.addFilter(new com.sun.jersey.api.client.filter.HTTPBasicAuthFilter(username, password));
}
}
}
CREATE TABLE users ( username varchar(255) NOT NULL, password varchar(255) DEFAULT NULL, PRIMARY KEY (username) );
CREATE TABLE groups ( username varchar(255) DEFAULT NULL, groupname varchar(255) DEFAULT NULL);
CREATE INDEX groups_users_idx ON groups(username ASC);
![]() |
| Create Tables |
This is an example message
14:23:30,058 INFO [TomcatDeployment] deploy, ctxPath=/admin-console
14:23:30,257 INFO [config] Initializing Mojarra 2.0.9 (SNAPSHOT 20120202) for context '/admin-console'
14:23:45,013 INFO [application] JSF1048: PostConstruct/PreDestroy annotations present. ManagedBeans
methods marked with these annotations will have said annotations processed.
curl -HAccept-Encoding:gzip -HAccept:application/json http://localhost:8080/content-coding-gzip/webresources/widget > json.gz
gzip -v -l json.gz
method crc date time compressed uncompressed ratio uncompressed_name defla 3a79ae18 Jan 20 15:25 1370 3389 60.3% json
curl -X OPTIONS -v http://localhost:8080
* About to connect() to localhost port 8080 (#0) * Trying ::1... Operation not permitted * Trying 127.0.0.1... connected > OPTIONS / HTTP/1.1 > User-Agent: curl/7.22.0 (i686-pc-cygwin) libcurl/7.22.0 OpenSSL/0.9.8r zlib/1.2.5 libidn/1.22 libssh2/1.2.7 > Host: localhost:8080 > Accept: */* > < HTTP/1.1 200 OK < X-Powered-By: Servlet/3.0 JSP/2.2 (GlassFish Server Open Source Edition 3.1.1 Java/Sun Microsystems Inc./1.6) < Server: GlassFish Server Open Source Edition 3.1.1 < Allow: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS < Content-Length: 0 < Date: Tue, 17 Jan 2012 14:31:17 GMT < * Connection #0 to host localhost left intact * Closing connection #0As you can see the command reports that GlassFish supports all standard HTTP 1.1 methods as denoted by the Allow: header. This example calls a specific resource on the server which only supports a subset of the HTTP methods based on the annotations in the resource. The methods always supported by a resource are HEAD and OPTIONS, but this has two methods annotated as
@GET and @POST. Additionally, not that the Content-Type: application/vnd.sun.wadl+xml is returned which includes the WADL for this particular resource.
curl -X OPTIONS -v http://localhost:8080/RESTApproachFormParameter/resources/example
* About to connect() to localhost port 8080 (#0) * Trying ::1... Operation not permitted * Trying 127.0.0.1... connected > OPTIONS /RESTApproachFormParameter/resources/example HTTP/1.1 > User-Agent: curl/7.22.0 (i686-pc-cygwin) libcurl/7.22.0 OpenSSL/0.9.8r zlib/1.2.5 libidn/1.22 libssh2/1.2.7 > Host: localhost:8080 > Accept: */* > < HTTP/1.1 200 OK < X-Powered-By: Servlet/3.0 JSP/2.2 (GlassFish Server Open Source Edition 3.1.1 Java/Sun Microsystems Inc./1.6) < Server: GlassFish Server Open Source Edition 3.1.1 < Allow: OPTIONS,POST,GET,HEAD < Content-Type: application/vnd.sun.wadl+xml < Content-Length: 1085 < Date: Tue, 17 Jan 2012 15:01:49 GMT < * Connection #0 to host localhost left intact * Closing connection #0In summary, you can take advantage of the OPTIONS method to determine what a particular resource will provide for you without any knowledge of the subject domain.
Accept: */* header which details which media types the client can accept. The server simply returns whatever we have coded for example @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}).
However I bet a number of you have seen something like this before:
http://www.example.com/documents/mydoc?format=docThe first example is not very subtle. It tells you the client wants a mydoc.doc by requesting a specific format. The second example is more subtle, and the end user (client) may not notice. I made it a "little" more obvious by changing it to .xml. However, most folks would have missed it if I used mydoc.doc. You would have assumed it was a MS Word document to start with. In actuality, the media type returned here is controlled by the file extension.
http://www.example.com/documents/mydoc.xml
Response being wrapped as demonstrated in a previous post: JAX-RS Tip of the Day: Use Response to wrap... Responses.![]() |
| Client Negotiation Examples |