1783289538 by Unknown
Author:Unknown
Language: rus
Format: mobi
Published: 2014-02-13T05:57:12+00:00
Chapter 6
Let's create a new contact by performing an HTTP POST request with curl.
$ curl --verbose --header "Content-Type: application/json" -X POST
-d '{"firstName": "FOO", "lastName":"BAR", "phone":"987654321"}'
http://localhost:8080/contact/
The contact is created, and the value of the inserted row's primary key, that is, the contact id, is 174, as you can see (the Location response header) in the following screenshot: JDBI's SQL Object API simplifies the creation of DAO. We created the DAO interfaces on which we can map plain, parameterized SQL queries to specific methods using the @SqlQuery annotation; note that apart from the object mapper, no additional implementation is needed.
Since we are retrieving data from the database and returning a Contact instance, we needed to create a Mapper class, which is a class that implements the org.skife.jdbi.
v2.tweak.ResultSetMapper<T> interface for the Contact class. Its implementation was fairly simple and straightforward. We created a Contact object with the values we got from the database ResultSet object using the #getLong() and #getString() methods and providing the column name.
We used jdbi to create our DAO instances within our resource class using the DBI#onDemand() method. However, in order to do that, we had to create a DBI factory and build the DBI instance prior to registering our resources. Again, this was pretty simple, and required minor modifications in the App#run() method.
49
Using a Database
The DBI factory requires the database connection settings in order to build the DBI instance. Going one step back, we had our configuration class updated to read and expose the DatabaseConfiguration settings, which were declared in the database section of the applications configuration file, that is, config.yaml.
There's moreā¦
JDBI identifies itself as an SQL convenience library for Java. We used the JDBI SQL Object API where a particular method is mapped to a specific SQL statement. However, this is not the only way of using JDBI to interact with a database. JDBI exposes another API too, that is, the fluent style API.
The JDBI fluent style API
The fluent style API allows us to open and use a database handle to create and execute SQL queries on demand on the fly, instead of using the predefined SQL statements that the SQL Object API utilizes.
Generally, the type of API that you should use depends on your personal taste, and you can even mix both APIs together.
The @MapResultAsBean annotation
In this example, we have implemented a mapper and used the @Mapper annotation in order to map the result of an SQL query to a Contact instance. An alternative approach would be the use of the MapResultAsBean annotation.
@MapResultAsBean
@SqlQuery("select * from contact where id = :id")
Contact getContactById(@Bind("id") int id);
By annotating #getContactById() in this example, we map the result of the SQL
query directly to a Contact instance, without needing to implement a custom mapper.
In order for this to work though, the Contact class should be updated with setters (that is, setFirstName(String firstName){ .. }). Due to this, the final keyword will have to be removed from the declaration of each member variable.
50
7
Validating Web Service
Requests
Up to this point, we have a RESTful Web Service that produces JSON representations and is also capable of storing and updating contacts.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Becoming Supernatural by Dr. Joe Dispenza(8226)
Crystal Healing for Women by Mariah K. Lyons(7936)
The Witchcraft of Salem Village by Shirley Jackson(7280)
Inner Engineering: A Yogi's Guide to Joy by Sadhguru(6800)
The Four Agreements by Don Miguel Ruiz(6770)
The Power of Now: A Guide to Spiritual Enlightenment by Eckhart Tolle(5790)
Secrets of Antigravity Propulsion: Tesla, UFOs, and Classified Aerospace Technology by Ph.D. Paul A. Laviolette(5374)
The Wisdom of Sundays by Oprah Winfrey(5168)
Room 212 by Kate Stewart(5130)
Pale Blue Dot by Carl Sagan(5013)
Fear by Osho(4743)
The David Icke Guide to the Global Conspiracy (and how to end it) by David Icke(4724)
Animal Frequency by Melissa Alvarez(4478)
Rising Strong by Brene Brown(4468)
How to Change Your Mind by Michael Pollan(4361)
Sigil Witchery by Laura Tempest Zakroff(4250)
Man and His Symbols by Carl Gustav Jung(4143)
Real Magic by Dean Radin PhD(4134)
The Art of Happiness by The Dalai Lama(4133)