春季启动的Docker容器返回文件,而不是JSP视图。

huangapple go评论87阅读模式
英文:

Spring boot docker container returns a file instead of jsp view

问题

我有一个简单的Docker化Spring Boot应用程序。
当我在本地运行应用程序(不使用Docker)时,一切正常运行。控制器返回JSP视图。

但是,当我使用容器运行应用程序时,它返回给我一个包含HTML代码的文件。
我可以尝试强制控制器返回HTML响应而不是八位字节流,但这不是一个明智的解决方案。

我意识到问题可能出现在jasper、tomcat、docker之间的通信之间,但我无法找出问题所在,并尝试了许多解决方案。

如果有任何帮助,将不胜感激。

以下是我的配置:

**项目结构**
...

**application.properties**
...

**MvcConfiguration.java**
...

**pom.xml**
...

**IndexController.java**
...

我正在使用fabric8自动构建我的镜像。在构建阶段,CustomClass基于以下模板创建Dockerfile。

```plaintext
FROM openjdk
VOLUME /tmp
ADD maven/${fileName}.jar ${fileName}.jar
RUN sh -c 'touch /${fileName}.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/${fileName}.jar"]

CustomClass只是一个简单的文件读取/写入类,根据pom.xml中的变量动态创建Dockerfile。

CustomClass
...

我注意到日志中存在差异,不知道是否相关。

使用Docker时,应用程序正在重定向并响应文件。

当运行应用程序时,没有重定向。



<details>
<summary>英文:</summary>

I have a simple dockerized spring boot aplication. 
When I run app locally (no docker) everything runs ok. Controllors return jsp views.

but when i run the app using container it is returning me a file which contains html code.
I could try forcing the controller to return html response instead of octet-stream but its not a smart solution.

I realize the issue is somewhere between jasper, tomcat, docker communication but i cant find it out, and i tried a bunch of solution.

Any help is appreciated, 

here is my configuration

Thanks

**project structure** 

├── main/
│   ├── java/
│   │   └── com/
│   │   └── example/
│   │   └── jsp/
│   │   ├── configuration/
│   │   │   └── MvcConfiguration.java
│   │   ├── controller/
│   │   │   ├── IndexController.java
│   │   │   └── ProductController.java
│   │   ├── JspApplication.java
│   │   ├── model/
│   │   │   ├── Author.java
│   │   │   ├── ProductCategory.java
│   │   │   └── Product.java*
│   │   ├── service/
│   │   │   ├── ProductServiceImpl.java
│   │   │   └── ProductService.java*
│   │   └── test/
│   │   └── CustomClass.java
│   └── resources/
│   ├── application.properties
│   └── META-INF/
│   └── resources/
│   └── index.jsp
└── test/
└── java/
└── com/
└── example/
└── jsp/
└── JspApplicationTests.java


**application.properties**

spring.mvc.view.suffix=.jsp

logging.level.org.springframework=DEBUG
logging.level.com=DEBUG

server.port=8080
spring.rabbitmq.host=


**MvcConfiguration.java**

package com.example.jsp.configuration;

import com.example.rabbit.client.pageview.PageViewService;
import com.example.rabbit.client.pageview.PageViewServiceImpl;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MvcConfiguration implements WebMvcConfigurer {

@Bean
public PageViewService pageViewService(RabbitTemplate template) {
    return new PageViewServiceImpl(template);
}

}


**pom.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 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

&lt;groupId&gt;com.example&lt;/groupId&gt;
&lt;artifactId&gt;jsp&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;name&gt;jsp&lt;/name&gt;
&lt;description&gt;Demo project for Spring Boot&lt;/description&gt;

&lt;properties&gt;
	&lt;java.version&gt;1.8&lt;/java.version&gt;
	&lt;docker.image.prefix&gt;eps&lt;/docker.image.prefix&gt;
	&lt;docker.image.name&gt;pageview_controller&lt;/docker.image.name&gt;
&lt;/properties&gt;

&lt;dependencies&gt;

	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
	&lt;/dependency&gt;

	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
		&lt;version&gt;2.3.3.RELEASE&lt;/version&gt;
	&lt;/dependency&gt;

	&lt;dependency&gt;
		&lt;groupId&gt;javax.servlet&lt;/groupId&gt;
		&lt;artifactId&gt;jstl&lt;/artifactId&gt;
	&lt;/dependency&gt;

	&lt;dependency&gt;
		&lt;groupId&gt;org.apache.tomcat.embed&lt;/groupId&gt;
		&lt;artifactId&gt;tomcat-embed-jasper&lt;/artifactId&gt;
		&lt;scope&gt;required&lt;/scope&gt;
	&lt;/dependency&gt;

	&lt;dependency&gt;
		&lt;groupId&gt;com.example.rabbit&lt;/groupId&gt;
		&lt;artifactId&gt;client&lt;/artifactId&gt;
		&lt;version&gt;1.5-SNAPSHOT&lt;/version&gt;
	&lt;/dependency&gt;

&lt;/dependencies&gt;

&lt;build&gt;
	&lt;plugins&gt;
		&lt;plugin&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
		&lt;/plugin&gt;

		&lt;plugin&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
		&lt;/plugin&gt;
		&lt;plugin&gt;
			&lt;groupId&gt;io.fabric8&lt;/groupId&gt;
			&lt;artifactId&gt;docker-maven-plugin&lt;/artifactId&gt;
			&lt;version&gt;0.20.0&lt;/version&gt;

			&lt;configuration&gt;
				&lt;dockerHost&gt;unix:///var/run/docker.sock&lt;/dockerHost&gt;

				&lt;verbose&gt;true&lt;/verbose&gt;
				&lt;images&gt;
					&lt;image&gt;
						&lt;name&gt;${docker.image.prefix}/${docker.image.name}&lt;/name&gt;
						&lt;build&gt;
							&lt;dockerFileDir&gt;${project.basedir}/target/dockerfile&lt;/dockerFileDir&gt;

							&lt;!--copies artficact to docker build dir in target--&gt;
							&lt;assembly&gt;
								&lt;descriptorRef&gt;artifact&lt;/descriptorRef&gt;
							&lt;/assembly&gt;
							&lt;tags&gt;
								&lt;tag&gt;latest&lt;/tag&gt;
								&lt;tag&gt;${project.version}&lt;/tag&gt;
							&lt;/tags&gt;
						&lt;/build&gt;
						&lt;run&gt;
							&lt;ports&gt;
								&lt;port&gt;8080:8080&lt;/port&gt;
							&lt;/ports&gt;
						&lt;/run&gt;
					&lt;/image&gt;
				&lt;/images&gt;
			&lt;/configuration&gt;
		&lt;/plugin&gt;

		&lt;plugin&gt;
			&lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
			&lt;artifactId&gt;exec-maven-plugin&lt;/artifactId&gt;
			&lt;version&gt;1.5.0&lt;/version&gt;
			&lt;executions&gt;
				&lt;execution&gt;
					&lt;phase&gt;prepare-package&lt;/phase&gt;
					&lt;goals&gt;
						&lt;goal&gt;java&lt;/goal&gt;
					&lt;/goals&gt;
				&lt;/execution&gt;
			&lt;/executions&gt;
			&lt;configuration&gt;
				&lt;mainClass&gt;com.example.jsp.test.CustomClass&lt;/mainClass&gt;
				&lt;arguments&gt;${project.name},${project.version}&lt;/arguments&gt;
			&lt;/configuration&gt;
		&lt;/plugin&gt;

	&lt;/plugins&gt;
&lt;/build&gt;

</project>


**IndexController.java**

package com.example.jsp.controller;

import com.example.jsp.service.ProductService;
import com.example.rabbit.client.pageview.PageViewService;
import guru.springframework.model.events.PageViewEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.UUID;

@Controller
public class IndexController {

private static final Logger log = LoggerFactory.getLogger(IndexController.class);

private ProductService productService;
private PageViewService pageViewService;;

@Autowired
public IndexController(ProductService productService,
                        PageViewService pageViewService
) {

    this.productService = productService;
    this.pageViewService = pageViewService;

}

@GetMapping({&quot;/&quot;, &quot;index&quot;})
public ModelAndView getIndex(ModelAndView model){

    model.addObject(&quot;products&quot;, productService.listProducts());

    //Send Page view event
    PageViewEvent pageViewEvent = new PageViewEvent();
    pageViewEvent.setPageUrl(&quot;/&quot;);
    pageViewEvent.setPageViewDate(new Date());
    pageViewEvent.setCorrelationId(UUID.randomUUID().toString());

    log.info(&quot;Sending Message to pagie view service&quot;);
    pageViewService.sendPageViewEvent(pageViewEvent);

    model.setViewName(&quot;index&quot;);
    return model;
}

}


I am using fabric8 to automatically build my image. During building stage CustomClass is creating a Dockerfile based on the following template. 

FROM openjdk
VOLUME /tmp
ADD maven/${fileName}.jar ${fileName}.jar
RUN sh -c 'touch /${fileName}.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/${fileName}.jar"]

CustomClass is just a dumb file reader/writer class which creates dynamicly Dockerfile based on the variables from pomx.ml

**CustomClass**

public class CustomClass {
public static void main(String[] args) throws IOException {

    String projectName = args[0];
    String version = args[1];

    File file = new File(&quot;development/DockerfileTemplate&quot;);
    FileReader reader = new FileReader(file.getAbsoluteFile());

    StringBuilder sb = new StringBuilder();

    try (BufferedReader br = new BufferedReader(new FileReader(file))) {
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line+&quot;\n&quot;);
        }
    } catch (Exception e) {

    }

    String content = sb.toString().replaceAll(&quot;\$\\{fileName\\}&quot;, projectName+ &quot;-&quot; + version);

    File saveFile = new File(&quot;target/dockerfile/Dockerfile&quot;);
    saveFile.getParentFile().mkdirs();

    FileWriter fw = new FileWriter(saveFile);
    fw.write(content);
    fw.close();
}

}



I noticed in logs a difference, dont know if that is related

when using docker, app is redirecting and responding with a file

2020-09-20 04:47:10.392 DEBUG 1 --- [nio-8080-exec-2] o.s.web.servlet.view.JstlView : Forwarding to [index.jsp]
2020-09-20 04:47:10.392 DEBUG 1 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : "FORWARD" dispatch for GET "/index.jsp", parameters={}
2020-09-20 04:47:10.395 DEBUG 1 --- [nio-8080-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler ["classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "/"]
2020-09-20 04:47:10.396 DEBUG 1 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Exiting from "FORWARD" dispatch, status 304
2020-09-20 04:47:10.396 DEBUG 1 --- [nio-8080-exec-2] o.j.s.OpenEntityManagerInViewInterceptor : Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
2020-09-20 04:47:10.396 DEBUG 1 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Completed 304 NOT_MODIFIED


when running app locally, there are no redirects

2020-09-20 07:47:54.461 DEBUG 1883 --- [nio-8080-exec-2] o.s.web.servlet.view.JstlView : Forwarding to [index.jsp]
2020-09-20 07:47:54.461 DEBUG 1883 --- [nio-8080-exec-2] o.j.s.OpenEntityManagerInViewInterceptor : Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
2020-09-20 07:47:54.461 DEBUG 1883 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Completed 200 OK


</details>


# 答案1
**得分**: 1

以下是您要翻译的部分:

"So, serving JSP has some limitations when deployed in a docker. Here is some interesting info on that which helped me finding the solution https://stackoverflow.com/questions/56537151/why-does-spring-boot-not-support-jsp-while-it-can-render-the-page-if-we-add-prop

Here is the solution. Its is using apache for serving pages

**Dockerfile template**
```shell
From tomcat
RUN rm -rf /usr/local/tomcat/webapps/*
COPY maven/${fileName}.war /usr/local/tomcat/webapps/ROOT.war

project structure

├── main/
│   ├── java/
│   │   └── com/
│   │       └── example/
│   │           └── jsp/
│   │               ├── configuration/
│   │               │   └── BeanConfiguration.java
│   │               ├── controller/
│   │               │   └── IndexController.java
│   │               ├── converter/
│   │               │   ├── ProductFormToProduct.java*
│   │               │   └── ProductToProductForm.java*
│   │               ├── dto/
│   │               │   └── ProductForm.java
│   │               ├── JspApplication.java
│   │               ├── model/
│   │               │   └── Product.java
│   │               ├── repository/
│   │               │   └── ProductRepository.java
│   │               ├── service/
│   │               │   └── ProductService.java
│   │               ├── test/
│   │               │   └── CustomClass.java
│   │               └── utility/
│   │                   └── HtmlFormatter.java
│   ├── resources/
│   │   ├── application.properties
│   │   └── META-INF/
│   └── webapp/
│       ├── assets/
│       │   ├── css/
│       │   │   └── main.css
│       │   └── js/
│       │       └── script.js
│       └── WEB-INF/
│           └── view/
│               ├── fragments/
│               │   └── nav.jsp
│               └── product/
│                   ├── list.jsp
│                   ├── productform.jsp
│                   └── show.jsp
└── test/
    └── java/
        └── com/
            └── example/
                └── jsp/
                    └── JspApplicationTests.java

Main Spring boot application class

package com.example.jsp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class JspApplication extends SpringBootServletInitializer {

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


    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(JspApplication.class);
    }
}

pom.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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>jsp</artifactId>
    <version>0.0.2-SNAPSHOT</version>
    <name>jsp</name>
    <description>Demo project for Spring Boot</description>
    <packaging>war</packaging>

    <properties>
        <project.name>jsp</project.name>
        <java.version>1.8</java.version>
        <docker.image.prefix>test</docker.image.prefix>
        <docker.image.name>dockertest</docker.image.name>
    </properties>

    <repositories>
        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!--WEBJARS-->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>webjars-locator</artifactId>
            <version>0.40</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>4.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</

<details>
<summary>英文:</summary>

So, serving JSP has some limitations when deployed in a docker. Here is some interesting info on that which helped me finding the solution https://stackoverflow.com/questions/56537151/why-does-spring-boot-not-support-jsp-while-it-can-render-the-page-if-we-add-prop

Here is the solution. Its is using apache for serving pages

**Dockerfile template**

From tomcat
RUN rm -rf /usr/local/tomcat/webapps/*
COPY maven/${fileName}.war /usr/local/tomcat/webapps/ROOT.war


**project structure**

├── main/
│   ├── java/
│   │   └── com/
│   │   └── example/
│   │   └── jsp/
│   │   ├── configuration/
│   │   │   └── BeanConfiguration.java
│   │   ├── controller/
│   │   │   └── IndexController.java
│   │   ├── converter/
│   │   │   ├── ProductFormToProduct.java*
│   │   │   └── ProductToProductForm.java*
│   │   ├── dto/
│   │   │   └── ProductForm.java
│   │   ├── JspApplication.java
│   │   ├── model/
│   │   │   └── Product.java
│   │   ├── repository/
│   │   │   └── ProductRepository.java
│   │   ├── service/
│   │   │   └── ProductService.java
│   │   ├── test/
│   │   │   └── CustomClass.java
│   │   └── utility/
│   │   └── HtmlFormatter.java
│   ├── resources/
│   │   ├── application.properties
│   │   └── META-INF/
│   └── webapp/
│   ├── assets/
│   │   ├── css/
│   │   │   └── main.css
│   │   └── js/
│   │   └── script.js
│   └── WEB-INF/
│   └── view/
│   ├── fragments/
│   │   └── nav.jsp
│   └── product/
│   ├── list.jsp
│   ├── productform.jsp
│   └── show.jsp
└── test/
└── java/
└── com/
└── example/
└── jsp/
└── JspApplicationTests.java


**Main Spring boot application class**

package com.example.jsp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class JspApplication extends SpringBootServletInitializer {

public static void main(String[] args) {
SpringApplication.run(JspApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(JspApplication.class);
}

}


**pom.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 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>jsp</artifactId>
<version>0.0.2-SNAPSHOT</version>
<name>jsp</name>
<description>Demo project for Spring Boot</description>
<packaging>war</packaging>

&lt;properties&gt;
&lt;project.name&gt;jsp&lt;/project.name&gt;
&lt;java.version&gt;1.8&lt;/java.version&gt;
&lt;docker.image.prefix&gt;test&lt;/docker.image.prefix&gt;
&lt;docker.image.name&gt;dockertest&lt;/docker.image.name&gt;
&lt;/properties&gt;
&lt;repositories&gt;
&lt;repository&gt;
&lt;id&gt;jitpack.io&lt;/id&gt;
&lt;url&gt;https://jitpack.io&lt;/url&gt;
&lt;/repository&gt;
&lt;/repositories&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-validation&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;javax.servlet&lt;/groupId&gt;
&lt;artifactId&gt;jstl&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;javax.servlet&lt;/groupId&gt;
&lt;artifactId&gt;jstl&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat.embed&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-embed-jasper&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;javax.servlet&lt;/groupId&gt;
&lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;
&lt;version&gt;4.0.1&lt;/version&gt;
&lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.h2database&lt;/groupId&gt;
&lt;artifactId&gt;h2&lt;/artifactId&gt;
&lt;scope&gt;runtime&lt;/scope&gt;
&lt;/dependency&gt;
&lt;!--WEBJARS--&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.webjars&lt;/groupId&gt;
&lt;artifactId&gt;webjars-locator&lt;/artifactId&gt;
&lt;version&gt;0.40&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.webjars&lt;/groupId&gt;
&lt;artifactId&gt;bootstrap&lt;/artifactId&gt;
&lt;version&gt;4.5.2&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.webjars&lt;/groupId&gt;
&lt;artifactId&gt;jquery&lt;/artifactId&gt;
&lt;version&gt;3.5.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;exclusions&gt;
&lt;exclusion&gt;
&lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
&lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
&lt;/exclusion&gt;
&lt;/exclusions&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;build&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;com.spotify&lt;/groupId&gt;
&lt;artifactId&gt;dockerfile-maven-plugin&lt;/artifactId&gt;
&lt;version&gt;1.3.6&lt;/version&gt;
&lt;configuration&gt;
&lt;repository&gt;${docker.image.prefix}/${project.artifactId}&lt;/repository&gt;
&lt;buildArgs&gt;
&lt;WAR_FILE&gt;target/${project.build.finalName}.war&lt;/WAR_FILE&gt;
&lt;/buildArgs&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;plugin&gt;
&lt;groupId&gt;io.fabric8&lt;/groupId&gt;
&lt;artifactId&gt;docker-maven-plugin&lt;/artifactId&gt;
&lt;version&gt;0.20.0&lt;/version&gt;
&lt;configuration&gt;
&lt;!--&lt;dockerHost&gt;http://127.0.0.1:2375&lt;/dockerHost&gt;--&gt;
&lt;dockerHost&gt;unix:///var/run/docker.sock&lt;/dockerHost&gt;
&lt;verbose&gt;true&lt;/verbose&gt;
&lt;images&gt;
&lt;image&gt;
&lt;name&gt;${docker.image.prefix}/${docker.image.name}&lt;/name&gt;
&lt;build&gt;
&lt;dockerFileDir&gt;${project.basedir}/target/dockerfile&lt;/dockerFileDir&gt;
&lt;!--copies artficact to docker build dir in target--&gt;
&lt;assembly&gt;
&lt;descriptorRef&gt;artifact&lt;/descriptorRef&gt;
&lt;/assembly&gt;
&lt;tags&gt;
&lt;tag&gt;latest&lt;/tag&gt;
&lt;tag&gt;${project.version}&lt;/tag&gt;
&lt;/tags&gt;
&lt;/build&gt;
&lt;run&gt;
&lt;ports&gt;
&lt;port&gt;8080:8080&lt;/port&gt;
&lt;/ports&gt;
&lt;/run&gt;
&lt;/image&gt;
&lt;/images&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
&lt;artifactId&gt;exec-maven-plugin&lt;/artifactId&gt;
&lt;version&gt;1.5.0&lt;/version&gt;
&lt;executions&gt;
&lt;execution&gt;
&lt;phase&gt;prepare-package&lt;/phase&gt;
&lt;goals&gt;
&lt;goal&gt;java&lt;/goal&gt;
&lt;/goals&gt;
&lt;/execution&gt;
&lt;/executions&gt;
&lt;configuration&gt;
&lt;mainClass&gt;com.example.jsp.test.DockerFileBulder&lt;/mainClass&gt;
&lt;arguments&gt;${project.name},${project.version}&lt;/arguments&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;

</project>


**DockerFileBulder**
    String projectName = args[0];
String version = args[1];
File file = new File(&quot;development/DockerfileTemplate&quot;);
FileReader reader = new FileReader(file.getAbsoluteFile());
StringBuilder sb = new StringBuilder();
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
sb.append(line+&quot;\n&quot;);
}
} catch (Exception e) {
}
String content = sb.toString().replaceAll(&quot;\\$\\{fileName\\}&quot;, projectName+ &quot;-&quot; + version);
File saveFile = new File(&quot;target/dockerfile/Dockerfile&quot;);
saveFile.getParentFile().mkdirs();
FileWriter fw = new FileWriter(saveFile);
fw.write(content);
fw.close();

</details>
# 答案2
**得分**: 0
在 ***IndexController.java*** 中尝试以下代码:
```java
@GetMapping({"/", "index"})
public ModelAndView getIndex(ModelAndView model){
model.addObject("products", productService.listProducts());
// 发送页面查看事件
PageViewEvent pageViewEvent = new PageViewEvent();
pageViewEvent.setPageUrl("/");
pageViewEvent.setPageViewDate(new Date());
pageViewEvent.setCorrelationId(UUID.randomUUID().toString());
log.info("向页面查看服务发送消息");
pageViewService.sendPageViewEvent(pageViewEvent);
return new ModelAndView("index", model);
}
英文:

Try this in the IndexController.java

@GetMapping({&quot;/&quot;, &quot;index&quot;})
public ModelAndView getIndex(ModelAndView model){
model.addObject(&quot;products&quot;, productService.listProducts());
//Send Page view event
PageViewEvent pageViewEvent = new PageViewEvent();
pageViewEvent.setPageUrl(&quot;/&quot;);
pageViewEvent.setPageViewDate(new Date());
pageViewEvent.setCorrelationId(UUID.randomUUID().toString());
log.info(&quot;Sending Message to pagie view service&quot;);
pageViewService.sendPageViewEvent(pageViewEvent);
return new ModelAndView(&quot;index&quot;, model);
}

huangapple
  • 本文由 发表于 2020年9月20日 13:21:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63975774.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定