【问题标题】:Link spring boot with react将弹簧靴与反应链接
【发布时间】:2017-09-25 05:22:29
【问题描述】:

我按照 this 教程使用 React 和 Spring Boot 创建 Web 应用程序。

我遵循了完全相同的步骤,但在我的 react 应用程序中我引入了 webpack。 React 应用程序已打开并正在运行。

pom.xml 文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>com.example</groupId>
<artifactId>ema</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>ema</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

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

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

<build>
<plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>


    <plugin>
    <groupId>com.github.eirslett</groupId>
    <artifactId>frontend-maven-plugin</artifactId>
    <version>1.2</version>
    </plugin>

    <plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.0.2</version>
    <executions>
    <execution>
    <id>Copy frontend production build to resources</id>
    <phase>package</phase>
    <goals>
    <goal>copy-resources</goal>
    </goals>
    <configuration>
    <outputDirectory>${basedir}/target/classes</outputDirectory>
    <resources>
    <resource>
    <directory>src/main/app/build/</directory>
    <filtering>true</filtering>
    </resource>
    </resources>
    </configuration>
    </execution>
    </executions>
    </plugin>


    </plugins>
</build>

我已完成教程的最后一步:

mvn clean package
java -jar target/cra-demo-0.0.1-SNAPSHOT.jar

但是我看到我的spring server 已经关闭了:

 :: Spring Boot ::        (v1.5.3.RELEASE)

2017-04-27 15:01:33.740  INFO 28295 --- [           main] com.example.EmaApplication               : Starting EmaApplication v0.0.1-SNAPSHOT on robucslvm03 with PID 28295 (/home/gecobici/EMA/target/ema-0.0.1-SNAPSHOT.jar started by gecobici in /home/gecobici/EMA)
2017-04-27 15:01:33.748  INFO 28295 --- [           main] com.example.EmaApplication               : No active profile set, falling back to default profiles: default
2017-04-27 15:01:33.860  INFO 28295 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@255316f2: startup date [Thu Apr 27 15:01:33 EEST 2017]; root of context hierarchy
2017-04-27 15:01:34.936  INFO 28295 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-04-27 15:01:34.958  INFO 28295 --- [           main] com.example.EmaApplication               : Started EmaApplication in 2.06 seconds (JVM running for 2.877)
2017-04-27 15:01:34.958  INFO 28295 --- [       Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@255316f2: startup date [Thu Apr 27 15:01:33 EEST 2017]; root of context hierarchy
2017-04-27 15:01:34.959  INFO 28295 --- [       Thread-2] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

我是 spring 和 react 的新手,我不知道如何让 spring 服务器启动并运行并与我的 react 应用程序交互。

编辑: 在 pom.xml 我有:

<directory>src/main/app/build/</directory>

我使用 webpack -p 创建了生产版本,但我没有看到任何构建文件夹。 如何在构建文件夹中生成生产版本?

【问题讨论】:

    标签: java spring maven reactjs pom.xml


    【解决方案1】:

    你介绍的 webpack 女巫一点都不错,你可以做些什么来简化你的 React 应用程序的使用 spring,设置 Webpack 代理来拦截特定端口中的所有 HTTP 请求,我就是这样做的:

    'webpack-dev-server': {
          options: {
            webpack: webpackConfig,
            inline: true,
          },
          start: {
            host: '0.0.0.0',
            port: '<%= config.webpackDevServerPort %>',
            contentBase: '<%= config.dist %>',
            webpack: {
              devtool: 'inline-source-map',
              debug: true,
            },
            proxy: {
              '/myapp': {
                target: 'http://localhost:' + (grunt.option('proxy-port') || config.connectPort),
                secure: false,
              },
            },
          },
        },
    

    在您的 Spring 应用程序中,您必须允许以下最后一件事:

      /**
       * Register WebSocket publisher
       *
       * @param registry where the handler will be registered
       */
      @Override
      public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(publisher, webSocketPath).setAllowedOrigins("*");
      }
    

    如果您不 setAllowedOrigins("*"); 来自 Spring 服务器外部的 rest 调用将不会被拦截,这仅适用于开发,并且必须在应用程序投入生产时禁用。

    【讨论】:

    • 如何使用 webpack 在 build 文件夹中生成生产版本?
    • 我在 webpack 配置中添加了代理设置和相同的行为
    • webpack 只会打包你的应用程序,然后你必须使用 maven 将前后打包到一个 jar 中才能执行
    猜你喜欢
    • 2017-12-22
    • 2018-02-14
    • 2019-05-03
    • 2021-04-20
    • 1970-01-01
    • 2023-01-07
    • 2016-02-16
    • 2019-06-24
    • 2020-03-16
    相关资源
    最近更新 更多