Vue SPA with Spring Boot, separate or not - java

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.

Related

Spring boot back end with Swing client

I have a design question: I use DB + Hibernate + SpringBoot as back end in a server and use Swing clients on desktops.
I am considering for a new project the following architecture:
Back end (maybe in separate servers):
SQL DB
Spring Boot + Hibernate Java App
Front end(s):
Swing Java App
later: mobile App; Browser
I have searched a bit, but still have some doubts and feel I am missing something. I initially considered using React for front end. But this is going to take too long for me and I have lots of Swing components that I already optimized (and this my applications tend to use lots of grids with edits "in place")
I searched for Spring + Swing, but what I found was about running everything on the same JVM. I would think that separating them would allow me to (later) build a Mobile or Web front end using the same back end.
To sum it up:
create back end services with spring boot + hibernate on top of SQL db
create Swing front end that consumes those services
(later stage) mobile app to consume these same services
Questions:
Is this a good approach ? Am I seeing this the wrong way
How to "share" the POJOs between the back end and the front end? (thinking about placing the models on a separate lib); I don't mean how to transport them, I mean how to reuse them on both projects.
This is possible.
I would create the backend application with Spring Boot and access a datastore with Spring DATA. I would make services available via REST.
To access REST from your Swing client you must use a REST client of some sort. I would recommend Spring RestTemplate but there are many alternatives.
Synchronous client to perform HTTP requests, exposing a simple,
template method API over underlying HTTP client libraries such as the
JDK HttpURLConnection, Apache HttpComponents, and others.
Since you want mobile support some time in the future you could consider creating a webclient instead and making it cross platform (browser only).
To answer your two questions:
Regarding backend you will be perfectly fine as Spring Boot is a good option. As for the client I personally would not choose Swing today since it is very old and rather go for JavaFX 2 or a web client if that is an option. If you are the only developer and since you are proficient it might be a good solution for you.
You can share the POJOs from you Server for your Client as an api that you include as dependency. However it is usually not recommended to share POJOs but rather generate it on the client side or just type them. In your case if it is a small project it probably is ok. Do what is most easy for you.
I would recommend start using Maven as a build tool if you do not already.
Good luck.
Your request is perfectly valid, as you are a Swing developer and your approach is absolutely fine.
So let's draw the general picture: you're building a backend/frontend product/software.
Backend
Your backend will be a Spring application that will run on a server, let's say http://localhost:8080, serving requests to clients.
You'd probably want to create a #RestController on http://localhost:8080/api/... to expose your functions to your Swing client.
Swing client
Your client will be a Swing application running on your desktop.
To connect your client to the Spring application, you now need to implement a service in your Swing application that will call your Spring web server and fetch the resources from there.
To achieve this goal, may options exist:
Why not plain Java ? Use this tutorial to call your Spring services.
Http components from Apache are quite common.
Spring your app ! Spring API includes RestTemplate that you can use in your Swing app to call Spring backend and fetch resources for your desktop app.
As you can see, this is EXACTLY the same thing as with a website in React:
your web page is the JFrame
your JSX components in React would be your JPanels and components
you'd connect action listeners on components (JComboBox, JButton) to a service calling your Spring backend.
you'd then use the resources fetched from your Spring server to update your application state.
rince and repeat
So, be confident, your approach is correct. You'll find people ranting at you having chosen an obsolete techno but really, who cares?
(May I add, totally a pun, you've been smarter than the JavaFX guys which are now facing the segregation of javafx libraries from the core Java ;-))
And as always... happy coding !

Separate frontend and backend in Java - Spring MVC Framework

I'm new at Spring MVC framework i want to learn how to fully separate frontend(html,js etc.) and backend(java).
I'm going to use RESTfull services, play with JSONs.
I think i'm going to build Single Page Application.
Most of tutorials shown in jsp pages which i dont like.
I saw my friends company project(Using Spring MVC) they used Embedded Jetty server and in server configuration they assigned two different paths for frontend and backend paths.
I saw frontend codes there was only html javascripts etc. on the backend side the approach was the same.(Fully Separated !!!)
My question is:'How they pass requests from frontend to backend and get results from backend and update frontend'.
Also they were using Maven backend and frontend defined as modules in the root.
Could you share tutorials so i can understand playing with codes ?
'How they pass requests from frontend to backend and get results from
backend and update frontend'
They probably use HTTP[S] as the transport and JSON as the data representation format. Browsers support AJAX which allows you to make HTTP connections without reloading the page.
Could you share tutorials so i can understand playing with codes ?
No, that's not what this site is for.
Comments:
JSP is still very useful for generating HTML on the server. This is pretty close to necessary if you want Google to crawl your site.
Check out Spring Data REST for a framework for quick REST APIs.
Also check out ExtJS or Dojo for good Single Page App frameworks.

App Engine: Having a web module and a endpoint module

I would like to have a web and an endpoint module.
I have this working in my dev environment largely following https://github.com/GoogleCloudPlatform/appengine-modules-sample-java .
However I can't get this working when hosted inside app engine. If I make the web module the default, I can't route to the endpoint module via dispatch.xml. This is because endpoints (apparently) need to live at /_ah/api and its not possible to route this away from the default module.
The other alternative is putting the endpoint module as the default module, however I don't then know how to route everything but /_ah/api/ to the web module. It seems that you can't route /* away from the default module.
EDIT: Note I want to have both modules running off of the same custom domain.
EDIT2: Note this is single page app. The front end module is purely html, css and js, which I want to talk to the endpoint module on the same domain.
Any way to solve this?
If the problem only resides in the routing, you can definitely have your default module with endpoints and a module 'website' for your frontend.
Then your dispatch file should look like that (python version, the Java one should be quite similar):
dispatch:
- url: "*/_ah/*"
module: default
- url: "*/*"
module: website
The dispatch file apparently sets the routing with a top to bottom priority so each request targeting the endpoints */_ah/* will be routed to the default module and the rest will go on the website module.
However if you have CORS issues, you can check in the handler configuration for static files or had the proper headers directly in your page handler code.
If the issue you have is splitting the traffic you should probably intercept all requests and look for the destination URL and divert accordingly. But i think your issue is actually accesing the endpoint deployed on a non default version.
You can deploy specific endpoints to specific (non default) versions of your app.
As stated in the official docs:
To access backend API versions that are deployed to non-default App Engine versions of your app, you must include the version specifier in the URL, like this: https://version-dot-your_app_id.appspot.com. For example, suppose your default app version is 1, but you want to access a backend API version deployed to App Engine app version 2; you would use this URL: https://2-dot-your_app_id.appspot.com.
The answer suggested by #Anhuin is good for your simpler purposes. But if you want to have totally independent modules, i suggest you to use subdomain mapping
Create a sub domain like api.domain.com for endpoints and web.domain.com for your regular frontend modules.
Map these in the dipatch.xml
<url>api.domain.com/*</url>
<module>endpointmod</module>
<url>web.domain.com/*</url>
<module>webmod</module>
I prefer this because '_ah' is used by appengine in multiple places like appstats, remote api and so on. This will be clearer to the people working in frontend also by calling the sub domains.

Build an Web UI with AngularJS for a java project?

I would like to add a Web User Interface for an existing Java project I did time ago. I learned basics of AngularJS on codeschool.com courses and now I know how to send an http or REST request to get data for my web UI.
My Java project has a set of methods that elaborate some data from a local database and return integers or integers arrays.
The goal of the interface would be to show a bunch of charts and data directly from that Java project.
What would be the most appropriate way to do this? I heard of implementing REST services on my Java project but it seems overkill for the purpose and i'm totally confused by all the frameworks for this. What would you use?
Thanks everyone for your answers!
I would use SprinvMVC to provide data from server to client side.
Here is my project from which you can start and learn basics :
https://github.com/xwid/thaimaster
Basicly you should create spring controllers mapped to urls, by doing it this way, you will be able to retrive server data using angular js.
http://www.javabeat.net/spring-mvc-angularjs-integration/
If you don't want to use a full REST framework such as Jersey, another possibility would be to use an embeddable HTTP server (there are several) and handle the requests yourself. This would mean that requests to something like /myapp/ would return your AngularJS filled HTML page, and requests to /resources/* would provide with REST functionality.
This would give you a standalone Java program that doesn't need a servlet container, but it would be a somewhat hacky solution. Not production grade, but you'd probably learn something from having to handle the HTTP traffic yourself.
With the Spring Framework's SprigBoot, it's quite easy to implement a REST service and have a runnable java application. You can follow this Building a RESTful Web Service guide from spring.io. They are clear and quickly understandable.
Also if you are not already familiar, through these guides you can have a glimpse of gradle and maven as well.

Connect grails front with java REST backend

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.

Categories