英文:
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">
<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.1-SNAPSHOT</version>
<name>jsp</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<docker.image.prefix>eps</docker.image.prefix>
<docker.image.name>pageview_controller</docker.image.name>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>2.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>required</scope>
</dependency>
<dependency>
<groupId>com.example.rabbit</groupId>
<artifactId>client</artifactId>
<version>1.5-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.20.0</version>
<configuration>
<dockerHost>unix:///var/run/docker.sock</dockerHost>
<verbose>true</verbose>
<images>
<image>
<name>${docker.image.prefix}/${docker.image.name}</name>
<build>
<dockerFileDir>${project.basedir}/target/dockerfile</dockerFileDir>
<!--copies artficact to docker build dir in target-->
<assembly>
<descriptorRef>artifact</descriptorRef>
</assembly>
<tags>
<tag>latest</tag>
<tag>${project.version}</tag>
</tags>
</build>
<run>
<ports>
<port>8080:8080</port>
</ports>
</run>
</image>
</images>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.5.0</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.example.jsp.test.CustomClass</mainClass>
<arguments>${project.name},${project.version}</arguments>
</configuration>
</plugin>
</plugins>
</build>
</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({"/", "index"})
public ModelAndView getIndex(ModelAndView model){
model.addObject("products", productService.listProducts());
//Send Page view event
PageViewEvent pageViewEvent = new PageViewEvent();
pageViewEvent.setPageUrl("/");
pageViewEvent.setPageViewDate(new Date());
pageViewEvent.setCorrelationId(UUID.randomUUID().toString());
log.info("Sending Message to pagie view service");
pageViewService.sendPageViewEvent(pageViewEvent);
model.setViewName("index");
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("development/DockerfileTemplate");
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+"\n");
}
} catch (Exception e) {
}
String content = sb.toString().replaceAll("\$\\{fileName\\}", projectName+ "-" + version);
File saveFile = new File("target/dockerfile/Dockerfile");
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">
<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</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<repository>${docker.image.prefix}/${project.artifactId}</repository>
<buildArgs>
<WAR_FILE>target/${project.build.finalName}.war</WAR_FILE>
</buildArgs>
</configuration>
</plugin>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.20.0</version>
<configuration>
<!--<dockerHost>http://127.0.0.1:2375</dockerHost>-->
<dockerHost>unix:///var/run/docker.sock</dockerHost>
<verbose>true</verbose>
<images>
<image>
<name>${docker.image.prefix}/${docker.image.name}</name>
<build>
<dockerFileDir>${project.basedir}/target/dockerfile</dockerFileDir>
<!--copies artficact to docker build dir in target-->
<assembly>
<descriptorRef>artifact</descriptorRef>
</assembly>
<tags>
<tag>latest</tag>
<tag>${project.version}</tag>
</tags>
</build>
<run>
<ports>
<port>8080:8080</port>
</ports>
</run>
</image>
</images>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.5.0</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.example.jsp.test.DockerFileBulder</mainClass>
<arguments>${project.name},${project.version}</arguments>
</configuration>
</plugin>
</plugins>
</build>
</project>
**DockerFileBulder**
String projectName = args[0];
String version = args[1];
File file = new File("development/DockerfileTemplate");
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+"\n");
}
} catch (Exception e) {
}
String content = sb.toString().replaceAll("\\$\\{fileName\\}", projectName+ "-" + version);
File saveFile = new File("target/dockerfile/Dockerfile");
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({"/", "index"})
public ModelAndView getIndex(ModelAndView model){
model.addObject("products", productService.listProducts());
//Send Page view event
PageViewEvent pageViewEvent = new PageViewEvent();
pageViewEvent.setPageUrl("/");
pageViewEvent.setPageViewDate(new Date());
pageViewEvent.setCorrelationId(UUID.randomUUID().toString());
log.info("Sending Message to pagie view service");
pageViewService.sendPageViewEvent(pageViewEvent);
return new ModelAndView("index", model);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论