Thursday Feb 14, 2008

Content negotiation in Jersey

Jersey serializes JAXB beans as JSON (BadgerFish convention) or XML.

Lets say I have an implementation of my service as below,

@UriTemplate("/myresource/")
public class MyResource {
   @HttpMethod
   @ProduceMime({"application/xml", "application/json"})
   public JAXBBean get() {
      JAXBBean j = ...
      return j;
   }
}

What do you have to do in your client to get the result in JSON format?

Jersey uses HTTP headers to determine value returned: If there is no accept header or the accept is "\*", "application/\*" or "application/xml" then the JAXB bean will be serialized as XML. If the accept header is "application/json" then the JAXB bean will be serialized as JSON. Content negotiation is done solely through the HTTP Accept header.

Another tip?
For testing from a browser try the Firefox add-on Poster. It let's you specify the HTTP method, set HTTP request headers and send arbitrary content in the body of requests.

Friday Feb 08, 2008

An introduction to Jersey

What is jersey?

Jersey is not an island off the north coast of Normandy, France. Nor is it (despite the logo) a soft, plain-knitted fabric used for clothing cyclists.

Jersey is the open source (under the CDDL license) JAX-RS (JSR 311) Reference Implementation for building RESTful Web services.

Let's examine a simple Resource class in Jersey.

 1    // The Java class will be hosted at the URI path "/helloworld"
 2    @UriTemplate("/helloworld")
 3    public class HelloWorldResource {
 4    
 5        @HttpContext
 6        private UriInfo context;
 7
 8        /\*\* Creates a new instance of HelloWorldResource \*/
 9        public HelloWorldResource() {
10        }
11
12       /\*\*
13       \* Retrieves representation of an instance of hello.world.HelloWorldResource
14       \* @return an instance of java.lang.String
15       \*/
16       @HttpMethod("GET")
17       @ProduceMime("text/plain")
18       public String getClichedMessage() {
19       //Return some cliched textual content
20       "Hello World! Here is " + context.getAbsolutePath();
21       }
22    }

The HelloWorldResource class is a very simple Web resource. The URI path of the resource is "/helloworld" (line 2), it supports the HTTP GET method (line 16) and produces cliched textual content (line 20) of the MIME media type "text/plain" (line 17).

Java annotations are used to declare the URI path (line 2), the HTTP method (line 16) and the MIME media type (line 17).

Also note the @HttpContext annotation (line 5), which acts as a marker to say "Please inject an instance of the Java type, in this case UriInfo, after the class has been constructed." This use of annotations is a key feature of JSR 311.

Wednesday Jan 02, 2008

Social Software for Glassfish available!

Sun recently released its "Social Software for GlassFish". It is available through the update center for 9.1 and 9.1 UR1 releases. [Read More]

Tuesday Jul 17, 2007

JSFTemplating and Netbeans

I came across a very useful tutorial on JSFTemplating - NetBeans Project Tutorial . It's a really neat way to get started and worked perfectly for me.

Wednesday Jun 27, 2007

Unconfiguring your update center module

If you want to unconfigure your update center module, one hack to do this would be to edit GF_HOME/domains/domain1/config/domain-registry (domain-registry for your particular domain instance)

Change this property to false

mymodule_configurator_version.configured=false

Then restart your Application server so that unconfigure can explicitly be called.

If you want to reconfigure your module, just set the property to true and restart the application server.

To uninstall,

 asadmin uninstall-addon mymodulename 

About

manveen

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today