Creating Java Spring MVC 3 based application with services, - java

I am new to Spring MVC3 framework in java but I am familiar with java coding.
I want to write two application using this framework.
First application recieves requests through a SOAP web services and sends response in form of SOAP XML Object.
Second application have a simple servlet to recieve request and send responces.
I have studied Java MVC3 framework. It requires view to be called who are mapped against which controller will handles its request. But,
How I can do this using a webservice, so that when a specific method using SOAP services is called, I can forward that request to its relevant servlet and sends response back as a SOAP xml file.
How can I do this for my second application as well that recieves request through a servlet.
I hope all this make sense.
regards,
Aqif

If you want to stick with Spring, you can use Spring Web Services for application 1. Application 2 would be a more traditonal Spring Web app (uses a servlet, but framework does not require you to work in the servlet...instead you will work in more fine grained components).
If you dont want to stick with Spring for the web services, you can always use something like Apache Axis

The usual structure is as follows:
you have spring-mvc controllers to handle your browser requests
you have other components that handle the SOAP requests
both of the above invoke the same underlying services which serve them with the data that is to be sent to the user. The data is in java objects, which are later transformed to whatever is required
For the 2nd point you can pick some JAX-WS implementation, like CXF (it has nice spring support as well)

Spring Web Services specifically supports a Spring MVC-like model for responding to SOAP calls, as you describe.
the second one is Spring MVC directly. Heck, it sounds like - though I can't be sure without more information - that you're trying to build RESTful web services. There, too, Spring MVC is the right choice.

Related

Should I use Spring restful web service or controllers to generate json?

I have built a web app using Spring 4. The app allows search users, create elements and so on.
The requirements changed. Now I have to deliver search results as json instead of html (but keep html just in case).
The question is, should I use controllers and redirect to a jsp with json/html according a parameter? or use Spring restful web service?(I've never used this and need to learn how to) if I use Spring restful do I have to do the same job I've already did with the web version to generate json?
It depends on how did you design your previous MVC application.
My suggestion is follow:
But better to use Spring REST Controller. Mechanism is more or less same like MVC. I assume you have service layer for CRUD data of your application.
Better to use those Service and Repository and just create #RestController for your application. #ResponseBody should be your models which are you returning from controller. And #RequestBody will hold the data for generaly POST, PATCH, DELETE requests.
Good luck.

Spring mvc - Server side programming

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?

Which Web technologies allow for the development of static user interface/presentation layers

I have a Spring-WS web service that runs on a Jboss application server. I also have a Spring-MVC application running on a separate server running Jboss 7. The Spring-MVC application is used mainly for the user interface. The Spring-WS application contains all the services for the business logic.
The standard approach to use the components we have would be in the following order:
Client Browser ---(HTTP Request)---> Spring MVC ----(SOAP Request)-----> Spring-WS
Client Browser <---(HTTP Request)--- Spring MVC <----(SOAP Response)<----- Spring-WS
There is a requirement to change some of the requests coming from the Client Browser to go direct to the Web Services instead of via the Spring MVC application. The Spring MVC application will be used to load the initial presentation screens but the actions that involve any updates/writes will be going through the Spring-WS process.
To achieve this, we have a bespoke process which runs on the same machine as the client browser that traps all HTTP requests. Its purpose is to convert the request to a SOAP message and the response to a HTTP response. The path is shown below:
Initial Request (Retrieve presentation/user interface)
Client Browser ---(HTTP Request)---> Spring MVC
Subsequent requests
Client Browser ---(Http Request) ----> SOAP Converter (Local process) ------> (SOAP Request) ----> Spring-WS
Client Browser <---(Http Request) ---- SOAP Converter (Local process) <------ (SOAP Request) <---- Spring-WS
There are two paths that happen in the above scenario. The initial request to display the pages on the screen will be a request to the Spring-MVC process. Any subsequent requests that involve changing the data will be via the path shown above.
The problem that i have now is that all responses from Spring-WS (the webservice) are in XML format. This means that when the request comes from the browser, the data will have to come from the web service but the pages will need to be refreshed from the Spring-MVC application. This somehow feels a bit wrong as each request will involve to calls. One to get the data and one to get the presentation data.
To overcome this, i would like to implement the Spring-MVC layer using a technique where i only need to make one initial request. This means that the user interface will rendered on the screen. All subsequent requests to the Spring-WS service should not result in the browser to be rendered other than refreshing the data.
I am interested in knowing what kind of technologies i can use to achieve this. One way of doing this is by using Applets but this has been ruled out for security reasons. I have seen several websites which work exactly the way i have described above. i.e. the page never refreshes. A very good example is the Sonatype Nexus Maven repository manager user interface shown below:
It runs in a web browser and when the Nexus user interface loads on the browser, it is almost like a Swing type application. (Any one know what technology Nexus use for the user interface?)
I guess my question is which Web based user interface technologies (preferably open source) can i use which has a swing type look and feel but is not Swing and requires minimal requests to the server to refresh the screens?
Thanks in advance.
Nexus' interface is built using Sencha GXT3, which now also contains ExtJS.
Have a look at the GXT API: it contains a lot of web components which knows how to update their own state without doing the full request/response cycle (using Ajax), which is what you seem to be trying to get away from.

Client architecture for calling Spring based web service

I have written a SOAP based web service which runs fine on a Tomcat server. The Web Service service itself is a Spring MVC based web service that runs on the Tomcat application server.
Now i need to write a Thick client which will be a standalone Java app that will use the services of the web service. I think i am correct in that the client only needs to know about the service details (i.e. operations) and nothing else.
What i am not sure of is the architecture and environment i should use for the client. The client application will be based on Swing but is it possible to use Spring with Swing together?
On the web service i have the following setup
view --> Service --> Model
The client application is basically a configuration tool. It uses the web service to configure user accounts. This means that the client application does not actually write anything to any database. It just uses the services of the web service to make changes to 'user account' and probably view list of accounts.
My question really is
- Is an MVC design suitable for such a use case
- Usually Spring is used for web based applications. Is there any benefit in using Spring with the Swing based client?
- Are there any alternative or better solutions/design/architecture that would achieve the same?
An example showing Spring used in conjunction with a Swing application would be very usefull.
Thanks in advance.
Spring MVC is not appropriate for a Swing-based client. Use the core Spring framework and a JAX-RS implementation like Jersey to provide simple REST web services in tomcat. Jersey also provides a corresponding client API that you can use within your Swing application to invoke the REST services.
If you have decided upon Swing as your platform, there are two options you can look at:
(1) Net Beans Rich Client Platform
http://netbeans.org/kb/trails/platform.html
(2) You can roll up your sleeves and write your own app using a low level yet extremely flexible framework called Swixml
http://www.swixml.org/
Give Swixml a good try before you try others, it may surprise you.
You can implement Swing-based thin client application with Spring Integration backend serving as a integration tier. It can expose gateways accepting simple Java types or DTOs. Your Swing presenters / controllers interacts with these components in order to call remote webservices.

What is a good technology stack for a java web based application with REST support?

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.

Categories

Resources