Question: How object automatically converted into JSON in RESTFull Webservice project?
Ans:
Spring’s HTTP
message converter support, you don’t need to do this conversion manually.
Because Jackson 2 is on the classpath, Spring’s
MappingJackson2HttpMessageConverter is automatically chosen to convert the
Class instance to JSON.
For this you have
to import the below line
import
org.codehaus.jackson.map.annotate.JsonSerialize;
For RESTFull
Service you need to copy following jars and paste them inside WEB-INF -> lib
folder in your project. Once you've done that, add those jars to your project
build path as well. Into your project
build path as well.
asm-3.1.jar
jersey-client-1.17.1.jar
jersey-core-1.17.1.jar
jersey-server-1.17.1.jar
jersey-servlet-1.17.1.jar
jsr311-api-1.1.1.jar
Question : How to retrieving and Accessing Array Values in ResultSet
Answer:
As with the JDBC 4.0 large object interfaces (Blob, Clob,
NClob), you can manipulate Array objects without having to bring all of their
data from the database server to your client computer. An Array object
materializes the SQL ARRAY it represents as either a result set or a Java
array.
The following excerpt retrieves the SQL ARRAY value in the
column ZIPS and assigns it to the java.sql.Array object z object. The excerpt
retrieves the contents of z and stores it in zips, a Java array that contains
objects of type String. The excerpt iterates through the zips array and checks
that each postal (zip) code is valid. This code assumes that the class ZipCode
has been defined previously with the method isValid returning true if the given
zip code matches one of the zip codes in a master list of valid zip codes:
ResultSet rs =
stmt.executeQuery("SELECT region_name, zips FROM REGIONS");
while (rs.next()) {
Array z = rs.getArray("ZIPS");
String[] zips = (String[])z.getArray();
for (int i = 0; i < zips.length; i++) {
if (!ZipCode.isValid(zips[i])) {
// ...
// Code to display warning
}
}
}
In the following statement, the ResultSet method getArray()
returns the value stored in the column ZIPS of the current row as the
java.sql.Array object z:
Array z = rs.getArray("ZIPS");
The variable z contains a locator, which is a logical
pointer to the SQL ARRAY on the server; it does not contain the elements of the
ARRAY itself. Being a logical pointer, z can be used to manipulate the array on
the server.
In the following line, getArray is the Array.getArray
method, not the ResultSet.getArray method used in the previous line. Because
the Array.getArray method returns an Object in the Java programming language
and because each zip code is a String object, the result is cast to an array of
String objects before being assigned to the variable zips.
String[] zips = (String[])z.getArray();
The Array.getArray method materializes the SQL ARRAY
elements on the client as an array of String objects. Because, in effect, the
variable zips contains the elements of the array, it is possible to iterate
through zips in a for loop, looking for zip codes that are not valid.
Comments
Post a Comment