首页 > 其他分享 >【Spring Boot 整合 Angular】

【Spring Boot 整合 Angular】

时间:2024-06-11 23:58:33浏览次数:27  
标签:License Spring under Boot springframework file import org Angular


今天我们尝试Spring Boot整合Angular,并决定建立一个非常简单的Spring Boot微服务,使用Angular作为前端渲编程语言进行前端页面渲染.

基础环境

技术版本
Java1.8+
SpringBoot1.5.x
创建项目

  • 初始化项目
bashmvn archetype:generate -DgroupId=com.edurt.sli.sliss -DartifactId=spring-learn-integration-springboot-storage -DarchetypeArtifactId=maven-archetype-quickstart -Dversion=1.0.0 -DinteractiveMode=false
  • 修改pom.xml增加java和springboot的支持
xml<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <parent>
        <artifactId>spring-learn-integration-springboot</artifactId>
        <groupId>com.edurt.sli</groupId>
        <version>1.0.0</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring-learn-integration-springboot-storage</artifactId>

    <name>SpringBoot开发存储服务器</name>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${dependency.springboot.version}</version>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${plugin.maven.compiler.version}</version>
                <configuration>
                    <source>${system.java.version}</source>
                    <target>${system.java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
  • 一个简单的应用类
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edurt.sli.sliss;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * <p> SpringBootStorageIntegration </p>
 * <p> Description : SpringBootStorageIntegration </p>
 * <p> Author : qianmoQ </p>
 * <p> Version : 1.0 </p>
 * <p> Create Time : 2019-06-10 15:53 </p>
 * <p> Author Email: <a href="mailTo:[email protected]">qianmoQ</a> </p>
 */
@SpringBootApplication
public class SpringBootStorageIntegration {

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

}
添加Rest API接口功能(提供上传服务)

  • 创建一个controller文件夹并在该文件夹下创建UploadController Rest API接口,我们提供一个简单的文件上传接口
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edurt.sli.sliss.controller;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * <p> UploadController </p>
 * <p> Description : UploadController </p>
 * <p> Author : qianmoQ </p>
 * <p> Version : 1.0 </p>
 * <p> Create Time : 2019-06-10 15:55 </p>
 * <p> Author Email: <a href="mailTo:[email protected]">qianmoQ</a> </p>
 */
@RestController
@RequestMapping(value = "upload")
public class UploadController {

    // 文件上传地址
    private final static String UPLOADED_FOLDER = "/Users/shicheng/Desktop/test/";

    @PostMapping
    public String upload(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return "上传文件不能为空";
        }
        try {
            byte[] bytes = file.getBytes();
            Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
            Files.write(path, bytes);
            return "上传文件成功";
        } catch (IOException ioe) {
            return "上传文件失败,失败原因: " + ioe.getMessage();
        }
    }

    @GetMapping
    public Object get() {
        File file = new File(UPLOADED_FOLDER);
        String[] filelist = file.list();
        return filelist;
    }

    @DeleteMapping
    public String delete(@RequestParam(value = "file") String file) {
        File source = new File(UPLOADED_FOLDER + file);
        source.delete();
        return "删除文件" + file + "成功";
    }

}
  • 修改SpringBootAngularIntegration类文件增加以下设置扫描路径,以便扫描Controller
bash/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edurt.sli.sliss;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

/**
 * <p> SpringBootStorageIntegration </p>
 * <p> Description : SpringBootStorageIntegration </p>
 * <p> Author : qianmoQ </p>
 * <p> Version : 1.0 </p>
 * <p> Create Time : 2019-06-10 15:53 </p>
 * <p> Author Email: <a href="mailTo:[email protected]">qianmoQ</a> </p>
 */
@SpringBootApplication
@ComponentScan(value = {
        "com.edurt.sli.sliss.controller"
})
public class SpringBootStorageIntegration {

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

}
启动服务,测试API接口可用性

在编译器中直接启动SpringBootStorageIntegration类文件即可,或者打包jar启动,打包命令mvn clean package

  • 测试上传文件接口
