I am designing a Java web application that will be deployed to the Wildfly or Tomcat (not decided yet).
Basically it's about the offline java application that needs a REST interface for communication (to accept JSON data). The idea is that application runs all the time and processes the requests stored in redis cache (where the received JSON data is stored).
I used the Spring MVC framework for a web site in the past but I don't need the MVC pattern for the REST interface.
Is there a way you can point me to for using a Spring framework (or some other Java framework) to add the capability to recieve a POST requests to the existing offline application? Or it will be better to just write the REST service that will use the same cache as the existing application?
To illustrate my question I am attaching the simplified diagram of the architecture I am looking for:
As soon as you speak of receiving a POST request, you are automatically referring to a HTTP server.
The question is just whether that server is running as a service, or on-demand.
Is there a way you can point me to for using a Spring framework (or some other Java framework) to add the capability to recieve a POST requests to the existing offline application?
Not without introducing some sort of HTTP container, no.
Or it will be better to just write the REST service that will use the same cache as the existing application?
Yes, exactly. And as another mentioned, personally I'd stick with Spring MVC. But Jersey should also work very well for you for your usecase.
I think you should use any light ESB, like Camel, Mule or Spring Integration. If you have already worked with Spring probably the latter will be the easiest for you.
The purpose of this kind of apps is to facilitate the task of communicating anything with anything (on this case, an HTTP endpoint with your offline app).
Take a look at this:
https://www.google.ie/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=spring%20integration%20http-inbound
You can use Spring MVC for that.
The Model are your domain classes, the View is JSON in this case, and the Controllers handle the requests to perform logic operations, business as usual.
You can also take advantage of Spring's #RestController annotation to quickly create your endpoints, like in this java example:
#RestController
public class MovieController {
#Autowired
private MovieRepository movieRepository;
#RequestMapping(value = "/movies/{search}", method = RequestMethod.GET)
public List<Movie> findMovies(#PathVariable String search) {
return movieRepository.findByName(search);
}
#RequestMapping(value = "/movies", method = RequestMethod.POST)
public void postMovie(#RequestBody Movie movie) {
movieRepository.save(movie);
}
}
Answers from Andres, ESala and kervin pointed me to the right direction but the solution I found most suitable was:
implementation of REST services using Spring MVC and
transformation of the main process from the existing application to the Spring scheduled task.
Related
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.
I am working on the server side of a mobile application. One of the requirements I have is to implement the server using Java EE, JPA, EJB, and JSON.
I am new to Java EE but I did some reading and so far managed to build the business logic by implementing the Pojos and the EJBs of the project.
Where I am encountering difficulties is in figuring out how to expose my EJBs as a RESTful web service that can be consumed by the client side of the application. I could find some documentation and tutorials but all of it was related to building a backend for a website as opposed to mobile.
What are the best practices in exposing the business logic as a RESTful web service to a mobile application in a Java EE environment?
I'm not sure what your app server is but with JavaEE 6 and 7, you can do this fairly easily. Take a look at this tutorial for a good starting point. In general, a simple service would look something like:
#Path("/login")
public class LoginService {
#Consumes({MediaType.APPLICATION_JSON})
#Produces({MediaType.APPLICATION_JSON})
#POST
public Response login(LoginRequest loginRequest) throws Exception {
// your code
}
where LoginRequest in this case is simple Java POJO that has an equivalent in the JavaScript world.
Note that it doesn't matter what the client side is - the server just wants the JSON encoded object to come in and it can be a browser or a native mobile app.
A bit of an issue is how to initialize your app servers underlying Rest framework. Some use web.xml, some use a specially annotated class. That is a bit of a different question - let us know which app server you're using.
You can take a look at this sample https://github.com/AdamBien/airhacks/tree/master/javaee-sample .
This class is all that you need to bootstrap your rest application: https://github.com/AdamBien/airhacks/blob/master/javaee-sample/src/main/java/com/airhacks/JAXRSConfiguration.java
and this one expose a rest resource using a stateless EJB:
https://github.com/AdamBien/airhacks/blob/master/javaee-sample/src/main/java/com/airhacks/messaging/boundary/MessagesResource.java
I have a general question on a topic I am starting to learn, but having difficulty imagining the specific implementations for.
I want to implement a service broker for Cloud Foundry. The service broker API is as follows:
http://docs.cloudfoundry.org/services/api.html
I'm new to web programming. I have worked with web applications where I publish html files which reference servlets. But I'm not sure how one goes about implementing, for example:
Route
GET /v2/catalog
I was wondering if someone could give a high level rundown of what is involved in doing this. How do I implement a "path" like this? Let's say I wrote a servlet which hangs around at site.com/Servlet. The service broker will call site.com/Servlet/v2/catalog. How would my Servlet understand this? Would this URI even direct to my Servlet as written? I'm using Liberty (Websphere), but any answer would be useful.
I suggest using the Spring framework - website at https://spring.io/. It might take a bit of learning to understand what Spring is (it has many components that do different things), but Spring provides tools to make writing REST APIs very easy. Spring is well documented, has tons of users, and is quite modern.
For a REST API in Spring, you'd define a "Controller" class that controls incoming HTTP calls to the port you program is listening on.
For your concerns about how your programs understands a GET call to a particular endpoint - Spring provides the #RequestMapping annotation to do exactly this task. Within a class you've annotated with #Controller, you will have the #RequestMapping annotation over a method like this:
#Controller
public class CloudFoundryController {
...
#RequestMapping(value = {"/servlet/v2/catalog"}, method = RequestMethod.GET
public HttpResponse getV2Catalogue() {
...
}
}
When this application detects an HTTP GET request with "/servlet/v2/catalog" as the URL endpoint, then Spring will ensure that the getV2Catalogue() method is called. When the method returns, Spring sends back, over the network, an object of whatever type you've defined in the method header as an http response.
Building a REST service with Spring: https://spring.io/guides/gs/rest-service/
I am new to spring world. I am trying to add to new functionality to an existing spring MVC based reporting project. The new functionality would give the user an ability to schedule a particular report to run every so often.
I see that in current design, model and Controller sections of MVC is tightly coupled. Due to time constraints, I am not trying to separate Model and Controller instead want to use the existing infrastructure as is. My plan is to mimic the browsers request in a pojo and somehow call spring's MVC to handle my request. Am i going in right direction, if so how to I invoke the spring MVC from a POJO, if not, what would be the right approach. Please help........
If i understand you correctly, basically you would like to :
create a non-web application that makes requests to existing Spring MVC controllers
and that you have to reuse existing controllers
and NOT reusing the business model, because you dont have the time to separate the business model from the controller
Basically you are doing web services with this approach, where you make requests to the controllers via http using a http client library. And perhaps, get the response as json, which you can later map into a java bean, and continue your work from there.
You can have the controller to return the view's model as xml, or json, etc, or even directly return a bean as json or xml using spring's message converter.
If you are already using Spring 3, there is a RestTemplate that you can make use of to simplify this.
You could also make use of Apache's HttpClient, whose interesting example that you can see in this REST template code.
This related Q&A on java http clients could also be helpful.
I want to set up an application using RESTful principles and Spring. I found out the RESTemplate for the client-side but I don't know how to configure the server. First I want to create a simple application in which the server simply respond with an Hello {name} string after a myserverapp:8080/{name} request. Can someone help me, maybe with Java code? Thanks.
It really does sound like you need to read the Spring docs and have a look around for examples on the internet (there are a lot)!
Here is one which I think it very clear and straightforward to get you started.
You need to start researching a little bit via google.
http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#rest-resttemplate
sample:
#RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String delete(#PathVariable("id") Integer id)
Might also want to look into using Jersey, allows you to decouple your web layer from your REST layer. Using Spring's method, you need to init a controller etc. can take away from what the purpose of a controller is serving.
I have used both technologies, and it really depends on what you want to do, currently I'm writing an app using backbone.js which uses Jersey to serve up the REST, and Spring MVC as a front end to render the jsp pages.