I have Java backend project with REST implementation which is a warfile,
I also have Grails as frontend, i can run theese two separeted projects on their own using tomcat,
but how do i configure the grails part so that i can use the java backend RESTfull webbservice within it,
do i have to make a war file from the java backend and import it to the grails frontend ? make a jarfile ?
if so, how do i do that ? or are there any other way to to this to make it work.
REST provides a way to access a service without having to link them explicitly.
RESTful applications use HTTP requests to post data (create = HTTP-POST, update = HTTP-PUT, read = HTTP-GET, delete = HTTP-DELETE
If you run the Java backend using Tomcat, can you access it from a browser?
i.e.
http://localhost:8080/backendservice/resource/1
(this will be a GET request.)
You need to make the http requests from your application.
A lot depends on how much control you have over the 2 apps and how they have been designed.
First choice is to create a service class in your grails app that communicates with the other app using REST. You could use something like Spring's RestTemplate. The data is then passed to the Controller layer and into your front end JSP/GSP.
If the RESTful application has been designed in such a way that the RESTful layer can be separated from the business layer, then you could add a jar of the business layer to the grails app as a dependency. In this case you would just change the Service class to talk to the jar instead of the RESTful service.
Another option is to have the browser talk to the REST layer directly using JavaScript. This should work as they come from the same server.
Related
What is the difference between writing Spring boot REST API backed SPA
inside one project, javascript served from Spring's app server
two projects, Spring backend server app and javascript frontend in separate node server
Are there any benefits of the first over the other other. For me separate project are more clean. I'm pretty new to javascript so I'm affraid to clutter the java project with all the folder, files and javascript build processes. Also there are more i.e. separate vuejs templates than vuejs-spring-boot templates.
Is there any way, to somehow connect both solutions, by generating one js file for the SPA and serv it from Spring.
Regarding authentication I can utilize Spring MVC in first option, the second one is asking for some token authentication.
Is there any way, to somehow connect both solutions, by generating one js file for the SPA and serv it from Spring.
You can copy the dist content into src/main/resources in SpringBoot:
npm run build
And that HTML/JS will be served from Spring.
However, this process is take time and not fits with daily development.
During development, you can let dev server proxy all API request to the actual backend:
`
// config/index.js
module.exports = {
// ...
dev: {
proxyTable: {
// proxy all requests starting with /api to jsonplaceholder
'/api': {
target: 'http://jsonplaceholder.typicode.com:8000', // <-- Spring app running here
changeOrigin: true,
pathRewrite: {
'^/api': ''
}
}
}
}
}
`
Refer: https://vuejs-templates.github.io/webpack/proxy.html
The above example will proxy the request /api/posts/1 to http://jsonplaceholder.typicode.com/posts/1.
So, in client side, you can develop normally, and run in dev mode
npm run dev
Hope this help!
You can use both ways,but separating the frontend from backend will make your application reusable.
I mean if you want your vue.js project to connect with another api for example with Laravel,then you have to make only the backend and let your frontend as it is.
In the other hand you can keep your api backend as it is and for frontend you can use angular or react.So then you have to make only the frontend and let your api as it is.
Also in my opinion it is great to use separate project when you working on group.Because it is more clear and gives you flexibility to manage the frontend and backend easily.
Actually i am working on project and i am making the frontend and someone else is making the backend.
I always prefer to separate backend from frontend so in my opinion
that is the best way.But ofcourse you can use both ways.
I am planning to start a project and I am looking for the best approach to make a RIA application using AngularJS.
Right know I am pretty sure of those technologies:
AngularJS (+ bootstrap CSS) for the client UI, logic and server
requests.
Spring for bootstrapping the server business logic.
Hibernate + MySQL for persistent data access
Jersey for the Restful web service API.
Spring Security for url and data protection over authentication.
The only piece I feel is not ok is that my application will not be the typical one page app, because it will be large and I want to break it into multiple one page apps, some protected and others public. To serve every index.html I want another technology like Spring MVC, making those small one page apps secure for this points, and also not allowing the access to some resources.
¿Do you think this is a good approach or you would change any of this technologies (like supressing jersey/Spring MVC redundant dependencies)?
i think that in general your aprroach is a good one, but maybe you could use the webapp generator yeoman with the JHispster, a java web app generator.
Or if you don't like the ideia you could add to your data access layer the Spring-Data-JPA, because you will avoid to write the boilerplate code.
I am working to create a web app. Simultaneously I am also developing a REST web service that will use the same codebase...What I want to know is, can I make the same codebase on a server function as base for both the web app and the web service. (In the web app a user can log in and perform operations while the web service is like an API).
Or do I need to keep 2 distinct code bases for the web app and the web service?
Also, I want the same web app to act as both producer and consumer of the web services...Is this possible?
Yes you can. Technical a REST API is the same thing as the server side of a web application.
But if your REST API is accessed by third parties you might want to have a different versioning strategy then for your web app, so for anything of serious size I would probably separate those.
Sorry can't speak about the producer/consumer part of the question. Don't understand that part of the question.
You can do it that way, but it means that you'll have to deploy the two together. That might not always be desirable.
The alternative is to refactor the common classes into a third project, removing them from the app and the service. Package those into a JAR that you add to both the REST service and the app, just as you would any other 3rd party JAR. If you do it that way, you can deploy the two independently. The cost is maintaining the common classes.
Yes you can do both things from one codebase.. app and web service also, the complete game will be around URLs i.e. app URL and Web service URL..
And Yes it's possible, same web app to act as both producer and consumer.
I am new to web services. I have a requirement in my project. I have to consume the web services of our vendor in my project. All he has shared with me is a WSDL file and a document about the description of the the different operations.
Question:-
1: What do I need to do consume these web services in my java project? I have been advised to use axis2, eclipse with tomcat6.
2: Do I need to ask for some other files/information from WS vendor OR wsdl file is enough to consume these web services?
3: Do I need to write a java WS client (using axis2 plugin) or another webservice which will talk to vendor web service?
Please suggest the best possible way.
I am sorry if the question sounds like a naive..
Axis is a solid choice for such application.
You need to generate an axis client based on the provided WSDL. Then you import the generated client and use it's methods. You can see the details of this process here (read whole page or starting from the linked section): http://ws.apache.org/axis2/1_0/userguide3.html#Writing_Web_Service_Clients_using_Code_Generation_with_Data_Binding_Support
You could also need some entry-point (WebService URL).
You need to generate a client, not a webservice. See point 1.
Don't use Axis if you need ambient authentication in a Windows environment. I went down that path and ended up going with Apache CXF - which seems better to me anyhow.
You can use SOAP UI to test the web service. It'll read the WSDL, let you create requests by filling in values, and display the response that you get back. It might help you get a better understanding of what the service does before you start writing your classes.
You don't need to create a new web service in order to consume a web service, you need to write a web service client.
Similar question to this one:
Steps in creating a web service using Axis2 - The client code
All the standard web frameworks have a command (normally called wsdl2java) that will read the WSDL and then generate a java based client object.
I can recommend Axis2, but another popular choice is CXF
I am building a REST based API (Read only) for accessing services of my application. I plan on writing a web application that will leverage these APIs to provide basic information on the status of the application. I plan to use AJAX (using jQuery) to show the information.
Originally I planned on using Grails, Spring MVC, RoR or one of the web frameworks to handle the back end of my application. The REST APIs I will be providing though are already built on a stanalone REST framework so I would only be leveraging the web framework for the core application and not the business logic. In the future, I might need the server side framework to handle other tasks but for now most of the work is done in the REST APIs.
My question is, should I bother with using a web application framework on the server side? I should be able to make all the API calls I need from AJAX directly from the browser. I cannot think of much I would need to do on the server side. Would it make sense to have the application be standard HTML + AJAX + REST?
Its hard to say without actually knowing more about your current setup. This is what your situation sounds like:
You already have an app ready to go with all of the business logic contained. Sounds like its written in Java?
You already have the services written and exposed through a REST api using some other standalone framework. Meaning that if you wanted to, you could access the data right now with the browser without any extra work.
You have not yet built the web application, but when you do, it will get all of its content from the REST api using XHR and jquery. I say that, because otherwise, I would think that you would already be using some kind of framework to generate the other content.
If I am correct in my assumptions, then I would say that you have no need for an additional framework layer. Grails, RoR, SpringMVC my use ajax, and aid in exposing REST services, but the bulk of what they provide is an easy way to make an application that must generate html on the server, deal with form submissions, and handle sessions in a request/response cycle. It doesn't really sound like you'll be doing any of that, and it will likely make your app more complicated.
If you did at some point need the things that rails etc. provides, I would say that you may not need to use rails to expose the rest apis you have now. You could use rails just for what you need, and continue to use what you have for the REST api.
Well, the AJAX calls need to pull data from a server somewhere. If the goal is to avoid a complicated setup on the server side, CherryPy can keep the server side code VERY small.
I've written a simple exapmle below. The first class would be where you put the logic for your ReST API. The code below the class is all you need to get the server up and running.
Install Python 2.6, save the code below to restExample.py. Then in your command line run the python file by doing "python restExample.py". Point your browser to http://localhost:8080/blog/999 and see some JSON come back.
import cherrypy
import json
# Create the controller
class Blog_Controller(object):
def get(self, entryID):
cherrypy.response.headers['Content-Type'] = 'application/json'
return json.dumps({'title':'Entry Title from DB', 'entry_text': 'Text From DB'})
def update(self, entryID, titleFromPOSTFormInput, textFromPOSTFormInput):
# Update DB with passed in arguments. entryID comes from URL,
# other two entries come from POST
cherrypy.response.headers['Content-Type'] = 'application/json'
return json.dumps({'success':True})
# Setup URL routes
d = cherrypy.dispatch.RoutesDispatcher()
d.connect(name='blog_entry', route='blog/:entryID', action='get',
controller=Blog_Controller(),
conditions={'method': ['GET']})
d.connect(name='blog_entry', route='blog/update/:entryID', action='update',
controller=Blog_Controller(),
conditions={'method': ['POST']})
config = { '/' : { 'request.dispatch': d } }
cherrypy.tree.mount(root=None, config=config)
# Start the webserver
engine = cherrypy.engine
try:
engine.start()
except:
sys.exit(1)
else:
engine.block()
It sounds like this might be a good use case for GWT with Restlet.
Forgive me for tooting my own horn, but if you are doing AJAX REST stuff with jQuery, you should probably check out my JSON-REST plugin:
http://plugins.jquery.com/project/rest
If you are getting XML back, then this won't be as useful, but you may still be able to adapt some of the code to your needs.