How can I communicate Servlet from Swing - java

I develop a CRUD Spring, Servlet/JSP application. I will develop a desktop Swing application with same functionality. My question is that possible to communicate and resuse servlets on swing application (not applet)?

Yes, by making http requests. You can do them with multiple tools. Two of them:
new URL(..).openConnection()
apache http components
since you tagged spring, if you are using it with swing, you can use its RestTemplate. It's used to invoke restful services. You can expose your logic as restful services through the servlets (or spring-mvc), and consume them with RestTemplate

The Rest idea above is pretty good. In fact, by using Spring and it's proxies, you could use any kind of webservices (RESTFul or WSDL), and Spring would make it transparent for you.
Please check some examples in the Spring manual. Take a look at how to create proxies using service interfaces.
Best regards.

since its all via HTTP, and mechanism as suggested above can work.
Or you can even open bytestream to the server and read/write using that stream.

Related

Solution for easy generating rest services?

We have a application which works with MySql database.
Now we are implementing mobile version of our application and I'm looking for java solution for easy generating rest services from already ready sql queries.
Details on security and performance:
Security is required (LDAP).
Performance - working time of rest request should be approximately equals working time of sql request.
What solutions can be used for it?
I think the best solution is REST4Enterprise
also can take a look on and restSQL
I suggest using spring roo very easy to use
I recommend using netbeans to generate rest webservices from database tables.
Netbeans also offers the ability to generate REST webservices from entity beans (JPA entities).
Have a look at this link.
If you are looking for just CRUD REST services - one of the above tools suggested by others can help.
But for something more check - Dropwizard
my 2c
For Mobile applications, REST is the best recommended architecture. But you have to ensure the session handling at the client side (recommended).
Eclipse -> Webservice -->Create a Sample Rest Webservice will be a good starting point
You can use spring-rest or grails to develop rest services.
Consider doing web services in PHP. No need for anything other than Tomcat, and offers LDAP verification. It is very quick time to market, and PHP works with MySql pretty seamlessly.
You can give a look at Developing REST Web Services Tutorial.. not sure if it's the best solution but could be one... I used it twice and found it usefull and really easy.
In the Java™ world, you can build a RESTful web service in several ways: Some folks use JSR 311(JAX-RS) and its reference implementation Jersey, others use the Restlet framework, and some might even implement from scratch. Spring framework for building Java EE applications, also supports REST in its MVC layer.
http://www.ibm.com/developerworks/webservices/library/wa-spring3webserv/index.html
Below article also covers RESTful web services api using java and mysql
http://www.9lessons.info/2012/09/restful-web-services-api-using-java-and.html

Remoting from iOS to Java/Spring backend