bashcurl localhost:8080/upload -F "file=@/Users/shicheng/Downloads/qrcode/qrcode_for_ambari.jpg"

返回结果

bash上传文件成功
  • 测试查询文件接口
bashcurl localhost:8080/upload

返回结果

bash["qrcode_for_ambari.jpg"]
  • 测试删除接口
bashcurl -X DELETE 'localhost:8080/upload?file=qrcode_for_ambari.jpg'

返回结果

bash删除文件qrcode_for_ambari.jpg成功

再次查询查看文件是否被删除

bashcurl localhost:8080/upload

返回结果

bash[]
增加下载文件支持

  • 在controller文件夹下创建DownloadController Rest API接口,我们提供一个文件下载接口
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edurt.sli.sliss.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * <p> DownloadController </p>
 * <p> Description : DownloadController </p>
 * <p> Author : qianmoQ </p>
 * <p> Version : 1.0 </p>
 * <p> Create Time : 2019-06-10 16:21 </p>
 * <p> Author Email: <a href="mailTo:[email protected]">qianmoQ</a> </p>
 */
@RestController
@RequestMapping(value = "download")
public class DownloadController {

    private final static String UPLOADED_FOLDER = "/Users/shicheng/Desktop/test/";

    @GetMapping
    public String download(@RequestParam(value = "file") String file,
                           HttpServletResponse response) {
        if (!file.isEmpty()) {
            File source = new File(UPLOADED_FOLDER + file);
            if (source.exists()) {
                response.setContentType("application/force-download");// 设置强制下载不打开
                response.addHeader("Content-Disposition", "attachment;fileName=" + file);// 设置文件名
                byte[] buffer = new byte[1024];
                FileInputStream fileInputStream = null;
                BufferedInputStream bufferedInputStream = null;
                try {
                    fileInputStream = new FileInputStream(source);
                    bufferedInputStream = new BufferedInputStream(fileInputStream);
                    OutputStream outputStream = response.getOutputStream();
                    int i = bufferedInputStream.read(buffer);
                    while (i != -1) {
                        outputStream.write(buffer, 0, i);
                        i = bufferedInputStream.read(buffer);
                    }
                    return "文件下载成功";
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (bufferedInputStream != null) {
                        try {
                            bufferedInputStream.close();
                        } catch (IOException e) {
                            return "文件下载失败,失败原因: " + e.getMessage();
                        }
                    }
                    if (fileInputStream != null) {
                        try {
                            fileInputStream.close();
                        } catch (IOException e) {
                            return "文件下载失败,失败原因: " + e.getMessage();
                        }
                    }
                }
            }
        }
        return "文件下载失败";
    }

}
  • 测试下载文件
bashcurl -o a.jpg 'localhost:8080/download?file=qrcode_for_ambari.jpg'

出现以下进度条

ruby  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  148k    0  148k    0     0  11.3M      0 --:--:-- --:--:-- --:--:-- 12.0M

查询是否下载到本地文件夹

bashls a.jpg

返回结果

basha.jpg
文件大小设置

默认情况下,Spring Boot最大文件上传大小为1MB,您可以通过以下应用程序属性配置值:

  • 配置文件
#http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-properties
#search multipart
spring.http.multipart.max-file-size=10MB
spring.http.multipart.max-request-size=10MB
  • 代码配置,创建一个config文件夹,并在该文件夹下创建MultipartConfig
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edurt.sli.sliss.config;

import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.MultipartConfigElement;

/**
 * <p> MultipartConfig </p>
 * <p> Description : MultipartConfig </p>
 * <p> Author : qianmoQ </p>
 * <p> Version : 1.0 </p>
 * <p> Create Time : 2019-06-10 16:34 </p>
 * <p> Author Email: <a href="mailTo:[email protected]">qianmoQ</a> </p>
 */
@Configuration
public class MultipartConfig {

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setMaxFileSize("10240KB"); //KB,MB
        factory.setMaxRequestSize("102400KB");
        return factory.createMultipartConfig();
    }

}
打包文件部署

  • 打包数据
bashmvn clean package -Dmaven.test.skip=true -X

