Spring Boot Vue | Vue Js + Spring Boot Rest Api Tutorial | Full Stack Development 최근 답변 270개

당신은 주제를 찾고 있습니까 “spring boot vue – Vue JS + Spring Boot REST API Tutorial | Full Stack Development“? 다음 카테고리의 웹사이트 you.tfvp.org 에서 귀하의 모든 질문에 답변해 드립니다: you.tfvp.org/blog. 바로 아래에서 답을 찾을 수 있습니다. 작성자 Java Guides 이(가) 작성한 기사에는 조회수 25,350회 및 좋아요 441개 개의 좋아요가 있습니다.

spring boot vue 주제에 대한 동영상 보기

여기에서 이 주제에 대한 비디오를 시청하십시오. 주의 깊게 살펴보고 읽고 있는 내용에 대한 피드백을 제공하세요!

d여기에서 Vue JS + Spring Boot REST API Tutorial | Full Stack Development – spring boot vue 주제에 대한 세부정보를 참조하세요

In this video tutorial, we will learn how to build a simple Full Stack application using #SpringBoot as backend and #Vue JS as frontend.
Vue JS is a progressive framework for building user interfaces (UI) on the front end.
Spring Boot is a very popular Java framework for building RESTful web services and microservices.
Blog post at https://www.javaguides.net/2021/07/vue-js-spring-boot-rest-api-tutorial.html
Course Content:
[00:00] – Overview
[04:01] – Creating Spring Boot Project and Expose REST API
[20:52] – Creating Vue Project and Consume REST API

spring boot vue 주제에 대한 자세한 내용은 여기를 참조하세요.

Tích hợp build VueJS trong Spring Boot – Viblo

Dạo gần đây, do có chút thời gian rảnh mình có vọc vạch về Spring Boot + … nên mình chỉ chọn các dependency đơn giản Giờ mình sẽ tạo thêm project vue.

+ 더 읽기

Source: viblo.asia

Date Published: 7/5/2021

View: 5771

[Full-stack] Spring Boot + Vue.js: CRUD example – BezKoder

– Spring Boot exports REST Apis using Spring Web MVC & interacts with embedded H2 Database using Spring JPA. – Vue Client sends HTTP Requests …

+ 여기에 표시

Source: www.bezkoder.com

Date Published: 6/15/2022

View: 858

Vue.js Frontend with a Spring Boot Backend – Baeldung

Explore how to set up a web application using Spring Boot and Vue.js.

+ 여기에 더 보기

Source: www.baeldung.com

Date Published: 3/23/2021

View: 4839

GitHub – jonashackt/spring-boot-vuejs

Example project showing how to build a Spring Boot App proving a GUI with Vue.js – GitHub – jonashackt/spring-boot-vuejs: Example project showing how to …

+ 여기에 표시

Source: github.com

Date Published: 2/19/2022

View: 1024

Chạy vuejs trên spring boot với maven – Kipalog

Maven – mình đang dùng maven 3.6.3; Nodejs; Vue cli … Tất nhiền rồi mình làm spring boot mà 😀 nhớ tạo với maven nhé :stuck_out_tongue: …

+ 여기에 표시

Source: kipalog.com

Date Published: 3/30/2021

View: 2070

Spring Boot Restful API and Vue.js Frontend – Simple Solution

Spring Boot Restful API and Vue.js Frontend · Introduction · Create New Spring Boot Restful API Project · Create new MySQL database to manage customer information.

+ 여기에 보기

Source: simplesolution.dev

Date Published: 7/3/2022

View: 2746

Spring Boot Vue.js – ZetCode

Spring Boot Vue.js · It is marketed as an approachable, performant and versatile framework for building web user interfaces. Vue.js is an …

+ 여기에 보기

Source: zetcode.com

Date Published: 10/24/2021

View: 235

Full Stack Java development with Spring Boot and VueJS

In this tutorial, you will learn how to build a full-stack application that uses Vue for the frontend and Spring Boot for the backend.

+ 여기를 클릭

Source: www.danvega.dev

Date Published: 5/21/2021

View: 6653

Build a Simple CRUD App with Spring Boot and Vue.js

Add CORS Filter to Your Spring Boot App. Before you move on to the Vue client app, there’s one more thing to update. Currently, the server …

+ 자세한 내용은 여기를 클릭하십시오

Source: developer.okta.com

Date Published: 2/27/2022

View: 149

주제와 관련된 이미지 spring boot vue

주제와 관련된 더 많은 사진을 참조하십시오 Vue JS + Spring Boot REST API Tutorial | Full Stack Development. 댓글에서 더 많은 관련 이미지를 보거나 필요한 경우 더 많은 관련 기사를 볼 수 있습니다.

Vue JS + Spring Boot REST API Tutorial | Full Stack Development
Vue JS + Spring Boot REST API Tutorial | Full Stack Development

주제에 대한 기사 평가 spring boot vue

  • Author: Java Guides
  • Views: 조회수 25,350회
  • Likes: 좋아요 441개
  • Date Published: 2021. 8. 30.
  • Video Url link: https://www.youtube.com/watch?v=hDC_kNlzz6c

Tích hợp build VueJS trong Spring Boot

Dạo gần đây, do có chút thời gian rảnh mình có vọc vạch về Spring Boot + Gradle thay cho Spring Boot + Maven như bình thường vẫn hay sử dụng do được vài đại ca giang hồ bảo là Gradle nó xịn xò hơn Maven nhiều. Kết quả thì mình thấy có vẻ là đúng thật, về cả hiệu năng và cách viết build-script trong Gradle có vẻ gọn gàng hơn Maven. Để minh họa thử, mình sẽ tạo một project Spring kết hợp với FE (cụ thể là VueJS, các bạn có thể thay thế bằng ReactJS cũng được) và build chung trong một lần luôn (cũng khá giống với việc viết Jenkins Pipeline để build cả FE và BE lên server vậy). Dài dòng đủ rồi, giờ bắt đầu thôi.

1. Tạo Project

Các bạn có thể tạo project tại Spring Initializr hoặc tạo trực tiếp trong IDE (mình sử dụng Eclipse, các bạn có thể sử dụng IntelliJ hoặc bất cứ IDE nào khác). Do đây chỉ là demo về build nên mình chỉ chọn các dependency đơn giản Giờ mình sẽ tạo thêm project vue. Cấu trúc của project như sau Ok vậy là xong. Các bạn có thể chạy thử 2 project vừa tạo để kiểm tra

2. Config build.gradle

Giờ chúng ta sẽ đi vào mục tiêu chính của bài viết. Về cơ bạn các task build của chúng ta sẽ như sau

yarn install để kéo các dependency về trong node_modules yarn build webpack sẽ build các code, component, css, ảnh thành các static resource duy nhất tại thư mục dist. Copy tất cả các static resource vào thư mục static resource của spring project Biên dịch các resource và java code thành một gói jar duy nhất. Đây là các step chúng ta sẽ làm khi build thông thường, tuy vậy với việc config trong build.gradle, Gradle sẽ tự chạy các task tương ứng và làm điều này dùm chúng ta (cũng từa tựa cấu hình các step trong CI/CD vậy). ! Cài đặt thêm plugin để hỗ trợ node và các script của node

plugins { id “com.moowork.node” version “1.3.1” }

Config cấu hình của node

node { version = ‘14.15.4’ workDir = file(“${project.projectDir}/src/main/fe/node”) }

Các task build

processResources{ dependsOn ‘vueBuild’ dependsOn ‘copyFrontendToBuild’ } task copyFrontendToBuild(type: Copy) { from “$projectDir/src/main/fe/dist/” into “$buildDir/resources/main/static” } task vueBuild(type: YarnTask) { dependsOn ‘vueInstall’ execOverrides { it.workingDir = ‘src/main/fe’ } args = [‘run’, ‘build’] } task vueInstall (type: YarnTask) { execOverrides { it.ignoreExitValue = true it.workingDir = ‘src/main/fe’ } args = [‘install’] }

Các task build sẽ bắt đầu chạy trong processResources đầu tiên là sẽ chạy vueBuild, mà cụ thể sẽ đợi vueInstall hoàn thành trước. Thứ tự sẽ giống với các step chúng ta khai báo ở trên. Sau đó, Gradle sẽ copy các file vừa build tới folder resource.

3. Build project

jonashackt/spring-boot-vuejs: Example project showing how to build a Spring Boot App providing a GUI with Vue.js

If you´re a JavaMagazin / blog.codecentric.de / Softwerker reader, consider switching to vue-cli-v2-webpack-v3

A live deployment is available on Heroku: https://spring-boot-vuejs.herokuapp.com

This project is used as example in a variety of articles & as eBook:

blog.codecentric.de/en/2018/04/spring-boot-vuejs | JavaMagazin 8.2018 | entwickler.press shortcuts 229 | softwerker Vol.12

Upgrade procedure

Get newest node & npm:

brew upgrade node npm install -g npm@latest

Update vue-cli

npm install -g @vue/cli

