Wednesday, March 26, 2014

Restful , javaEE & json



WebComponent over JAVA EE



Lets think about JSON


JSON is a text-based data exchange format derived from JavaScript that is used in web services and other connected applications. The following sections provide an introduction to JSON syntax, an overview of JSON uses, and a description of the most common approaches to generate and parse JSON.

JSON Syntax

JSON defines only two data structures: objects and arrays. An object is a set of name-value pairs, and an array is a list of values. JSON defines six data types: stringnumberobjectarraytruefalse and null.

The following example shows JSON data for a sample object that contains name-value pairs. The value for the name "phoneNumbers" is an array whose elements are two objects.
{
   "firstName": "Duke",
   "lastName": "Java",
   "age": 18,
   "streetAddress": "100 Internet Dr",
   "city": "JavaTown",
   "state": "JA",
   "postalCode": "12345",
   "phoneNumbers": [
      { "Mobile": "111-111-1111" },
      { "Home": "222-222-2222" }
   ]
}

Class or Interface:
   Main Classes and Interfaces in javax.json
Class or InterfaceDescription
JsonContains static methods to create instances of JSON parsers, builders, generators. This class also contains methods to create parser, builder, and generator factory objects.
JsonReaderReads JSON data from a stream and creates an object model in memory.
JsonObjectBuilder
JsonArrayBuilder
Create an object model or an array model in memory by adding elements from application code.
JsonWriterWrites an object model from memory to a stream.
JsonValueRepresents an element (such as an object, an array, or a value) in JSON data.
JsonStructureRepresents an object or an array in JSON data. This interface is a subtype of JsonValue.
JsonObject
JsonArray
Represent an object or an array in JSON data. These two interfaces are subtypes of JsonStructure.
JsonString
JsonNumber
Represent data types for elements in JSON data. These two interfaces are subtypes of JsonValue.
JsonExceptionIndicates that a problem occurred during JSON processing.

Classes and Interfaces in javax.json.stream
Class or InterfaceDescription
JsonParserRepresents an event-based parser that can read JSON data from a stream or from an object model.
JsonGeneratorWrites JSON data to a stream one element at a time.

Example:
import javax.json.Json;
import javax.json.JsonObject;
...
JsonObject model = Json.createObjectBuilder()
   .add("firstName", "Duke")
   .add("lastName", "Java")
   .add("age", 18)
   .add("streetAddress", "100 Internet Dr")
   .add("city", "JavaTown")
   .add("state", "JA")
   .add("postalCode", "12345")
   .add("phoneNumbers", Json.createArrayBuilder()
      .add(Json.createObjectBuilder()
         .add("type", "mobile")
         .add("number", "111-111-1111"))
      .add(Json.createObjectBuilder()
         .add("type", "home")
         .add("number", "222-222-2222")))
   .build();

Functions of JsonObject 
JsonObjectBuilder add(String name, BigDecimal value)
JsonObjectBuilder add(String name, BigInteger value)
JsonObjectBuilder add(String name, boolean value)
JsonObjectBuilder add(String name, double value)
JsonObjectBuilder add(String name, int value)
JsonObjectBuilder add(String name, JsonArrayBuilder builder)
JsonObjectBuilder add(String name, JsonObjectBuilder builder)
JsonObjectBuilder add(String name, JsonValue value)
JsonObjectBuilder add(String name, long value)
JsonObjectBuilder add(String name, String value)
JsonObjectBuilder addNull(String name)

FileWriter writer = new FileWriter("test.txt");
JsonGenerator gen = Json.createGenerator(writer);
gen.writeStartObject()
   .write("firstName", "Duke")


JSON in Java EE RESTful Web Services

What Are RESTful Web Services?

JavaEE tutorial said that "Representational State Transfer (REST) is an architectural style of client-server application centered around the transfer of representations of resources through requests and responses"
We have to have in mind the following concepts:
Uniform interface
URI
HTTP METHODS  POST,GET,DELETE,PUT  
Self-descriptive messages HTML,XHTML,JSON,XML,Images
Stateful interactions through links
Summary of JAX-RS Annotations (by javaEE tutorial)
AnnotationDescription
@PathThe @Path annotation's value is a relative URI path indicating where the Java class will be hosted: for example, /helloworld. You can also embed variables in the URIs to make a URI path template. For example, you could ask for the name of a user and pass it to the application as a variable in the URI: /helloworld/{username}.
@GETThe @GET annotation is a request method designator and corresponds to the similarly named HTTP method. The Java method annotated with this request method designator will process HTTP GET requests. The behavior of a resource is determined by the HTTP method to which the resource is responding.
@POSTThe @POST annotation is a request method designator and corresponds to the similarly named HTTP method. The Java method annotated with this request method designator will process HTTP POST requests. The behavior of a resource is determined by the HTTP method to which the resource is responding.
@PUTThe @PUT annotation is a request method designator and corresponds to the similarly named HTTP method. The Java method annotated with this request method designator will process HTTP PUT requests. The behavior of a resource is determined by the HTTP method to which the resource is responding.
@DELETEThe @DELETE annotation is a request method designator and corresponds to the similarly named HTTP method. The Java method annotated with this request method designator will process HTTP DELETE requests. The behavior of a resource is determined by the HTTP method to which the resource is responding.
@HEADThe @HEAD annotation is a request method designator and corresponds to the similarly named HTTP method. The Java method annotated with this request method designator will process HTTP HEAD requests. The behavior of a resource is determined by the HTTP method to which the resource is responding.
@PathParamThe @PathParam annotation is a type of parameter that you can extract for use in your resource class. URI path parameters are extracted from the request URI, and the parameter names correspond to the URI path template variable names specified in the @Path class-level annotation.
@QueryParamThe @QueryParam annotation is a type of parameter that you can extract for use in your resource class. Query parameters are extracted from the request URI query parameters.
@ConsumesThe @Consumes annotation is used to specify the MIME media types of representations a resource can consume that were sent by the client.
@ProducesThe @Produces annotation is used to specify the MIME media types of representations a resource can produce and send back to the client: for example, "text/plain".
@ProviderThe @Provider annotation is used for anything that is of interest to the JAX-RS runtime, such as MessageBodyReader and MessageBodyWriter. For HTTP requests, the MessageBodyReader is used to map an HTTP request entity body to method parameters. On the response side, a return value is mapped to an HTTP response entity body by using a MessageBodyWriter. If the application needs to supply additional metadata, such as HTTP headers or a different status code, a method can return a Response that wraps the entity and that can be built using Response.ResponseBuilder.
@ApplicationPathThe @ApplicationPath annotation is used to define the URL mapping for the application. The path specified by @ApplicationPath is the base URI for all resource URIs specified by @Path annotations in the resource class. You may only apply @ApplicationPathto a subclass of javax.ws.rs.core.Application.


Tutorials:


No comments:

Post a Comment