2017-04-27 117 views
1

我跟着this教程創建一個Web應用程序使用反應和彈簧引導。鏈接彈簧引導與反應

我遵循完全相同的步驟,但在我的反應應用程序中,我介紹了webpack。反應應用程序正在運行。

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 

但是我看到我的春天服務器closig:

:: 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.spring[email protected]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.spring[email protected]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 

我新的春天和反應,我不知道怎麼去春服務器啓動和運行,並與我的反應的應用進行互動。

編輯: 在pom.xml我有:

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

創建生產建設與-p的WebPack,但我看不出有任何build文件夾。 如何在生成文件夾中生成生產版本?

回答

0

你引入了webpack witch並不壞,你可以通過spring來簡化你的React應用程序的用法,就是設置Webpack Proxy來攔截特定端口中的所有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("*");其餘來自外部的春天服務器將無法被攔截來電,這只是dev,並且在應用程序g時必須禁用oes到生產。

+0

如何使用webpack在生成文件夾中生成生成版本? –

+0

我在webpack config中添加了代理設置並且行爲相同 –

+0

webpack只會禁用您的應用程序,然後您必須使用maven將正面和背面封裝在一個單獨的jar中才能執行 –