Update Vue components/plugins (see https://cli.vuejs.org/migrating-from-v3/#upgrade-all-plugins-at-once)

vue upgrade

In Search of a new Web Frontend-Framework after 2 Years of absence…

Well, I’m not a Frontend developer. I’m more like playing around with Spring Boot, Web- & Microservices & Docker, automating things with Ansible and Docker, Scaling things with Spring Cloud, Docker Compose, and Traefik… And the only GUIs I’m building are the “new JS framework in town”-app every two years… 🙂 So the last one was Angular 1 – and it felt, as it was a good choice! I loved the coding experience and after a day of training, I felt able to write awesome Frontends…

But now we’re 2 years later and I heard from afar, that there was a complete rewrite of Angular (2), a new kid in town from Facebook (React) and lots of ES201x stuff and dependency managers like bower and Co. So I’m now in the new 2-year-cycle of trying to cope up again – and so glad I found this article: https://medium.com/reverdev/why-we-moved-from-angular-2-to-vue-js-and-why-we-didnt-choose-react-ef807d9f4163

Key points are:

Angular 2 isn’t the way to go if you know version 1 (complete re-write, only with Typescript, loss of many of 1’s advantages, Angular 4 is coming)

React (facebookish problems (licence), need to choose btw. Redux & MObX, harder learning curve, slower coding speed)

And the introduction phrase sounds really great:

Vue (pronounced /vjuː/, like view) is a progressive framework for building user interfaces. Unlike other monolithic frameworks, Vue is designed from the ground up to be incrementally adoptable. The core library is focused on the view layer only and is very easy to pick up and integrate with other libraries or existing projects. On the other hand, Vue is also perfectly capable of powering sophisticated Single-Page Applications when used in combination with modern tooling and supporting libraries.

So I think, it could be a good idea to invest a day or so into Vue.js. Let’s have a look here!

Setup Vue.js & Spring Boot

Prerequisites

MacOSX

brew install node npm install -g @vue/cli

Linux

sudo apt update sudo apt install node npm install -g @vue/cli

Windows

choco install npm npm install -g @vue/cli

Project setup

spring-boot-vuejs ├─┬ backend → backend module with Spring Boot code │ ├── src │ └── pom.xml ├─┬ frontend → frontend module with Vue.js code │ ├── src │ └── pom.xml └── pom.xml → Maven parent pom managing both modules

Backend

Go to https://start.spring.io/ and initialize a Spring Boot app with Web and Actuator . Place the zip’s contents in the backend folder.

Customize pom to copy content from Frontend for serving it later with the embedded Tomcat:

< build > < plugins > < plugin > < groupId >org.springframework.boot < artifactId >spring-boot-maven-plugin < plugin > < artifactId >maven-resources-plugin < executions > < execution > < id >copy Vue.js frontend content < phase >generate-resources < goals > < goal >copy-resources < configuration > < outputDirectory >src/main/resources/public < overwrite >true < resources > < resource > < directory >${project.parent.basedir}/frontend/target/dist < includes > < include >static/ < include >index.html < include >favicon.ico

Frontend

Creating our frontend project is done by the slightly changed (we use –no-git here, because our parent project is already a git repository and otherwise vue CLI 3 would initialize an new one):

vue create frontend –no-git

see https://cli.vuejs.org/guide/

This will initialize a project skeleton for Vue.js in /frontend directory – it, therefore, asks some questions in the cli:

Do not choose the default preset with default (babel, eslint) , because we need some more plugins for our project here (choose the Plugins with the space bar):

You can now also use the new vue ui command/feature to configure your project:

If you want to learn more about installing Vue.js, head over to the docs: https://vuejs.org/v2/guide/installation.html

Use frontend-maven-plugin to handle NPM, Node, Bower, Grunt, Gulp, Webpack and so on 🙂

If you’re a backend dev like me, this Maven plugin here https://github.com/eirslett/frontend-maven-plugin is a great help for you – because, if you know Maven, that’s everything you need! Just add this plugin to the frontend’s pom.xml :

< build > < plugins > < plugin > < groupId >com.github.eirslett < artifactId >frontend-maven-plugin < version >${frontend-maven-plugin.version} < executions > < execution > < id >install node and npm < goals > < goal >install-node-and-npm < configuration > < nodeVersion >v10.10.0 < execution > < id >npm install < goals > < goal >npm < phase >generate-resources < configuration > < arguments >install < execution > < id >npm run build < goals > < goal >npm < configuration > < arguments >run build

Tell Webpack to output the dist/ contents to target/

Commonly, node projects will create a dist/ directory for builds which contains the minified source code of the web app – but we want it all in /target . Therefore we need to create the optional vue.config.js and configure the outputDir and assetsDir correctly:

module . exports = { … // Change build paths to make them Maven compatible // see https://cli.vuejs.org/config/ outputDir ; : ‘target/dist’ , assetsDir ; : ‘static’ ; }

First App run

Inside the root directory, do a:

mvn clean install

Run our complete Spring Boot App:

mvn –projects backend spring-boot:run

Now go to http://localhost:8098/ and have a look at your first Vue.js Spring Boot App.

Faster feedback with webpack-dev-server

The webpack-dev-server, which will update and build every change through all the parts of the JavaScript build-chain, is pre-configured in Vue.js out-of-the-box! So the only thing needed to get fast feedback development-cycle is to cd into frontend and run:

npm run serve

That’s it!

Browser developer tools extension

Install vue-devtools Browser extension https://github.com/vuejs/vue-devtools and get better feedback, e.g. in Chrome:

IntelliJ integration

There’s a blog post: https://blog.jetbrains.com/webstorm/2018/01/working-with-vue-js-in-webstorm/

Especially the New… Vue Component looks quite cool 🙂

HTTP calls from Vue.js to (Spring Boot) REST backend

Prior to Vue 2.0, there was a build in solution (vue-resource). But from 2.0 on, 3rd party libraries are necessary. One of them is Axios – also see blog post https://alligator.io/vuejs/rest-api-axios/

npm install axios –save

Calling a REST service with Axios is simple. Go into the script area of your component, e.g. Hello.vue and add:

import axios from ‘axios’ data ( ) ; { return { response : [ ] , errors : [ ] } } , callRestService ( ) ; { axios . get ( `api/hello` ) . then ( response => { // JSON responses are automatically parsed. this . response = response . data } ) . catch ( e => { this . errors . push ( e ) } ) } }

In your template area you can now request a service call via calling callRestService() method and access response data:

< button class = ”Search__button” @click =" callRestService() " > CALL Spring Boot REST backend service < h3 > {{ response }}

The problem with SOP

Single-Origin Policy (SOP) could be a problem if we want to develop our app. Because the webpack-dev-server runs on http://localhost:8080 and our Spring Boot REST backend on http://localhost:8098.

We need to use Cross-Origin Resource Sharing Protocol (CORS) to handle that (read more background info about CORS here https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS)

Enabling Axios CORS support

Create a central Axios configuration file called http-commons.js :

import axios from ‘axios’ export const AXIOS = axios . create ( { baseURL : `http://localhost:8098` , headers : { ‘Access-Control-Allow-Origin’ : ‘http://localhost:8080’ } } )

Here we allow requests to the base URL of our Spring Boot App on port 8098 to be accessible from 8080.

Now we could use this configuration inside our Components, e.g. in Hello.vue :

import { AXIOS } from ‘./http-common’ export default { name : ‘hello’ , data ( ) { return { posts : [ ] , errors : [ ] } } , methods : { // Fetches posts when the component is created. callRestService ( ) { AXIOS . get ( `hello` ) . then ( response => { // JSON responses are automatically parsed. this . posts = response . data } ) . catch ( e => { this . errors . push ( e ) } ) } }

Enabling Spring Boot CORS support

Additionally, we need to configure our Spring Boot backend to answer with the appropriate CORS HTTP Headers in its responses (there’s a good tutorial here: https://spring.io/guides/gs/rest-service-cors/). Therefore we add the annotation @CrossOrigin to our BackendController:

@ CrossOrigin ( origins = “http://localhost:8080” ) @ RequestMapping ( path = “/hello” ) public @ ResponseBody String sayHello () { LOG . info ( “GET called on /hello resource” ); return HELLO_TEXT ; }

Now our Backend will respond CORS-enabled and will accept requests from 8080. But as this only enables CORS on one method, we have to repeatedly add this annotation to all of our REST endpoints, which isn’t a nice style. We should use a global solution to allow access with CORS enabled to all of our REST resources. This could be done in the SpringBootVuejsApplication.class :

// Enable CORS globally @ Bean public WebMvcConfigurer corsConfigurer () { return new WebMvcConfigurerAdapter () { @ Override public void addCorsMappings ( CorsRegistry registry ) { registry . addMapping ( “/api/*” ). allowedOrigins ( “http://localhost:8080” ); } }; }

Now all calls to resources behind api/ will return the correct CORS headers.

But STOP! Webpack & Vue have something much smarter for us to help us with SOP!

Thanks to my colleague Daniel who pointed me to the nice proxying feature of Webpack dev-server, we don’t need to configure all the complex CORS stuff anymore!

According to the Vue CLI 3 docs the only thing we need to configure is a devserver-proxy for our webpack devserver requests. This could be done easily in the optional vue.config.js inside devServer.proxy :

module . exports = { // proxy all webpack dev-server requests starting with /api // to our Spring Boot backend (localhost:8098) using http-proxy-middleware // see https://cli.vuejs.org/config/#devserver-proxy devServer : { proxy : { ‘/api’ : { target : ‘http://localhost:8098’ , ws : true , changeOrigin : true } } } , … }

With this configuration in place, the webpack dev-server uses the http-proxy-middleware, which is a really handy component, to proxy all frontend-requests from http://localhost:8080 –> http://localhost:8098 – incl. Changing the Origin accordingly.

This is used in the webpack build process to configure the proxyMiddleware (you don’t need to change something here!):

// proxy api requests Object . keys ( proxyTable ) . forEach ( function ( context ) { var options = proxyTable [ context ] ; if ( typeof options === ‘string’ ) { options = { target : options } } app . use ( proxyMiddleware ( options . filter || context , options ) ) } )

Using history mode for nicer URLs

If we use the default configuration of the generated Vue.js template, we see URLs with a # inside them – like this:

http://localhost:8098/#/bootstrap or http://localhost:8098/#/user

With the usage of HTML5 history mode, we can achieve much nicer URLs without the # in them. Only thing to do in the Vue.js frontend is to configure our router accordingly inside the router.js:

… Vue.use(Router); const router = new Router({ mode: ‘history’, // uris without hashes #, see https://router.vuejs.org/guide/essentials/history-mode.html#html5-history-mode routes: [ { path: ‘/’, component: Hello }, { path: ‘/callservice’, component: Service }, …

That’s nearly everything. BUT only nearly! If one clicks on a link inside our frontend, the user is correctly send to the wished component.

But if the user enters the URL directly into the Browser, we get a Whitelabel Error Page because our Spring Boot backend gives us a HTTP 404 – since this URL isn’t present in the backend:

The solution is to redirect or better forward the user to the frontend (router) again. The Vue.js docs don’t provide an example configuration for Spring Boot, but luckily there are other resources. In essence we have to implement a forwarding controller in our BackendController:

// Forwards all routes to FrontEnd except: ‘/’, ‘/index.html’, ‘/api’, ‘/api/**’ // Required because of ‘mode: history’ usage in frontend routing, see README for further details @RequestMapping(value = “{_:^(?!index\\.html|api).$}”) public String redirectApi() { LOG.info(“URL entered directly into the Browser, so we need to redirect…”); return “forward:/”; }

This controller will forward every request other then ‘/’, ‘/index.html’, ‘/api’, ‘/api/**’ to our Vue.js frontend.

Bootstrap & Vue.js

There’s a nice integration of Bootstrap in Vue.js: https://bootstrap-vue.js.org/

npm install bootstrap-vue

Now you can use all the pretty Bootstrap stuff with ease like:

CALL Spring Boot REST backend service

instead of

The docs contain all the possible components: https://bootstrap-vue.js.org/docs/components/alert/

See some elements, when you go to http://localhost:8080/#/bootstrap/ – this should look like this:

A good discussion about various UI component frameworks: http://vuetips.com/bootstrap

Heroku Deployment

As you may already read, the app is automatically deployed to Heroku on https://spring-boot-vuejs.herokuapp.com/.

The project makes use of the nice Heroku Pipelines feature, where we do get a full Continuous Delivery pipeline with nearly no effort:

And with the help of super cool Automatic deploys , we have our GitHub Actions build our app after every push to master – and with the checkbox set to Wait for CI to pass before deploy – the app gets also automatically deployed to Heroku – but only, if the GitHub Actions (and Codegov…) build succeeded:

You only have to connect your Heroku app to GitHub, activate Automatic deploys and set the named checkbox. That’s everything!

Accessing Spring Boot REST backend on Heroku from Vue.js frontend

Frontend needs to know the Port of our Spring Boot backend API, which is automatically set by Heroku every time, we (re-)start our App.

You can try out your Heroku app locally! Just create a .env-File with all your Environment variables and run heroku local !

To access the Heroku set port, we need to use relative paths inside our Vue.js application instead of hard-coded hosts and ports!

All we need to do is to configure Axios in such a way inside our frontend/src/components/http-common.js:

export const AXIOS = axios.create({ baseURL: `/api` })

Using Heroku’s Postgres as Database for Spring Boot backend and Vue.js frontend

First, add Heroku Postgres database for your Heroku app.

Then follow these instructions on Stackoverflow to configure all needed Environment variables in Heroku: https://stackoverflow.com/a/49978310/4964553

Mind the addition to the backend’s pom.xml described here: https://stackoverflow.com/a/49970142/4964553

Now you’re able to use Spring Data’s magic – all you need is an Interface like UserRepository.java:

package de . jonashackt . springbootvuejs . repository ; import de . jonashackt . springbootvuejs . domain . User ; import org . springframework . data . repository . CrudRepository ; import org . springframework . data . repository . query . Param ; import java . util . List ; public interface UserRepository extends CrudRepository < User , Long > { List < User > findByLastName ( @ Param ( “lastname” ) String lastname ); List < User > findByFirstName ( @ Param ( “firstname” ) String firstname ); }

Now write your Testcases accordingly like UserRepositoryTest.java:

package de . jonashackt . springbootvuejs . repository ; import de . jonashackt . springbootvuejs . domain . User ; import org . junit . Before ; import org . junit . Test ; import org . junit . runner . RunWith ; import org . springframework . beans . factory . annotation . Autowired ; import org . springframework . boot . test . autoconfigure . orm . jpa . DataJpaTest ; import org . springframework . boot . test . autoconfigure . orm . jpa . TestEntityManager ; import org . springframework . test . context . junit4 . SpringRunner ; import java . util . List ; import static org . hamcrest . Matchers . contains ; import static org . junit . Assert .*; @ RunWith ( SpringRunner . class ) @ DataJpaTest public class UserRepositoryTest { @ Autowired private TestEntityManager entityManager ; @ Autowired private UserRepository users ; private final User norbertSiegmund = new User ( “Norbert” , “Siegmund” ); private final User jonasHecht = new User ( “Jonas” , “Hecht” ); @ Before public void fillSomeDataIntoOurDb () { // Add new Users to Database entityManager . persist ( norbertSiegmund ); entityManager . persist ( jonasHecht ); } @ Test public void testFindByLastName () throws Exception { // Search for specific User in Database according to lastname List < User > usersWithLastNameSiegmund = users . findByLastName ( “Siegmund” ); assertThat ( usersWithLastNameSiegmund , contains ( norbertSiegmund )); } @ Test public void testFindByFirstName () throws Exception { // Search for specific User in Database according to firstname List < User > usersWithFirstNameJonas = users . findByFirstName ( “Jonas” ); assertThat ( usersWithFirstNameJonas , contains ( jonasHecht )); } }

Then include this functionality in your REST-API – see BackendController.java:

@ RequestMapping ( path = “/user” , method = RequestMethod . POST ) @ ResponseStatus ( HttpStatus . CREATED ) public @ ResponseBody long addNewUser ( @ RequestParam String firstName , @ RequestParam String lastName ) { User user = new User ( firstName , lastName ); userRepository . save ( user ); LOG . info ( user . toString () + ” successfully saved into DB” ); return user . getId (); }

and use it from the Vue.js frontend, see User.vue:

< template > < div class =" user " > < h1 > Create User < h3 > Just some database interaction… < input type =" text " v-model =" user.firstName " placeholder =" first name " > < input type =" text " v-model =" user.lastName " placeholder =" last name " > < button @click =" createUser() " > Create User < div v-if =" showResponse " > < h6 > User created with Id: {{ response }} < button v-if =" showResponse " @click =" retrieveUser() " > Retrieve user {{user.id}} data from database < h4 v-if =" showRetrievedUser " > Retrieved User {{retrievedUser.firstName}} {{retrievedUser.lastName}} < script > // import axios from ‘axios’ import { AXIOS } from ‘./http-common’ export default { name : ‘user’ , data ( ) { return { response : [ ] , errors : [ ] , user : { lastName : ” , firstName : ” , id : 0 } , showResponse : false , retrievedUser : { } , showRetrievedUser : false } } , methods : { // Fetches posts when the component is created. createUser ( ) { var params = new URLSearchParams ( ) ; params . append ( ‘firstName’ , this . user . firstName ) ; params . append ( ‘lastName’ , this . user . lastName ) ; AXIOS . post ( `/user` , params ) . then ( response => { // JSON responses are automatically parsed. this . response = response . data ; this . user . id = response . data ; console . log ( response . data ) ; this . showResponse = true } ) . catch ( e => { this . errors . push ( e ) } ) } , retrieveUser ( ) { AXIOS . get ( `/user/` + this . user . id ) . then ( response => { // JSON responses are automatically parsed. this . retrievedUser = response . data ; console . log ( response . data ) ; this . showRetrievedUser = true } ) . catch ( e => { this . errors . push ( e ) } ) } } }

Testing

Install vue-test-utils

https://github.com/vuejs/vue-test-utils

npm install –save-dev @vue/test-utils

Jest

Jest is a new shooting star in the sky of JavaScript testing frameworks: https://facebook.github.io/jest/

Intro-Blogpost: https://blog.codecentric.de/2017/06/javascript-unit-tests-sind-schwer-aufzusetzen-keep-calm-use-jest/

Examples: https://github.com/vuejs/vue-test-utils-jest-example

Vue.js Jest Docs: https://vue-test-utils.vuejs.org/guides/#testing-single-file-components-with-jest

A Jest Unittest looks like Hello.spec.js:

import { shallowMount } from ‘@vue/test-utils’ ; import Hello from ‘@/components/Hello’ describe ( ‘Hello.vue’ , ( ) => { it ( ‘should render correct hello message’ , ( ) => { // Given const hellowrapped = shallowMount ( Hello , { propsData : { hellomsg : ‘Welcome to your Jest powered Vue.js App’ } , stubs : [ ‘router-link’ , ‘router-view’ ] } ) ; // When const contentH1 = hellowrapped . find ( ‘h1’ ) ; // Then expect ( contentH1 . text ( ) ) . toEqual ( ‘Welcome to your Jest powered Vue.js App’ ) ; } ) } )

To pass Component props while using Vue.js Router, see https://stackoverflow.com/a/37940045/4964553.

How to test components with router-view or router-link https://vue-test-utils.vuejs.org/guides/using-with-vue-router.html#testing-components-that-use-router-link-or-router-view.

The test files itself could be named xyz.spec.js or xyz.test.js – and could reside nearly everywhere in the project.

Jest Configuration

The Jest run-configuration is done inside the package.json:

“scripts” ; : { … “test:unit” ; : “vue-cli-service test:unit –coverage” , ; … . } ,

Jest can be configured via jest.config.js in your project root, or the jest field in package.json. In our case we especially need to configure coverageDirectory :

], “jest” : { … “coverageDirectory” : ” /tests/unit/coverage ” , “collectCoverageFrom” : [ ” src/**/*.{js,vue} ” , ” !src/main.js ” , ” !src/router/index.js ” , ” !**/node_modules/** ” ] } }

Jest needs to know the right output directory /tests/unit/coverage to show a correct output when npm run test:unit is run (or the corresponding Maven build). If you run the Jest Unit tests now with:

npm run test:unit

you´ll recognize the table of test covered files:

Integration in Maven build (via frontend-maven-plugin)

Inside the pom.xml we always automatically run the Jest Unittests with the following configuration:

< execution > < id >npm run test:unit < goals > < goal >npm < phase >test < configuration > < arguments >run test:unit

This will integrate the Jest Unittests right after the npm run build command, just you are used to in Java-style projects:

And don’t mind the depiction with ERROR – this is just a known bug: eirslett/frontend-maven-plugin#584

Run Jest tests inside IntelliJ

First, we need to install the NodeJS IntelliJ plugin (https://www.jetbrains.com/help/idea/developing-node-js-applications.html), which isn’t bundled with IntelliJ by default:

IntelliJ Jest integration docs: https://www.jetbrains.com/help/idea/running-unit-tests-on-jest.html

The automatic search inside the package.json for the Jest configuration file jest.conf.js doesn’t seem to work right now, so we have to manually configure the scripts part of:

“unit”: “jest –config test/unit/jest.conf.js –coverage”,

inside the Run Configuration under Jest and All Tests :

Now, when running All Tests , this should look like you’re already used to Unittest IntelliJ-Integration:

End-2-End (E2E) tests with Nightwatch

Great tooling: http://nightwatchjs.org/ – Nightwatch controls WebDriver / Selenium standalone Server in own child process and abstracts from those, providing a handy DSL for Acceptance tests:

Docs: http://nightwatchjs.org/gettingstarted/#browser-drivers-setup

Nightwatch is configured through the nightwatch.conf.js. Watch out for breaking changes in 1.x: https://github.com/nightwatchjs/nightwatch/wiki/Migrating-to-Nightwatch-1.0

More options could be found in the docs: http://nightwatchjs.org/gettingstarted/#settings-file

Write Nightwatch tests

An example Nightwatch test is provided in HelloAcceptance.test.js:

module . exports = { ‘default e2e tests’ : browser => { browser . url ( process . env . VUE_DEV_SERVER_URL ) . waitForElementVisible ( ‘#app’ , 5000 ) . assert . elementPresent ( ‘.hello’ ) . assert . containsText ( ‘h1’ , ‘Welcome to your Vue.js powered Spring Boot App’ ) . assert . elementCount ( ‘img’ , 1 ) . end ( ) } }

Run E2E Tests

npm run test:e2e

Run all tests

npm test

NPM Security

npm Security – npm@6

https://medium.com/npm-inc/announcing-npm-6-5d0b1799a905

npm audit

https://blog.npmjs.org/post/173719309445/npm-audit-identify-and-fix-insecure

Run npm audit fix to update the vulnerable packages. Only in situations, where nothing else helps, try npm audit fix –force (this will also install braking changes)

https://nodejs.org/en/blog/vulnerability/june-2018-security-releases/

—> Update NPM regularly

https://docs.npmjs.com/troubleshooting/try-the-latest-stable-version-of-npm

npm install -g npm@latest

—> Update Packages regularly

https://docs.npmjs.com/getting-started/updating-local-packages

npm outdated

npm update

Shift from templates to plugin-based architecture in Vue Cli 3

In the long run, templates like the main webpack are deprecated in the Vue.js universe:

https://vuejsdevelopers.com/2018/03/26/vue-cli-3/

Plugins bring the following benefits compared to templates:

No lock in, as plugins can be added at any point in the development lifecycle

Zero config plugins allow you to spend time developing rather than configuring

Easy to upgrade, as configuration can be customized without “ejecting”

Allows developers to make their own plugins and presets

Starting point: https://cli.vuejs.org/

OMG! My package.json is so small – Vue CLI 3 Plugins

From https://cli.vuejs.org/guide/plugins-and-presets.html:

Vue CLI uses a plugin-based architecture. If you inspect a newly created project’s package.json, you will find dependencies that start with @vue/cli-plugin- . Plugins can modify the internal webpack configuration and inject commands to vue-cli-service . Most of the features listed during the project creation process are implemented as plugins.

With plugings, extensions to an existing project could also be made via: vue add pluginName . E.g. if you want to add Nightwatch E2E tests to your project, just run vue add @vue/e2e-nightwatch . All scoped packages are available here: https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue

These new Vue CLI 3 plugin architecture cleans our big package.json to a really neat compact thing. This was the old big dependency block:

“devDependencies” : { “@vue/test-utils” : ” ^1.0.0-beta.25 ” , “autoprefixer” : ” ^7.1.2 ” , “babel-core” : ” ^6.26.3 ” , “babel-helper-vue-jsx-merge-props” : ” ^2.0.3 ” , “babel-jest” : ” ^21.0.2 ” , “babel-loader” : ” ^7.1.5 ” , “babel-plugin-dynamic-import-node” : ” ^1.2.0 ” , “babel-plugin-syntax-jsx” : ” ^6.18.0 ” , “babel-plugin-transform-es2015-modules-commonjs” : ” ^6.26.0 ” , “babel-plugin-transform-runtime” : ” ^6.22.0 ” , “babel-plugin-transform-vue-jsx” : ” ^3.5.0 ” , “babel-preset-env” : ” ^1.7.0 ” , “babel-preset-stage-2″ : ” ^6.22.0 ” , “babel-register” : ” ^6.22.0 ” , “chalk” : ” ^2.4.1 ” , “chromedriver” : ” ^2.41.0 ” , “copy-webpack-plugin” : ” ^4.5.2 ” , “cross-spawn” : ” ^5.0.1 ” , “css-loader” : ” ^0.28.0 ” , “extract-text-webpack-plugin” : ” ^3.0.0 ” , “file-loader” : ” ^1.1.4 ” , “friendly-errors-webpack-plugin” : ” ^1.6.1 ” , “html-webpack-plugin” : ” ^2.30.1 ” , “jest” : ” ^22.0.4 ” , “jest-serializer-vue” : ” ^0.3.0 ” , “nightwatch” : ” ^1.0.11 ” , “node-notifier” : ” ^5.1.2 ” , “optimize-css-assets-webpack-plugin” : ” ^3.2.0 ” , “ora” : ” ^1.2.0 ” , “portfinder” : ” ^1.0.17 ” , “postcss-import” : ” ^11.0.0 ” , “postcss-loader” : ” ^2.1.6 ” , “postcss-url” : ” ^7.2.1 ” , “rimraf” : ” ^2.6.0 ” , “selenium-server” : ” ^3.14.0 ” , “semver” : ” ^5.5.1 ” , “shelljs” : ” ^0.7.6 ” , “uglifyjs-webpack-plugin” : ” ^1.3.0 ” , “url-loader” : ” ^1.1.1 ” , “vue-jest” : ” ^1.0.2 ” , “vue-loader” : ” ^13.7.3 ” , “vue-style-loader” : ” ^3.0.1 ” , “vue-template-compiler” : ” ^2.5.17 ” , “webpack” : ” ^3.6.0 ” , “webpack-bundle-analyzer” : ” ^2.13.1 ” , “webpack-dev-server” : ” ^2.11.3 ” , “webpack-merge” : ” ^4.1.4 ” },

As you can see, we´re not only maintaining our high-level libraries of choice like nightwatch, jest and so on. We´re also maintaining libraries that they use itself. Now this is over with Vue CLI 3. Let´s have a look at the super clean dependency block now:

“devDependencies” : { “@vue/cli-plugin-babel” : ” ^3.0.3 ” , “@vue/cli-plugin-e2e-nightwatch” : ” ^3.0.3 ” , “@vue/cli-plugin-unit-jest” : ” ^3.0.3 ” , “@vue/cli-service” : ” ^3.0.3 ” , “@vue/test-utils” : ” ^1.0.0-beta.20 ” , “babel-core” : ” 7.0.0-bridge.0 ” , “babel-jest” : ” ^23.0.1 ” , “node-sass” : ” ^4.9.0 ” , “sass-loader” : ” ^7.0.1 ” , “vue-template-compiler” : ” ^2.5.17 ” },

As you dig into the directories like node_modules/@vue/cli-plugin-e2e-nightwatch , you´ll find where the used libraries of nightwatch are configured – in the respective package.json there:

“dependencies” : { “@vue/cli-shared-utils” : ” ^3.0.2 ” , “chromedriver” : ” ^2.40.0 ” , “deepmerge” : ” ^2.1.1 ” , “execa” : ” ^0.10.0 ” , “nightwatch” : ” ^0.9.21 ” , “selenium-server” : ” ^3.13.0 ” },

This is really cool, I have to admit!

The vue.config.js file

Vue CLI 3 removes the need for explicit configuration files – and thus you wont find any build or config directories in your projects root any more. This now implements a “convention over configuration” approach, which makes it much easier to kick-start a Vue.js project, as it provides widly used defaults to webpack etc. It also eases the upgradeability of Vue.js projects – or even makes it possible.

But: How do we configure webpack etc. for CORS handling, the build directories and so on? This could be done with the optional vue.config.js:

module . exports = { // proxy all webpack dev-server requests starting with /api // to our Spring Boot backend (localhost:8098) using http-proxy-middleware // see https://cli.vuejs.org/config/#devserver-proxy devServer : { proxy : { ‘/api’ : { target : ‘http://localhost:8098’ , ws : true , changeOrigin : true } } } , // Change build paths to make them Maven compatible // see https://cli.vuejs.org/config/ outputDir : ‘target/dist’ }

Updating Vue in an existing project

Update your local @vue/cli to the latest version:

npm install -g @vue/cli

Then update Vue.js and all your other JS dependencies with:

cd frontend npm update

Upgrade to Vue.js 3.x/4.x next

Let’s move from 2.6.x -> 3.x/4.x next here.

Be aware that the latest version of vue currently is 2.6.x and 3.x is considered next !

There are some resources:

https://v3.vuejs.org/guide/migration/introduction.html#quickstart

https://johnpapa.net/vue2-to-vue3/

And if we are using 3.x, we can even migrate to 4.x: https://cli.vuejs.org/migrating-from-v3/

Upgrade from 2.x to 3.x

There’s a migration tooling, simply use:

vue add vue-next

This took around 3 minutes or more on my MacBook and changed some files:

The package.json got some new or upgraded deps:

As John stated in his post it’s strange to find beta versions with vue , vue-router and vuex .

So in order to see what a fresh skeleton would produce, let’s also create one in another dir (I assume you have npm install -g @vue/cli installed:

mkdir vue3test && cd vue3test vue create hello-vue3

I aligned my project to match the latest skeleton generation much better: So router, store and api got their own directories. The views are now in the correct folder views – and I extracted one component to use from the newly introduced Home.vue view: the HelloSpringWorld.vue component.

I also went over the package.json and upgraded to the latest release versions instead of alphas (except @vue/test-utils which only has a rc atm).

All imports were refactored too. Coming from this style:

import Vue from ‘vue’ import Router from ‘vue-router’

everything now reads:

import { createApp } from ‘vue’ ; import { createRouter , createWebHistory } from ‘vue-router’

Also check your router.js or router/index.js! Using a path redirect like this leads to a non working routing configuration:

// otherwise redirect to home { path : ‘*’ , redirect : ‘/’ }

The error in the Browser console states:

Uncaught Error: Catch all routes ( ” * ” ) must now be defined using a param with a custom regexp. See more at https://next.router.vuejs.org/guide/migration/ # removed-star-or-catch-all-routes.

I changed it to the new param with regex syntax like this:

// otherwise redirect to home { path : ‘/:pathMatch(.*)*’ , redirect : ‘/’ }

A crucial point to get jest to work again, was to add the following to the jest.config.js:

transform: { ‘^.+\\.vue$’ : ‘vue-jest’ }

Otherwise my tests ran into the following error:

npm run test:unit > [email protected] test:unit > vue-cli-service test:unit –coverage FAIL tests/unit/views/User.spec.js ● Test suite failed to run Vue packages version mismatch: – [email protected] (/Users/jonashecht/dev/spring-boot/spring-boot-vuejs/frontend/node_modules/vue/index.js) – [email protected] (/Users/jonashecht/dev/spring-boot/spring-boot-vuejs/frontend/node_modules/vue-template-compiler/package.json) This may cause things to work incorrectly. Make sure to use the same version for both. If you are using vue-loader@ > =10.0, simply update vue-template-compiler. If you are using vue-loader@ < 10.0 or vueify, re-installing vue-loader/vueify should bump vue-template-compiler to the latest. at Object. < anonymous > (node_modules/vue-template-compiler/index.js:10:9)

Luckily this so answer helped me out: https://stackoverflow.com/a/65111966/4964553

And finally Bootstrap Vue doesn’t support Vue 3.x right now: bootstrap-vue/bootstrap-vue#5196 – So I temporarily commented out the imports.

Add TypeScript

Vue 3.x is now build with TypeScript: https://v3.vuejs.org/guide/typescript-support.html

A static type system can help prevent many potential runtime errors as applications grow, which is why Vue 3 is written in TypeScript. This means you don’t need any additional tooling to use TypeScript with Vue – it has first-class citizen support.

There’s also a huge documentation of TypeScript itself at https://www.typescriptlang.org/docs/ I can also recommend https://medium.com/js-dojo/adding-typescript-to-your-existing-vuejs-2-6-app-aaa896c2d40a

To migrate your project there’s the command:

vue add typescript

The first question arises: Use class-style component syntax? (Y/n) whether to use class-style component syntax or not. I didn’t use it. I think the interface definitions of components are concise enough without the class-style. But let’s see how this will work out.

So this was the output:

vue add typescript WARN There are uncommitted changes in the current repository, it ‘ s recommended to commit or stash them first. ? Still proceed? Yes 📦 Installing @vue/cli-plugin-typescript… added 59 packages, removed 58 packages, and audited 2219 packages in 6s 85 packages are looking for funding run `npm fund` for details 3 low severity vulnerabilities To address all issues (including breaking changes), run: npm audit fix –force Run `npm audit` for details. ✔ Successfully installed plugin: @vue/cli-plugin-typescript ? Use class-style component syntax? No ? Use Babel alongside TypeScript (required for modern mode, auto-detected polyfills, transpiling JSX)? Yes ? Use TSLint? Yes ? Pick lint features: Lint on save ? Convert all .js files to .ts? Yes ? Allow .js files to be compiled? Yes ? Skip type checking of all declaration files (recommended for apps)? Yes 🚀 Invoking generator for @vue/cli-plugin-typescript… 📦 Installing additional dependencies… added 2 packages, and audited 2221 packages in 3s … ✔ Successfully invoked generator for plugin: @vue/cli-plugin-typescript

Now I went through all the componentes and views and extended

For now the conditional is only handled by two boolean values: loginSuccess and loginError .

To bring those to life, we implement the callLogin() method:

, methods: { callLogin() { api.getSecured(this.user, this.password).then(response => { console.log("Response: '" + response.data + "' with Statuscode " + response.status) if(response.status == 200) { this.loginSuccess = true } }).catch(error => { console.log("Error: " + error) this.loginError = true }) } }

With this simple implementation, the Login component asks the Spring Boot backend, if a user is allowed to access the /api/secured resource. The backend-api.js provides an method, which uses axios' Basic Auth feature:

getSecured(user, password) { return AXIOS.get(`/secured/`,{ auth: { username: user, password: password }}); }

Now the Login component works for the first time:

Protect multiple Vue.js components

Now we have a working Login component. Now let's create a new Protected.vue component, since we want to have something that's only accessible, if somebody has logged in correctly:

This component should only be visible, if the appropriate access was granted at the Login. Therefore we need to solve 2 problems:

Store the login state

Redirect user from Protected.vue to Login.vue, if not authenticated before

Store login information with vuex

The super dooper simple solution would be to simply use LocalStorage . But with vuex there is a centralized state management in Vue.js, which is pretty popular. So we should invest some time to get familiar with it. There's a full guide available: https://vuex.vuejs.org/guide/ and a great introductory blog post here: https://pusher.com/tutorials/authentication-vue-vuex

You could also initialize a new Vue.js project with Vue CLI and mark the vuex checkbox. But we try to extend the current project here.

First we add the vuex dependency into our package.json:

... "vue": "^2.6.10", "vue-router": "^3.0.6", "vuex": "^3.1.1" },

There are four things that go into a Vuex module: the initial state, getters, mutations and actions

Define the vuex state

To implement them, we create a new store.js file:

import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) export default new Vuex.Store({ state: { loginSuccess: false, loginError: false, userName: null }, mutations: { }, actions: { }, getters: { } })

We only have an initial state here, which is that a login could be successful or not - and there should be a userName .

Define a vuex action login() and the mutations login_success & login_error

Then we have a look onto vuex actions: They provide a way to commit mutations to the vuex store.

As our app here is super simple, we only have one action to implement here: login . We omit the logout and register actions, because we only define one admin user in the Spring Boot backend right now and don't need an implemented logout right now. Both could be implemented later!

We just shift our logic on how to login a user from the Login.vue to our vuex action method:

mutations: { login_success(state, name){ state.loginSuccess = true state.userName = name }, login_error(state){ state.loginError = true state.userName = name } }, actions: { async login({commit}, user, password) { api.getSecured(user, password) .then(response => { console.log("Response: '" + response.data + "' with Statuscode " + response.status); if(response.status == 200) { // place the loginSuccess state into our vuex store return commit('login_success', name); } }).catch(error => { console.log("Error: " + error); // place the loginError state into our vuex store commit('login_error', name); return Promise.reject("Invald credentials!") }) } },

Instead of directly setting a boolean to a variable, we commit a mutation to our store if the authentication request was successful or unsuccessful. We therefore implement two simple mutations: login_success & login_error

Last but not least: define getters for the vuex state

To be able to access vuex state from within other components, we need to implement getters inside our vuex store. As we only want some simple info, we need the following getters:

getters: { isLoggedIn: state => state.loginSuccess, hasLoginErrored: state => state.loginError }

Use vuex Store inside the Login component and forward to Protected.vue, if Login succeeded

Instead of directly calling the auth endpoint via axios inside our Login component, we now want to use our vuex store and its actions instead. Therefore we don't even need to import the store.js inside our Login.vue , we can simply access it through $store . Thy is that? Because we already did that inside our main.js:

import store from './store' ... new Vue({ router, store, render: h => h(App) }).$mount('#app')

With that configuration store and router are accessible from within every Vue component with the $ prefixed 🙂

If we have a look into our Login.vue we see that in action:

callLogin() { this.$store.dispatch('login', { user: this.user, password: this.password}) .then(() => this.$router.push('/Protected')) .catch(error => { this.error.push(error) }) }

Here we access our vuex store action login and issue a login request to our Spring Boot backend. If this succeeds, we use the Vue $router to forward the user to our Protected.vue component.

Redirect user from Protected.vue to Login.vue, if not authenticated before

Now let's enhance our router.js slightly. We use the Vue.js routers' meta field feature to check, whether a user is loggin in already and therefore should be able to access our Protected component with the URI /protected :

{ path: '/protected', component: Protected, meta: { requiresAuth: true } },

We also add a new behavior to our router, that checks if it requires authentication every time a route is accessed. If so, it will redirect to our Login component:

router.beforeEach((to, from, next) => { if (to.matched.some(record => record.meta.requiresAuth)) { // this route requires auth, check if logged in // if not, redirect to login page. if (!store.getters.isLoggedIn) { next({ path: '/login' }) } else { next(); } } else { next(); // make sure to always call next()! } });

Now if one clicks onto Protected and didn't login prior, our application redirects to Login automatically:

With this redirect, we also don't need the part with

inside our Login.vue, since in case of a successful login, the user is directly redirected to the Protected.vue.

Check auth state at secured backend endpoints

We're now already where we wanted to be at the first place: Our Spring Boot backend has a secured API endpoint, which works with simple user/password authentication. And our Vue.js frontend uses this endpoint to do a Login and protect the Protected component, if the user didn't log in before. The login state is held in the frontend, using the vuex store.

Now if we want to go a step ahead and call a secured API endpoint in the backend from within our Protected frontend component, we need to fully store the credentials inside our vuex store, so we could access our secured resource

+-----------------------------------------------------------------------+ | Vue.js frontend | | +----------------------------------------+ | | | vuex store | | | +----------------------------------------+ | | | | | | +-----------------+ +-----------------+ +-----------------+ | | | | | | | | | | | | | Login.vue | | Protected | | | | | | | | | | | +-----------------+ +-----------------+ +-----------------+ | | | | | +-------------------------------------------|---------------|-----------+ |-------------| | +---+ +---+ +---+ | | /api/hello | | /api/user | | /api/secured +---+ +---+ +---+ | | | +-----------------------------------------------------------------------+ | | | | | | | | | | | | | Spring Boot backend | +-----------------------------------------------------------------------+

Therefore we enhance our store.js:

export default new Vuex.Store({ state: { loginSuccess: false, loginError: false, userName: null, userPass: null, response: [] }, mutations: { login_success(state, payload){ state.loginSuccess = true; state.userName = payload.userName; state.userPass = payload.userPass; }, ... }, actions: { login({commit}, {user, password}) { ... // place the loginSuccess state into our vuex store commit('login_success', { userName: user, userPass: password }); ... getters: { isLoggedIn: state => state.loginSuccess, hasLoginErrored: state => state.loginError, getUserName: state => state.userName, getUserPass: state => state.userPass }

Be sure to use the current way to define and interact with vuex mutations. Lot's of blog posts are using an old way of committing multiple parameters like commit('auth_success', token, user) . This DOES NOT work anymore. Only the first parameter will be set, the others are lost!

Now inside our Protected.vue, we can use the stored credentials to access our /secured endpoint:

{{ message }}

In the HTML file, we include the Vue.js library from an external CDN source. Upon starting the application, we generate a request to the Spring Boot application and display the message in the div tag.

$ spring run app.groovy

We start the application and navigate to localhost:8080.

In the next example, we list words.

app.groovy └── static └── index.html

This is the project structure.

app.groovy

@RestController class MyApp { @RequestMapping("/words") Map home() { return ['words': ['sky', 'cup', 'snow', 'war', 'water', 'ocean']] } }

The Spring Boot application sends a JSON response containing a list of words.

static/index.html

Document

  • {{word}}

The Vue application fetches the words from the endpoint, parses the JSON data and displays the words inside the HTML list.

Vue.js Java example

In the following example, we create a Spring Boot Java example. This time we will utilize the Vue build system.

vue-app/ ├── backend │ ├── bin │ ├── build │ ├── build.gradle │ ├── gradle │ ├── gradlew │ ├── gradlew.bat │ ├── HELP.md │ ├── settings.gradle │ └── src └── frontend ├── babel.config.js ├── jsconfig.json ├── node_modules ├── package.json ├── package-lock.json ├── public ├── README.md ├── src └── vue.config.js

The project is split into two directories: backend and frontend . The Spring Boot application is in the backend and Vue.js in the frontend.

$ spring init -d web build:gradle backend

We create the Spring Boot project.

build.gradle

plugins { id 'org.springframework.boot' version '2.7.0' id 'io.spring.dependency-management' version '1.0.11.RELEASE' id 'java' } group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = '17' repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' }

This is the Gradle build file.

resources/application.properties

server.servlet.context-path=/api

We set the context-path in the application.properties file.

com/zetcode/controller/HelloController.java

package com.zetcode.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @GetMapping(value="/hello") public String hello() { return "Hello there!"; } }

We have a simple HelloController where we map the /hello URL path to a short text message.

com/zetcode/Application.java

package com.zetcode; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }

We set up the Spring Boot application.

Now we create the frontend part.

$ npm install -g @vue/cli

We install the Vue command line tool.

$ vue create frontend

We create a new Vue application.

src/components/MessageItem.vue

We have a custom MessageItem component, which displays the hello message.

src/App.vue

This is the Vue application. We import the custom component and use it in the template section. We make a request to the Spring Boot application and pass the message to the custom component.

src/main.js

import { createApp } from 'vue' import App from './App.vue' createApp(App).mount('#app')

This is the main.js file which bootstraps Vue application.

vue.config.js

const { defineConfig } = require('@vue/cli-service') module.exports = defineConfig({ transpileDependencies: true, devServer: { port: 8081, proxy: { "/api/hello": { target: "http://localhost:8080", secure: false } } } })

We set the proxy for the development server so that it does not collide with the Spring Boot's server.

$ ./gradlew bootRun

We start the Spring Boot application.

$ npm run serve

We start the Vue.js development server.

After starting the servers, we navigate to localhost:8081.

In this tutorial, we have integrated the Vue.js framework with Spring Boot.

List all Spring Boot tutorials.

Full Stack Java development with Spring Boot and VueJS

💡 The video tutorial for this blog post can be found above or you can click here to watch it on YouTube.

In this tutorial, you will learn how to build a full-stack application that uses Vue for the frontend and Spring Boot for the backend. As always you can find the code associated with this over on Github.

Table of Contents

Design decisions

Monolithic architecture

Creating the project Requirements Spring Boot Vue Build plugins

Conclusion

Design decisions

Creating a brand new application from scratch is exciting, isn't it? The possibilities are endless but the freedom to create whatever you want comes with some future implications. I know your first instinct is to jump right into the deep end and start writing code but before you do, you should have a clear idea of what you're trying to build.

"IF YOU FAIL TO PLAN, YOU ARE PLANNING TO FAIL"

Without fully understanding the problems you are trying to solve for I can't recommend the absolute best solution but I will give you a general approach. One of the first questions I try to answer is "What is the structure of the development team on this project?". This shouldn't be the driving force for all of your decisions but it is an important one.

If you have full-stack developers that will be working on both sides of the application a monolithic approach could make sense. If you have separate teams working on the frontend and backend splitting these projects into two independent projects might make sense. There are pros and cons to each and these should really be evaluated on a per-project basis.

Monolithic architecture

In this article we are going to focus on building a Monolithic application that uses Vue on the fronted and Spring Boot on the backend. A lot of developers I come across are familiar with both of these technologies but aren't quite sure how to configure them in a single project.

Having a single deployable asset simplifies the deployment process which I know a lot of developers don't like to deal with. If you're a single developer or a team of full-stack developers this is a great option. There are some tricks to getting this all into a single project but that is exactly what you will learn in this tutorial.

The biggest advantage to this approach is that you end up with a single artifact to deploy. If you don't have a team dedicated to the DevOps side of development this will help cut down some of the complexities of moving to production.

This also means that if you want to change something small on the frontend you will to redeploy the entire application. If you have a large team there could be a lot of paths crossing which could end up causing lots of merge conflicts. I don't know about you but the only thing worse than real life conflicts is dealing with merge conflicts 😳

Creating the project

You will start by creating a Spring Boot project that will act as the foundation for your project. You can then generate a new Vue project in the Spring Boot project and with some configuration you will be able to put all of the puzzle pieces together.

This could be done using any front end technology (Angular,React,Vue,Svelte,etc..)

Requirements

I am going to make some assumptions about you before we get started. You should be familiar and have some experience with the following:

Java

Maven

Spring Boot

JavaScript

VueJS

Spring Boot

My IDE of choice is IntelliJ Ultimate Edition. If you're using the same you can create the project right in the IDE:

If you're not you can do the same from https://start.spring.io

Start by creating a new Spring Boot project with the following properties:

Group: dev.danvega

dev.danvega Artifact: fsjava

fsjava Type: Maven

Maven Spring Boot: 2.4.2 (or latest)

2.4.2 (or latest) Language: Java

Java Packaging: Jar

Jar Java Version: 11

11 Package: Jar

Jar Dependencies: Spring Boot DevTools Spring Web

The first thing I like to do with any new project is to make sure it runs as expected. Open up the main application class (I renamed mine to Application.java) and run it to make sure

@ SpringBootApplication public class Application { public static void main ( String [] args ) { SpringApplication . run ( Application . class , args ) ; } }

Message Controller

The first class you are going to create is a simple controller that contains a single mapping. This will be a public REST endpoint that you can call from your client application.

@ RestController @ RequestMapping ( " /api/messages " ) public class MessageController { @ GetMapping ( " /hello " ) public String hello () { return " Full Stack Java with Spring Boot & VueJS! " ; } }

With DevTools your application should have restarted after this change, if it hasn't please re-run the application. Open up a browser and navigate to http://localhost:8080/api/messages/hello

Vue

Next, you will create the frontend application using the Vue CLI. If you're in IntelliJ you can open up the terminal or open up what ever shell you use. If you have the Vue CLI installed you can navigate to the /src folder and run the command vue create frontend which will generate a project into /src/frontend . For this project I am generating a Vue 3 application but this will also work with v2.

Once the application has been created navigate to /src/frontend and run the application.

npm run serve

If your Spring Boot application isn't running the client app will start on port 8080, if it is it will find the next available port. At this point you should be able to run both of the applications independently.

Vue Configuration

Next let's add some configuration so to our Vue application that will solve a couple of problems. Create a new file in the root of the /frontend folder named vue.config.js that contains the following:

// vue.config.js module . exports = { // https://cli.vuejs.org/config/#devserver-proxy devServer : { port : 3000 , proxy : { ' /api ' : { target : ' http://localhost:8080 ' , ws : true , changeOrigin : true } } } }

This will set the port to 3000 so we don't collide with the Spring Boot application. Next you're setting a proxy so that any request that starts with /api will be forwarded to http://localhost:8080. This means that you don't have to worry about setting up different environment variables for development and production.

App.vue is the main component that is loaded in your Vue application. You can start by cleaning this up and removing some of the default styles.

HelloWorld Component

Last step on the frontend is to remove the boiler-plate code from the HelloWorld.vue component. In this component you can make a fetch request to your backend API to retrieve the message that is being exposed on your hello endpoint. I am doing this in the mounted() lifecycle hook and I am using the Fetch API.

If you run the Vue application using npm run serve you should see the message from your backend API being displayed.

My development workflow consists of running these as separate applications. I usually start up a new shell in the background for running the Vue application. This gives me one less distraction in IntelliJ and I appreciate the larger iTerm window to view anything going wrong with the client.

Build plugins

When it comes to production the goal of our Monolithic application is to have a single deployable artifact. To accomplish this you will need to build a production version of our VueJS application and copy that to your /target/classes/static directory.

With index.html in the static folder this is the file that will be served when you hit the root. Finally as part of that build process Spring Boot will build a runnable JAR. Open up your pom.xml and make sure you have the following properties set:

11 v10.15.0 6.14.3

Next, locate the section and add the frontend-maven-plugin. This plugin downloads/installs Node and NPM locally for your project, and runs npm install and npm build to build your Vue application. The output for this build is stored in the /src/frontend/dist directory.

com.github.eirslett frontend-maven-plugin 1.7.6 Install node and npm install-node-and-npm generate-resources ${node.version} ${npm.version} npm install npm generate-resources install npm build npm generate-resources run build ${node.version} src/frontend

Next, add the maven-resources-plugin which handles the copying of project resources to the output directory. You will need to copy everything from the src/frontend/dist directory to the target/classes/static directory.

org.apache.maven.plugins maven-resources-plugin Copy Vue frontend into Spring Boot target static folder process-resources copy-resources target/classes/static src/frontend/dist true

And that is all of the configuration you need. From the command-line you can now package the application using Maven:

mvn clean package

When that is complete you should see something that looks like this and it will end up producing a new JAR fsjava-0.0.1-SNAPSHOT.jar for you.

[ INFO ] [ INFO ] Results: [ INFO ] [ INFO ] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [ INFO ] [ INFO ] [ INFO ] --- maven-jar-plugin:3.2.0:jar ( default-jar ) @ fsjava --- [ INFO ] Building jar: /Users/vega/dev/boot/full-stack-java-vue/target/fsjava-0.0.1-SNAPSHOT.jar [ INFO ] [ INFO ] --- spring-boot-maven-plugin:2.4.2:repackage ( repackage ) @ fsjava --- [ INFO ] Replacing main artifact with repackaged archive [ INFO ] ------------------------------------------------------------------------ [ INFO ] BUILD SUCCESS [ INFO ] ------------------------------------------------------------------------ [ INFO ] Total time: 20.083 s [ INFO ] Finished at: 2021-01-22T13:24:38-05:00 [ INFO ] ------------------------------------------------------------------------

You can run this jar from the command line java -jar target/fsjava-0.0.1-SNAPSHOT.jar

With this single artifact you can push this into a production environment and you are ready to go. A few of my favorite options for doing this are:

Conclusion

I wanted to keep the application simple in this article so we could focus on the plumbing of the project. That said if you're interested in seeing more please let me know. Spring Boot and Vue are two technologies I really enjoy using and I hope this helps others put the two together. As always friends...

Happy Coding

Dan

Build a Simple CRUD App with Spring Boot and Vue.js

You will use Vue and Spring Boot to build a todo list web application. The application will include CRUD abilities, meaning that you can create, read, update, and delete the todo items on the Spring Boot API via the client. The Vue frontend client will use the Quasar framework for the presentation. OAuth 2.0 and OpenID Connect (OIDC) will secure the Spring Boot API and the Vue client using Okta as the security provider.

This project has two major parts:

Spring Boot API

Vue client

The Spring Boot app will include an H2 in-memory database and will use Spring Data JPA to map our todo data model to a database table for persistence. As you’ll see, the server will leverage Spring Boot’s ability to quickly expose data via a REST API with minimal configuration.

The client will use Vue 3 and the Quasar framework. The Quasar framework provides components and layout tools to help build Vue applications quickly with a consistent, high-quality user interface.

Before you dig into the tutorial, I want to quickly introduce the technologies for those that might be unfamiliar. Feel free to skip down to the prerequisites section if you’re already familiar with Vue and Spring Boot.

Table of Contents

What is Vue.js?

Vue is a JavaScript view library, like React and Angular. It’s designed to be incrementally adoptable, and the core library focuses solely on the view layer.

In my experience, Vue.js is a great alternative to React. I learned React first and came to use Vue later. Like React, Vue uses a virtual DOM, provides reactive and composable view components, and enforces a strict one-way parent-child relationship when defining properties and state. This means that it is performant and avoids many confusing state relationships that can occur without one-way data binding.

However, unlike React, Vue uses templates instead of JSX (a potentially welcome and more immediately accessible option). Vue gives you component-scoped CSS using style tags in single-file components. In practice, this difference is pretty significant because, in React, the JSX and CSS-like syntax are close enough to HTML and CSS to be confusing but not the same, which creates problems initially. (Ever gone from a language that doesn’t require semicolons back to one that does? It’s something like that.)

I find Vue to be a simpler, cleaner implementation. React requires a deep dive. You gotta take the red pill and go all the way. It’s a super powerful system, but you have to be all in. Vue is a little friendlier and a little easier to get started.

Introducing the Quasar Framework

The Quasar Framework builds on top of Vue to add a cross-platform component library and grid layout system. It also provides many tools for deploying Vue-based applications to basically any platform you can think of, from web single-page and progressive web apps to mobile apps and Electron-based desktop apps. In this tutorial, you’ll only be using the layout and component library features. Still, Quasar’s big push is to allow developers to write a single web application and deploy it to any platform with a consistent look with minimal changes.

About Spring Boot

The server technology you’re going to use is Spring Boot. Pure, unadulterated Spring (pre-Spring Boot) is a bit of a behemoth: super powerful but potentially time-sucking and frustrating. I’m pretty sure the whole computer conference phenomena came about so that people could learn and understand old-school Spring XML files. It certainly drove large sections of the computer publishing empires.

Spring Boot was Spring’s answer to this complexity (and to frameworks like Ruby on Rails and Grails). They did a great job of distilling down all the power of Spring into a simple, quick, easy-to-use web framework. You can have a fully functioning resource server with a ridiculously small number of lines of code and a few annotations.

Plus, when you’re ready, you have all the power of Spring under the hood, just waiting.

Prerequisites:

Before you start, please make sure you have the following prerequisites installed (or install them now).

Java 11: or use SDKMAN! to manage and install multiple versions

Okta CLI: the Okta command-line interface

HTTPie: a simple tool for making HTTP requests from a Bash shell

Node 16+

Vue CLI: you’ll use this to bootstrap the Vue client

You will need a free Okta Developer account if you don’t already have one. But you can wait until later in the tutorial and use the Okta CLI to log in or register for a new account.

Instead of building the project, you can also clone the repo and follow the instructions there to configure it.

Create an OpenID Connect app

Open a Bash shell. Create a parent directory for the project. Eventually, this will include both the resource server and client projects.

mkdir spring-boot-vue-crud cd spring-boot-vue-crud

Before you begin, you’ll need a free Okta developer account. Install the Okta CLI and run okta register to sign up for a new account. If you already have an account, run okta login . Then, run okta apps create . Select the default app name, or change it as you see fit. Choose Single-Page App and press Enter.

Use http://localhost:8080/callback for the Redirect URI and accept the default Logout Redirect URI of http://localhost:8080 .

What does the Okta CLI do? The Okta CLI will create an OIDC Single-Page App in your Okta Org. It will add the redirect URIs you specified and grant access to the Everyone group. It will also add a trusted origin for http://localhost:8080 . You will see output like the following when it’s finished: Okta application configuration: Issuer: https://dev-133337.okta.com/oauth2/default Client ID: 0oab8eb55Kb9jdMIr5d6 NOTE: You can also use the Okta Admin Console to create your app. See Create a Vue App for more information.

Copy the client ID and issuer URI somewhere safe. You’ll need them for both the client and resource server applications.

Bootstrap a Spring Boot app using Spring Initializr

You’re going to use the Spring Initializr to create a starter project for the resource server. You can look at the project website if you want, but here you’ll use the REST API to download a pre-configured starter.

The following command will download the starter project and un-tar it to a new directory named resource-server .

curl https://start.spring.io/starter.tgz \ -d bootVersion = 2.7.3 \ -d javaVersion = 11 \ -d dependencies = web,data-rest,lombok,data-jpa,h2,okta \ -d type = gradle-project \ -d baseDir = resource-server \ | tar -xzvf - && cd resource-server

The dependencies you’re including are:

Project Lombok saves a lot of clutter and ceremony code. However, if you’re using an IDE, you’ll need to install a plugin for Lombok. See the project’s installation docs for more information.

Configure Spring Security

Open the application properties file and update it. You’re changing the server port so it doesn’t conflict with the default Vue local server (which also defaults 8080 ).

src/main/resources/application.properties

server.port = 9000 okta.oauth2.issuer = okta.oauth2.clientId =

You need to replace the two bracketed values with the values you generated above for the OIDC app using the Okta CLI.

You can run the bootstrapped project right now and see if it starts. It should start but won’t do much.

./gradlew bootRun

Create a SecurityConfiguration class to configure Spring Security. The class below configures web security to allow all requests, effectively bypassing security. This is just so you can test the resource server initially. You’ll enable security shortly.

src/main/java/com/example/demo/SecurityConfiguration.java

package com.example.demo ; import org.springframework.context.annotation.Bean ; import org.springframework.context.annotation.Configuration ; import org.springframework.security.config.annotation.web.builders.HttpSecurity ; import org.springframework.security.web.SecurityFilterChain ; @Configuration public class SecurityConfiguration { @Bean public SecurityFilterChain filterChain ( HttpSecurity http ) throws Exception { http . authorizeRequests () . anyRequest (). permitAll (); return http . build (); } }

Replace the DemoApplication.java file with the following.

src/main/java/com/example/demo/DemoApplication.java

package com.example.demo ; import org.springframework.boot.ApplicationRunner ; import org.springframework.boot.SpringApplication ; import org.springframework.boot.autoconfigure.SpringBootApplication ; import org.springframework.boot.web.servlet.FilterRegistrationBean ; import org.springframework.context.annotation.Bean ; import org.springframework.core.Ordered ; import org.springframework.data.rest.core.config.RepositoryRestConfiguration ; import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer ; import org.springframework.stereotype.Component ; import org.springframework.web.cors.CorsConfiguration ; import org.springframework.web.cors.UrlBasedCorsConfigurationSource ; import org.springframework.web.filter.CorsFilter ; import org.springframework.web.servlet.config.annotation.CorsRegistry ; import java.util.Collections ; import java.util.Random ; import java.util.stream.Stream ; @SpringBootApplication public class DemoApplication { public static void main ( String [] args ) { SpringApplication . run ( DemoApplication . class , args ); } // Bootstrap some test data into the in-memory database @Bean ApplicationRunner init ( TodoRepository repository ) { return args -> { Random rd = new Random (); Stream . of ( "Buy milk" , "Eat pizza" , "Update tutorial" , "Study Vue" , "Go kayaking" ). forEach ( name -> { Todo todo = new Todo (); todo . setTitle ( name ); todo . setCompleted ( rd . nextBoolean ()); repository . save ( todo ); }); repository . findAll (). forEach ( System . out :: println ); }; } // Fix the CORS errors @Bean public FilterRegistrationBean simpleCorsFilter () { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource (); CorsConfiguration config = new CorsConfiguration (); config . setAllowCredentials ( true ); // *** URL below needs to match the Vue client URL and port *** config . setAllowedOrigins ( Collections . singletonList ( "http://localhost:8080" )); config . setAllowedMethods ( Collections . singletonList ( "*" )); config . setAllowedHeaders ( Collections . singletonList ( "*" )); source . registerCorsConfiguration ( "/**" , config ); FilterRegistrationBean bean = new FilterRegistrationBean <>( new CorsFilter ( source )); bean . setOrder ( Ordered . HIGHEST_PRECEDENCE ); return bean ; } // Expose IDs of Todo items @Component class RestRespositoryConfigurator implements RepositoryRestConfigurer { public void configureRepositoryRestConfiguration ( RepositoryRestConfiguration config , CorsRegistry cors ) { config . exposeIdsFor ( Todo . class ); } } }

This demo application does three things that are helpful for demonstration purposes. First, it loads some test todo items into the repository.

Second, it configures the REST repository to expose IDs for the todo items.

Third, it defines a filter to allow cross-origin requests from http://localhost:8080 . This is necessary so that the Vue application, which is loaded from http://localhost:9000 via the local test server, can load data from the Spring Boot resource server at http://localhost:8080 .

For more info on CORS (cross-origin resource sharing), take a look at the Mozilla docs.

Now, create the data model for the todo items.

src/main/java/com/example/demo/Todo.java

package com.example.demo ; import lombok.* ; import javax.persistence.Id ; import javax.persistence.GeneratedValue ; import javax.persistence.Entity ; @Entity @Data @NoArgsConstructor public class Todo { @Id @GeneratedValue private Long id ; @NonNull private String title ; private Boolean completed = false ; }

Notice the use of the Lombok annotations ( @Entity , @Data , and @NoArgsConstructor ) to keep the code simple and clean.

The todo items have two fields: a title string and a completed boolean. The fields are annotated with Spring Data JPA annotations that allow the Java class to be mapped to a database table for persistence.

Create a repository to persist the data model.

src/main/java/com/example/demo/TodoRepository.java

package com.example.demo ; import org.springframework.data.jpa.repository.JpaRepository ; import org.springframework.data.rest.core.annotation.RepositoryRestResource ; @RepositoryRestResource interface TodoRepository extends JpaRepository < Todo , Long > {}

This is a Spring Data JpaRepository that can persist the data model you just defined. Because it is annotated with @RepositoryRestResource (and because the data-rest dependency was included), this repository will be automatically exposed as a web resource.

Test your Vue and Spring Boot app

Run the app using the following command from the resource-server subdirectory.

./gradlew bootRun

Open a new Bash shell and use HTTPie to test the resource server.

http :9000/todos

You should see a response like the following:

HTTP/1.1 200 ... { "_embedded" : { "todos" : [ { "_links" : { "self" : { "href" : "http://localhost:9000/todos/1" } , "todo" : { "href" : "http://localhost:9000/todos/1" } } , "completed" : false , "id" : 1, "title" : "Buy milk" } , { "_links" : { "self" : { "href" : "http://localhost:9000/todos/2" } , "todo" : { "href" : "http://localhost:9000/todos/2" } } , "completed" : true , "id" : 2, "title" : "Eat pizza" } , ... ] } , ... }

Stop the resource server using CTRL + C .

Secure your Spring Boot API

Edit the SecurityConfiguration.java file and change the filter chain’s bean definition to enable a resource server.

src/main/java/com/example/demo/OAuth2ResourceServerSecurityConfiguration.java

@Bean public SecurityFilterChain filterChain ( HttpSecurity http ) throws Exception { http . authorizeRequests () . anyRequest (). authenticated () . and () . oauth2ResourceServer (). jwt (); return http . build (); }

This configuration requires JWT auth on all requests.

Restart the server. Use CTRL + C to stop it if it’s running.

./gradlew bootRun

Use HTTPie again to try and request the todo items.

http :9000/todos

You will get an error.

HTTP/1.1 401 ... 401 Unauthorized

The resource server is finished. The next step is to create the Vue client.

Create a Vue JavaScript client

Use the Vue CLI to create a new application from the project’s root directory and navigate into the newly created client directory. Install the Vue CLI if you don’t have it installed with npm i -g @vue/cli .

vue create client cd client

Pick Default ([Vue 3] babel, eslint) when prompted.

Wait for it to finish. Add the Quasar framework.

vue add quasar

You can just accept the defaults. For me, they were the following.

Allow Quasar to replace App.vue, About.vue, Home.vue and (if available) router.js? Yes

Pick your favorite CSS preprocessor: Sass with indented syntax

Choose Quasar Icon Set: Material Icons (recommended)

Default Quasar language pack: en-US

Use RTL support? No

Select features: Enter to select none

Add additional dependencies for HTTP requests, logging, routing, and authentication.

npm i [email protected] [email protected] [email protected] @okta/[email protected]

axios : an HTTP client request library

: an HTTP client request library vuejs3-logger : a logging library

: a logging library vue-router : the standard for routing between pages in Vue

: the standard for routing between pages in Vue okta/okta-vue : the Okta helper for Vue

To learn more about how Okta integrates with Vue, look at the GitHub page for the okta/okta-vue project. More resources and example applications are listed in the Okta docs for Vue.

Replace main.js with the following. Look at the OktaAuth configuration object. Notice the client ID and issuer URI are pulled from a .env file.

src/main.js

import { createApp } from ' vue ' import App from ' ./App.vue ' import { Quasar } from ' quasar ' import quasarUserOptions from ' ./quasar-user-options ' import VueLogger from ' vuejs3-logger ' import router from ' ./router ' import createApi from ' ./Api ' import { OktaAuth } from ' @okta/okta-auth-js ' import OktaVue from ' @okta/okta-vue ' if ( process . env . VUE_APP_ISSUER_URI == null || process . env . VUE_APP_CLIENT_ID == null || process . env . VUE_APP_SERVER_URI == null ) { throw " Please define VUE_APP_ISSUER_URI, VUE_APP_CLIENT_ID, and VUE_APP_SERVER_URI in .env file " } const oktaAuth = new OktaAuth ({ issuer : process . env . VUE_APP_ISSUER_URI , // pulled from .env file clientId : process . env . VUE_APP_CLIENT_ID , // pulled from .env file redirectUri : window . location . origin + ' /callback ' , scopes : [ ' openid ' , ' profile ' , ' email ' ] }) const options = { isEnabled : true , logLevel : ' debug ' , stringifyArguments : false , showLogLevel : true , showMethodName : false , separator : ' | ' , showConsoleColors : true }; const app = createApp ( App ) . use ( Quasar , quasarUserOptions ) . use ( VueLogger , options ) . use ( OktaVue , { oktaAuth }) . use ( router ) app . config . globalProperties . $api = createApi ( app . config . globalProperties . $auth ) app . mount ( ' #app ' )

Stated very briefly, the file above creates the main Vue app and configures it to use the dependencies you added: Quasar, VueLogger, OktaVue, and the router. It also creates the API class that handles the requests to the resource server and passes it the $auth object it needs to get the JWT.

Create a .env file in the client project root directory. The Client ID and Issuer URI are the values you used above in the Spring Boot application.properties file. The Server URI is the local URI for the Spring Boot API. You can leave this unless you made a change (this gets used in the Api.js file).

.env

VUE_APP_CLIENT_ID= VUE_APP_ISSUER_URI= VUE_APP_SERVER_URI=http://localhost:9000

It’s important to note that putting values like this in a .env file in a client application does not make them secure. It helps by keeping them out of a repository. However, they are still public because they are necessarily visible in the JavaScript code sent to the browser. In this use case, it’s more of a configuration and organizational tool than a security tool.

If you want to keep the .env file out of the repository, you need to update the .gitignore file. There’s no particular need to do this for the Client ID as it will be publicly available anyway.

Replace App.vue with the following.

src/App.vue

< template > Todo App {{ this . claims && this . claims . email ? claims . email : '' }} < script > export default { name : ' LayoutDefault ' , data : function () { return { claims : null } }, watch : { ' authState.isAuthenticated ' () { this . $log . debug (( " watch triggered! " )) this . updateClaims () } }, created () { this . updateClaims () }, methods : { async updateClaims () { if ( this . authState && this . authState . isAuthenticated ) { this . claims = await this . $auth . getUser () } }, async login () { await this . $auth . signInWithRedirect ({ originalUri : ' /todos ' }) }, async logout () { await this . $auth . signOut () } }, }

This top-level component defines the header bar and includes the router component. The header bar has a login or logout button and will show the authenticated user’s email address when logged in.

The app gets the authenticated user’s email address from the JWT claims. (A claim is a piece of information asserted about the subject by the authenticating authority.) This happens in the updateClaims() method, which is triggered when the component is created, and is also triggered by a watch method so that it is updated as the authenticated state changes.

Create a new file to encapsulate the resource server access logic.

src/Api.js

import axios from ' axios ' const instance = axios . create ({ baseURL : process . env . VUE_APP_SERVER_URI , timeout : 2000 }); const createApi = ( auth ) => { instance . interceptors . request . use ( async function ( config ) { let accessToken = auth . getAccessToken () config . headers = { Authorization : `Bearer ${ accessToken } ` } return config ; }, function ( error ) { return Promise . reject ( error ); }); return { // (C)reate createNew ( text , completed ) { return instance . post ( ' /todos ' , { title : text , completed : completed }) }, // (R)ead getAll () { return instance . get ( ' /todos ' , { transformResponse : [ function ( data ) { return data ? JSON . parse ( data ). _embedded . todos : data ; }] }) }, // (U)pdate updateForId ( id , text , completed ) { return instance . put ( ' todos/ ' + id , { title : text , completed : completed }) }, // (D)elete removeForId ( id ) { return instance . delete ( ' todos/ ' + id ) } } } export default createApi

All of the requests to the server go through this module. Take a look at how the access token is retrieved from the global auth object and injected into every request.

Create the router file.

src/router/index.js

import { createRouter , createWebHistory } from ' vue-router ' import { navigationGuard } from ' @okta/okta-vue ' import Todos from " @/components/Todos " ; import Home from " @/components/Home " ; import { LoginCallback } from ' @okta/okta-vue ' const routes = [ { path : ' / ' , component : Home }, { path : ' /todos ' , component : Todos , meta : { requiresAuth : true } }, { path : ' /callback ' , component : LoginCallback }, ] const router = createRouter ({ history : createWebHistory ( process . env . BASE_URL ), routes , }) router . beforeEach ( navigationGuard ) export default router

The router has three paths. The home path and the todos path are straightforward. The Okta Vue SDK provides the last path, /callback , to handle the login redirect from the Okta servers after authentication.

Create the Home component.

src/components/Home.vue

< template >

You are logged in as {{ claims . email }}
You are logged in

Go to Todo app Log out

Please log in to access Todo app

< script > export default { name : " home-component " , data : function () { return { claims : '' } }, created () { this . setup () }, methods : { async setup () { if ( this . authState && this . authState . isAuthenticated ) { this . claims = await this . $auth . getUser () } }, todo () { this . $router . push ( " /todos " ) }, async login () { await this . $auth . signInWithRedirect ({ originalUri : ' /todos ' }) }, async logout () { await this . $auth . signOut () } } }

Create the TodoItem component.

src/components/TodoItem.vue

< template > {{ this . item . title }} < script > import { nextTick } from ' vue ' export default { name : " TodoItem " , props : { item : Object , deleteMe : Function , showError : Function , setCompleted : Function , setTitle : Function }, data : function () { return { editing : false , editingTitle : this . item . title , } }, methods : { handleClickEdit () { this . editing = true this . editingTitle = this . item . title nextTick ( function () { this . $refs . input . focus () }. bind ( this )) }, handleCancelEditing () { this . editing = false }, handleDoneEditing () { this . editing = false this . $api . updateForId ( this . item . id , this . editingTitle , this . item . completed ). then (( response ) => { this . setTitle ( this . item . id , this . editingTitle ) this . $log . info ( " Item updated: " , response . data ); }). catch (( error ) => { this . showError ( " Failed to update todo title " ) this . $log . debug ( error ) }); }, handleClickSetCompleted ( value ) { this . $api . updateForId ( this . item . id , this . item . title , value ). then (( response ) => { this . setCompleted ( this . item . id , value ) this . $log . info ( " Item updated: " , response . data ); }). catch (( error ) => { this . showError ( " Failed to update todo completed status " ) this . $log . debug ( error ) }); }, handleClickDelete () { this . deleteMe ( this . item . id ) } } } < style scoped > .todo-item .close-icon { min-width : 0px ; padding-left : 5px !important ; } .todo-item .hide-icon { opacity : 0.1 ; } .todo-item :hover .hide-icon { opacity : 0.8 ; } .check-icon { min-width : 0px ; padding-right : 5px !important ; } input .list-item-input { border : none ; }

This component encapsulates a single todo item. It has logic for editing the title, setting the completed status, and deleting items. If you look closely at the code, you’ll notice that it sends changes to the server and updates the local copy stored in the todos array in the parent component.

Create the Todos component.

src/components/Todos.vue

< template >

Todos

Filter the todos

ERROR: {{ this . error }}

< script > import TodoItem from " @/components/TodoItem " ; import { ref } from ' vue ' export default { name : ' LayoutDefault ' , components : { TodoItem }, data : function () { return { todos : [], newTodoTitle : '' , visibility : ' all ' , loading : true , error : "" , filter : " all " } }, setup () { return { alert : ref ( false ), } }, mounted () { this . $api . getAll () . then ( response => { this . $log . debug ( " Data loaded: " , response . data ) this . todos = response . data }) . catch ( error => { this . $log . debug ( error ) this . error = " Failed to load todos " }) . finally (() => this . loading = false ) }, computed : { filteredTodos () { if ( this . filter === ' all ' ) return this . todos else if ( this . filter === ' complete ' ) return this . todos . filter ( todo => todo . completed ) else if ( this . filter === ' incomplete ' ) return this . todos . filter ( todo => ! todo . completed ) else return [] } }, methods : { handleSetFilter ( value ) { this . filter = value }, handleClickDelete ( id ) { const todoToRemove = this . todos . find ( todo => todo . id === id ) this . $api . removeForId ( id ). then (() => { this . $log . debug ( " Item removed: " , todoToRemove ); this . todos . splice ( this . todos . indexOf ( todoToRemove ), 1 ) }). catch (( error ) => { this . $log . debug ( error ); this . error = " Failed to remove todo " }); }, handleDeleteCompleted () { const completed = this . todos . filter ( todo => todo . completed ) Promise . all ( completed . map ( todoToRemove => { return this . $api . removeForId ( todoToRemove . id ). then (() => { this . $log . debug ( " Item removed: " , todoToRemove ); this . todos . splice ( this . todos . indexOf ( todoToRemove ), 1 ) }). catch (( error ) => { this . $log . debug ( error ); this . error = " Failed to remove todo " return error }) })) }, handleDoneEditingNewTodo () { const value = this . newTodoTitle && this . newTodoTitle . trim () if ( ! value ) { return } this . $api . createNew ( value , false ). then (( response ) => { this . $log . debug ( " New item created: " , response ) this . newTodoTitle = "" this . todos . push ({ id : response . data . id , title : value , completed : false }) this . $refs . newTodoInput . blur () }). catch (( error ) => { this . $log . debug ( error ); this . error = " Failed to add todo " }); }, handleCancelEditingNewTodo () { this . newTodoTitle = "" }, handleSetCompleted ( id , value ) { let todo = this . todos . find ( todo => id === todo . id ) todo . completed = value }, handleSetTitle ( id , value ) { let todo = this . todos . find ( todo => id === todo . id ) todo . title = value }, handleShowError ( message ) { this . error = message }, handleErrorClick () { this . error = null ; }, }, } < style > #row-container { margin-top : 100px ; } .my-card { min-width : 600px ; } .error { color : red ; text-align : center ; min-width : 600px ; margin-top : 10px ; }

This component encapsulates the card that holds all of the todos and the todo-associated interface elements. It also handles the rest of the functions related to updating todos on the server and in the local cache.

You’re welcome to delete the HelloWorld.vue component if you want. Or you can leave it. It’s not needed.

Confirm your Spring Boot and Vue todo app works

Make sure the Spring Boot API is still running. In a separate Bash shell, from the resource server directory, run the following command (if it is not already still running).

./gradlew bootRun

Start the Vue app using the embedded development server. From the client directory:

npm run serve

Open a browser and navigate to http://localhost:8080 . You’ll see the “please log in” page.

Log into the app using Okta’s sign-in interface.

That will redirect you to the Todo app’s main screen.

You should be able to delete items, add new items, rename, and filter items. All data is stored on the Spring Boot resource server and is presented by the Vue + Quasar frontend.

Do more with Spring Boot, Vue, and Okta

You built a Spring Boot resource server backend and a Vue frontend in this tutorial. The Vue client used the latest Vue 3 version with the Quasar framework. The app included full CRUD (create, read, update, and delete) capabilities. It was all secured using Okta.

You can find the source code for this example on GitHub in the @oktadev/okta-spring-boot-vue-crud-example repository.

If you liked this post, there’s a good chance you’ll like similar ones:

If you have questions, please ask them in the comments below! If you’re into social media, follow us: @oktadev on Twitter, Okta for Developers on LinkedIn, and OktaDev on Facebook. If you like learning via video, subscribe to our YouTube channel.

키워드에 대한 정보 spring boot vue

다음은 Bing에서 spring boot vue 주제에 대한 검색 결과입니다. 필요한 경우 더 읽을 수 있습니다.

이 기사는 인터넷의 다양한 출처에서 편집되었습니다. 이 기사가 유용했기를 바랍니다. 이 기사가 유용하다고 생각되면 공유하십시오. 매우 감사합니다!

사람들이 주제에 대해 자주 검색하는 키워드 Vue JS + Spring Boot REST API Tutorial | Full Stack Development

  • spring boot
  • vue
  • java guides
  • java

Vue #JS #+ #Spring #Boot #REST #API #Tutorial #| #Full #Stack #Development


YouTube에서 spring boot vue 주제의 다른 동영상 보기

주제에 대한 기사를 시청해 주셔서 감사합니다 Vue JS + Spring Boot REST API Tutorial | Full Stack Development | spring boot vue, 이 기사가 유용하다고 생각되면 공유하십시오, 매우 감사합니다.

Leave a Comment