I just want to implement a service in java that will:
take some arguments, then search the database
return the JSON object of the fetched data
I need help to identify the ways through which I c开发者_运维问答an implement this thing.
e.g. Suppose I am getting the name of the book as argument I want to render.
On service part, I have to fetch book data and convert it to JSON and write/return to response.
I was looking at the Apache Axis2
but I am not sure that I am going in the right direction.
So, pls help.
Need guidelines not implementation.
Thanks
I would suggest using JAX-RS based services which would be ideal for your scenario as you want json data. These are pretty easy to get started with. Jersey is a widely used frameworks. Also see RESTEasy.
If you are returning the data in JSON then you probably don't need to implement a full web service, which uses XML for both the request and the response.
A normal dynamic web application (written as a Java Servlet) will be able to read request parameters in the HTTP payload and return a JSON-encoded HTTP response.
However you need to consider your clients; if they are only able to access web services then you need to forget about a JSON response and simply objectify the response. However if the clients can access web resources without issue then go with the servlet approach.
If you need to go with web services then look at the Metro 2 framework.
One way to do this is to keep it standards-based.
If you are using the JEE5/6 framework, your best bet would be to go with JAX-WS
- comes built-in with the JSE too (if I remember correctly)
You really just have to annotate a POJO with @WebService
to achieve this.
Regarding creating a JSON response, a good bet is to stick with the implementation from http://code.google.com/p/google-gson/ ; simple and straightforward
Axis2
can handle/support the webservice related part, iaw, transforming java objects into JSON and vice-versa and providing an easy-to-use API for the communication part.
Hibernate or JPA could be useful for database related tasks, although it might be easier to just use JDBC to send some simple SQL commands to the database (especially if the database already existst).
精彩评论