I am looking for a way to setting up a JSON proxy client in a spring framework way.
We are going to use Spring MVC on the server side. We don't like XML as they are overkill and heavy. JSON seem to be a lightweight and effective message container for us.
However, I've search around and read http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/remoting.html many times and I don't find any hits to put my spring client into a JSON client.
They provided RMI, Http, JAX-WS, SOAP and others. But nothing related to as a client of MVC (which I guess it could be common as we don't want to write it twice)
RestTemplate looks good but I am wonder is it the suggested way to do in spring 3.0.
The RestTemplate is indeed the preferred way of accessing rest services.
I've been in the same position as you - looking through the Spring docs for how to implement a simple JSON client API. I ended up implementing it myself, as I only needed it for a few RPC-like calls to another webapp. IIRC Jax-RS has this capability so you might want to invest in implementing it - for my needs it seemed overkill.
All there is to it:
write a simple method to perform the HTTP GET to the JSON web service and return a String (I used Apache HttpClient)
pass the String to Jackson to deserialize into a Java object (see mapper.readValue())
This assumes you already know what kind of object you expect to get back from a given JSON web service.
As an aside, the other thing I needed from my Spring MVC JSON web service was the ability to do JSONP (cross site callback) for consumption in the browser with JQuery (note: JSONP is not secure so use at your own risk). The automagic Spring JSON webservices that Bozho outlined does not provide an option for JSONP. The easiest way to provide JSONP is to implement a simple servet filter.
Related
I have a spring mvc application which i use as only a server. I have deployed some html and javascript files on my hosting account and sending post requests to my server only. Retrieve data from database and send back the data to the html or javascript pages. I don't use any servlets,jsp or jsf. Everywhere i look it says that i should use them however. Am i doing something wrong? It feels like bad practice but i don't know the right way to do it i guess. Any help would be appreciated.
As I already mentioned in my comments, you can look into creating a webservice. Since you are using Spring already, try this guide on Building a RESTful Web Service.
After creating the service, you can call it up like this, where you pass in input parameters to your rest endpoints,
http://localhost:8080/greeting?name=User
Now your service will respond back to your GET request and you'll be output a JSON/xml string which you can process later at the client side. The sample json response looks like this,
{"id":1,"content":"Hello, User!"}
Here's another example blog article on Spring Restful Web Service Example with JSON, Jackson and Client Program
Well, I think you should first expand your question a bit since one needs to assume a lot to give you an answer.
IMO, JSF is not great and there are other better options for UI and JSPs are dead. But I'm biased since I used last time JSF 1.X and then went with Spring MVC and Angular or Spring and Apache Wicket more recently.
I used for instance Spring MVC to implement an HTTP RPC API with JSON payloads to which an Angular frontend connected and it worked just fine. I also had the feeling that everyone is doing that nowadays:).
I also guess you are using at least one servlet to configure Spring DispatcherServlet. Right?
I researched a lot about it, but I wasn't able to reach a conclusion about this matter.
I'm creating a new front-end in GWT, using GWT-Platform and GIN, for an existing application. But I can't figure out which is the best way to interact with the existing REST API.
What I found up to now is that I can use RequestBuilder to do the calls, and that also exists the RestyGWT framework for REST communications. But I don't know how to integrate any of them with GIN Injector. And I have doubts on how to translate the JSON return from the service to the JTO available in the client code translated by GWT.
The last one specially due to a legacy code that translate the Beans from the server to a kind of generic Json format.
So what I want to know is do anybody have experience integrating legacy backend to a new GWT front-end with REST. How they integrate both? How they solve, if experienced, the Beans integration?
I agree with Ümit, If you are worried about the "communication" between backend and frontend don´t get stress:
Something like:
public String serializeToJson(YoutEntity report) {
AutoBean<YoutEntity > bean = AutoBeanUtils.getAutoBean(report);
return AutoBeanCodex.encode(bean).getPayload();
}
public YoutEntity deserializeCompanyFromJson(String json) {
AutoBean<YoutEntity > bean =
AutoBeanCodex.decode(factoryYourEntity, YoutEntity .class, json);
return bean.as();
}
is perfectly possible using Autobeans!
And using GWT you can share your entities between Client and Server, so you don´t need to touch anything.
Also, in our last project using Apache Wink as a REST client, in the server using the correct annotations we were able to have the Entity automatically from the JSON, so is even easier (but I think most of the REST libraries can do the same).
Thanks!
Your question touches a couple of different aspects both client-side but also server-side.
In general there is nothing special about integration between GWT and a REST API.
On the GWT side there are different ways to consume a REST API:
RequestBuilder to manually create the HTTP Requests to the REST API. You have to parse the JSON payload. There are different ways to do this:
Javascript OVerlay Types.
3rd party library (Piriti)
AutoBeans
RestyGWT (backend + GWT. See this tutorial)
Restlet has a GWT client (see this thread for more details).
GIN per se doesn't have anything to do with the communication with a REST API.
It's only responsible for dependency injection on the client side.
The translation of beans to JSON depends on the backend. Spring can basically automatically serialize Java beans into JSON using Jackson.
Evening all :)
I'm looking to create a Java web application. I envisage that it will use Spring web MVC and JSPs, however I would like to expose certain functionality as REST calls so I can create an android client.
Does spring offer anything to help me in this area? How can I keep the REST code and the web front end code separate yet not have to maintain essentially 2 versions of my application (one for the web, one for REST clients).
Not looking for spoon feeding, just some pointers of where I should start reading.
As others have mentioned, Spring has pretty good in-built REST support now. When combined with annotations, this allows really simple set-up of a RESTful API. Spring can be configured with different view resolvers, which can automatically respond with a different view of the data depending on the Accept header for example. So you could return either JSON or JSP automatically from the same data, see the ContentNegotiatingViewResolver. Your Controller and Model can then be common and implemented once, leaving the work in the View layer.
I've used this approach before to return JSON when the request was via AJAX and a JSP view built with the same data when accessed by a browser.
Jersey is a pretty nifty tool. It integrates well with tools like Spring, Guice, and Jackson to provide you a pretty seamless way to create RESTful resources.
Jersey is pretty simple, works well, and serves as the reference implementation to boot. Plus, it has some nice REST client support, much of which will probably make it into the JAX-RS spec.
In terms of marrying that with Spring MVC, I'd recommend you make sure you model your application so that you have facades (Service classes) that provide all the core functionality you need and then simply reference them as needed in your MVC code or REST code. You shouldn't be duplicating business logic
You can do this using Spring 3.0. Spring 3.0 came out with the ability to specify #PathVariables to pull values out of the URL path (previously this was not easy in Spring MVC).
You would also use #RequestMapping to specify the HTTP method(s) each method in your controller should respond to.
I've also use Spring Security to implement an API key type of functionality. This way you can restrict access to your API in a way that is easy for REST clients to implement. I had to extend org.springframework.web.filter.GenericFilterBean and add a the proper Authentication like this
SecurityContextHolder.getContext().setAuthentication(apiKeyAuth)
There's a new REST API in Spring 3.0 MVC:
http://blog.springsource.com/2009/03/08/rest-in-spring-3-mvc/
http://www.springsource.org/download
Apache CXF integrates well with Spring and offers many method for exposing services. From the overview section of the CXF homepage:
CXF helps you build and develop
services using frontend programming
APIs, like JAX-WS and JAX-RS. These
services can speak a variety of
protocols such as SOAP, XML/HTTP,
RESTful HTTP, or CORBA and work over a
variety of transports such as HTTP,
JMS or JBI.
I am developing a web service in java and Metro that requires a lot of information to be passed. For example, something like xml describing all the attributes of a customer.
I am wondering if there is some standard way in which to pass the data in a document. Currently I have been passing the data as a string parameter named 'customerXML'.
Any suggestions appreciated. FYI I have defined another restful ws using RestEasy which works great using input/output streams, but am looking for a way to leverage soap-based web services to expose similar functionality.
JAX WS is perfect for this requirement, It works on SOAP
My hypothesis is that there isn't a standard way to pass xml documents to a soap-based web service without coding the entire SOAP message yourself. Hence I do not think there is an easy way to do so and one reason why RESTful web services are gaining acceptance. The best way to do it using SOAP based web services is to pass the document as a string parameter and validate/parse within your server code.
Basically I need webservice where client can request with id one boolean value from our webservice. What technology would be most suitable for this small API? Of course it is possible that there will be more functions to interface, but now we need only one function. It also needs to have authentication, so that only auhtorized clients can access service. And every client have different auth credientials.
What would be good technology for this purpose?
I am using resteasy to build my webservices and it is pretty easy to use ... just need to use annotations on my methods to deliver the webservices.
Here is a comparison of different JAX-RS frameworks. Take a look at it
First of all: authentication and authorization. Don't do it yourself, pick an app server or servlet container and configure it to do the job.
For the web service....
The simplest thing to do it just implement a servlet that responds to a POST (not a GET if request modifies internal state) and returns the result in the body. This way you don't need any frames works, no learning to do (if you already know servlets). The downside is it won't scale as you add more features, and your not using enough buzz words.
If you want a SOAP based webservice, then look at JAX-WS. Now that it's backed into java 6 it's pretty easy.
At the simplest level JAX-WS lets you put a few annotations on your class, like #WebService, and it auto generates a wsdl and exposes an instance of your class via the web service.
There is plenty of documenation out around how to do it:
http://java.sun.com/webservices/docs/2.0/tutorial/doc/
http://www.java-tips.org/java-ee-tips/java-api-for-xml-web-services/developing-web-services-using-j.html
http://cwiki.apache.org/GMOxDOC20/simple-web-service-with-jax-ws.html
JAX-WS + any servlet container (Tomcat is usual choice)
#WebService(targetNamespace = "http://affinity.foo.com", name="RewardsStatus")
#SOAPBinding(style=SOAPBinding.Style.RPC, use=SOAPBinding.Use.LITERAL)
public interface RewardsStatusIF {
#WebMethod(operationName="GetLastNotificationDate", action="urn:GetLastNotificationDate")
#WebResult(name="return")
public Date getLastNotificationDate() throws AffinityException;
...
Actually, you don't even need a servlet container. JAX-WS has a way to run the service under a standalone Java application. It has some limitations (I have failed to make a stateful service work), but it's
very simple to create.
Given that you tagged your question as "Java", I suggest Jetty. It is a very good small servlet engine. It has support for sessions, so adding authentication should not be a problem.
If you are using Java 6, there is already a HTTP Server builtin, it supports Http authentication. That's all you need. Check out,
com.sun.net.httpserver
You could use some restful framework like jersey.
An alternative to SOAP-based web services with JAX-WS would be JAX-RS (for RESTful web services).
We have a lot of scenarios on our project where we want small amounts of data available via simple HTTP URLs while the app is running and in my experience, Restlet (http://www.restlet.org/) seems to be one of the easiest things available for setting up simple "web-service"-like interfaces (RESTful interfaces) within Java apps.