In our application we have an Adobe Flex client that communicates to our Java/Spring backend via a facade (using AMF) that is exposed via Spring.
Any recommendations on how I could leverage this facade to make remote calls from iOS? Note that I would prefer backend frameworks that would be reusable from other clients (Android, etc).
I hear about JSON & RESTful web services. Would there be a way to rather easily get existing facade services to be exposed as RESTFul web services that uses JSON for object serialization?
Or would you recommend something different?
Any information and/or pointers will be appreciated!
Update:
So we have one option so far for this: JSON requests and responses via Spring
Anybody want to suggest any other ways?
Spring supports JSON requests and responses (see for instance, this article from the Spring In Practice blog), largely through the use of annotations.
While I don't think that it is likely that you'll be able to go through your existing AMF facade, I think that it should probably be pretty straight-forward to create a JSON-over-HTTP facade using the same underlying Spring controllers (assuming that you're using Spring MVC).
Edit: Whether the JSON-over-HTTP facade that you create is truly RESTful depends largely on your implementation.

For performance, Spring webservices or pure restful jersey even if i use MVC?

im using spring MVC and webflow to create a game server and serve some web pages to the users. Thing is, the javascript game will also make multiple ajax calls to restful services on the same server for some game logic. While the web page serving performance is not critical, the restful service calls need to be as efficient as possible (efficient as in response time).
For performance of the services, would it be better to use pure JAX-RS (jersey) web service calls without the spring ws overhead (since i understand the spring layer could affect performances negatively) or would it be the same if i used the spring webservices framework instead and maintain integration with the rest of the spring family?
thanks!
There aren't many clear benchmarks out there, but take a look here:
http://www.techempower.com/benchmarks/
It clearly shows the overhead of using Spring. Compared to Servlets that serve JSON manually Spring is "slower". Personally, I don't think that Spring cuts development time, unless you are very much familiar with it. Creating a simple servlet that will act as a REST API is very simple. Take a look at the servlet code from the benchmark:
Servlet benchmark
I don't think Spring per se will affect performance negatively. Where did you hear that?
Spring web services are "contract first" SOAP services. If you're saying that you don't want to pay the overhead of SOAP, XML, marshalling and unmarshalling, then you have a valid point. That's true whether you use Spring to implement the services or not.
REST is HTTP, so it's a natural for AJAX calls.
I believe Spring 3.0 supports REST, so it's not a "Spring or not Spring" choice:
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/new-in-3.html#d0e1188

Can we use php web services in java?

I have a web application in JAVA (Spring MVC framework) and I want to connect to a website and use its web services which wrote in php, is it possible ? if yes how can I do that?
It doesn't matter what language one used to create a webservice. You can talk to any webservice by agreeing on how it exchanges messages.
Yes, Spring integrates well with Apache Axis: http://axis.apache.org/axis2/java/core/docs/spring.html
To consume web services with Spring you can use JaxWsPortProxyFactoryBean to create a client proxy: http://musingsofaprogrammingaddict.blogspot.com/2009/03/writing-and-testing-jax-ws-clients.html
From Spring in Action, here's how you'd configure an example web service proxy bean:
<bean id="spitterService" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean"
p:wsdlDocumentUrl="http://localhost:8080/services/SpitterService?wsdl"
p:serviceName="spitterService" p:portName="spitterServiceHttpPort"
p:serviceInterface="com.habuma.spitter.service.SpitterService"
p:namespaceUri="http://spitter.com"/>
The beautiful thing about web services, and web services are beautiful things, their hosts are made with wsdl's, their client is made in Spring! (Totally stole that from the mouse (just a warning, as per the comments, this rhyme is not to be used as an authoritative anything. It is an attempt at humor by someone who was excessively tired at 3 AM because of Win 7 issues))
Web services are language agnostic -- the only thing which matters is that they are up and running (and serving valid outputs), not that they are using technology X, Y, or Z. Theoretically, you could even have a webservice running on something written in Piet or Brainf*ck.
A tutorial on how to create a client can be found here.
Use CWSDLGeneratorDocument.php for generate WSDL file in DOCUMENT/LITERAl style and use standart library JAX-WS in java 1.7.

CDI/JSF and JAX-RS?

I would like to use RESTful services in a CDI/JSF2 application. I am not very familiar with JAX-RS however I have read that its lifecycle does not play well with CDI/JSF2. Is it possible to incorporate JAX-RS with CDI/JSF2 in a JEE6 stack? If not are there alternatives?
thanks
You can use JAX-RS just fine along side a JSF application, however, they don't play well together. For instance, you can't use JAX-RS to have "http://localhost:8080/story/92/" return a JSF page with a Story entity with ID 92.
You can, however, use PrettyFaces to do something very similar to this.
JAX-RS makes sense for non-JSF resources. "http://localhost:8080/story.xml" makes perfect sense for JAX-RS.
You can call your rest services from your JSF templates. For example you can run your rest services from http://localhost:8080/services/ while your JSF page is returned on http://localhost:8080/index.xhtml. You can then write some custom javascript to call your rest services from your JSF page. This is actually the general practice with REST services so they can be consumed by many different UIs. JSF may not be the best for consuming REST services, but it is possible.

Categories