运行打包后的文件即可

bashjava -jar target/spring-learn-integration-springboot-storage-1.0.0.jar
源码地址

标签:License,Spring,under,Boot,springframework,file,import,org,Angular
From: https://blog.csdn.net/m0_50116974/article/details/139611463

相关文章

  • 基于jeecgboot-vue3的Flowable流程--抄送我的功能
    因为这个项目license问题无法开源,更多技术支持与服务请加入我的知识星球。1、抄送我的界面代码如下:<template><divclass="p-2"><!--查询区域--><divclass="jeecg-basic-table-form-container"><a-formref="formRef"@keyup.enter.nati......
  • spring-1-IOC、创建bean的方式、创建bean的过程
    1.背景IOC(InversionofControl,控制反转)控制反转是一种设计原则,它将对象的创建和管理责任从应用代码中移交给容器。在Spring中,IOC容器负责管理应用中的所有对象,包括它们的生命周期和相互之间的依赖关系。IOC的主要目的是为了减少代码之间的耦合,使代码更加模块化和可测试。这......
  • SpringBoot内置数据源
    回顾:在我们之前学习在配置文件当中配置对应的数据源的时候,我们设置的数据源其实都是Druid的数据源,并且其配置有两种方式,当然这两种方式都需要我们导入对应的有关德鲁伊的依赖才行一种是直接在开始设置为druid数据源类型的一种是在对应的正常的数据库配置下,设置......
  • 【Azure Spring Apps】Spring App部署上云遇见 502 Bad Gateway nginx
    问题描述在部署AzureSpringApp应用后,访问应用,遇见了502BadGatewayNginx。问题解答502BadGateway, 并且由Nginx返回。而自己的应用中,并没有定义Nginx相关内容,所以需要查看问题是否出现在AzureSpringApp服务的设置上。根据SpringApp的通信模型图判断,502的请求是由N......
  • 基于SpringBoot的刷题小程序的设计与实现+附源码+数据库
    摘要:随着互联网技术的快速发展,在线教育平台逐渐成为学生学习和复习的重要工具。为了提高用户在学习过程中的效率和体验,本文提出并实现了一个基于SpringBoot的刷题小程序。该小程序旨在通过高效的题库管理、智能化的刷题功能以及友好的用户界面,帮助用户更好地进行知识点的巩......
  • Java项目:208Springboot + vue实现的校园服务平台(含论文+开题报告)
    作者主页:夜未央5788 简介:Java领域优质创作者、Java项目、学习资料、技术互助文末获取源码项目介绍基于Springboot+vue实现的汽车服务管理系统本系统包含管理员、接单员、普通用户三个角色。管理员角色:管理员管理、基础数据管理、接单详情管理、接单员管理、公告信......
  • Ubuntu22给boot加密码
    目录确保安装了必要的GRUB工具生成GRUB密码编辑GRUB配置文件更新GRUB配置确保安装了必要的GRUB工具rambo@test1:~$sudoaptupdaterambo@test1:~$sudoaptinstallgrub-common生成GRUB密码rambo@test1:~$grub-mkpasswd-pbkdf2输入密码:重新输入口令:您......
  • Spring学习笔记--1.IoC入门
    Spring学习笔记一、IoC入门1.什么是IoCIoC即控制反转,一个类不再主动控制创建自己所依赖的类,而是交给外部容器去控制创建自己所依赖的类。例如,有一个汽车厂,原本想要制作一辆汽车,需要自己制作发动机、轮胎、方向盘等零部件,汽车就是这个类,发动机和轮胎就是它的依赖项,这些依......
  • 基于springboot+vue.js+uniapp小程序的社区团购系统附带文章源码部署视频讲解等
    文章目录前言详细视频演示具体实现截图技术栈后端框架SpringBoot前端框架Vue持久层框架MyBaits系统测试系统测试目的系统功能测试系统测试结论为什么选择我代码参考数据库参考源码获取前言......
  • SpringBoot3.0.x适配mybatis版本
    SpringBoot适配mybatis版本最低为3.0.3<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>3.0.3</version><......