为什么Spring Boot默认将Web应用程序打包为.jar?.war有什么优势吗?
不应该将所有Web应用程序打包为.war以便部署到真正的Web服务器?
我认为.jar文件用于桌面应用程序,而.war文件用于Web应用程序.因为.jar不包含这里提到的Web应用程序组件:
我正在使用 Apache POI 生成.xlsx文件。
我想从 Spring 控制器返回该文件。这是我到目前为止所做的:
控制器:
@RequestMapping(method = RequestMethod.GET)
public HttpEntity<byte[]> createExcelWithTaskConfigurations(HttpServletResponse response) throws IOException {
byte[] excelContent = excelService.createExcel();
HttpHeaders header = new HttpHeaders();
header.setContentType(new MediaType("application", "vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
header.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=my_file.xls");
header.setContentLength(excelContent.length);
return new HttpEntity<>(excelContent, header);
}
Run Code Online (Sandbox Code Playgroud)
是否可以从 rest 控制器返回实际的 excel 文件,以便用户可以将其下载到他的计算机上?至于现在控制器返回 byte[] 但我想返回它的实际文件。我怎样才能做到这一点?
我开始学习设计模式.我知道原型用于制作我已经拥有的对象的精确副本,而Flyweight用于制作类似的对象.
我编写了像Mario(Java)中的2D平台游戏.有很多敌人是相同的,唯一的区别是他们的位置[x,y].还有从巨大的矩形建造的墙壁,唯一的区别是它们的位置[x,y].
在这种特殊情况下使用这些设计模式是否明智?我应该使用prototype来通过cloneable克隆对象然后设置[x,y]吗?
使用flyweight是否更好 - 当我需要新对象时,我只是从我的hashmap返回它们然后设置[x,y]?
在这两种情况下,我都避免使用new运算符,但我不确定使用哪种运算符.
我正在使用Spring 4.x并且我有以下RestController方法,它应该返回所有航班的列表
@RequestMapping(produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE}, method = RequestMethod.GET)
public FlightWrapper returnAllFlights() {
List<FlightDto> flights = data.findAll();
return new FlightWrapper(flights);
}
Run Code Online (Sandbox Code Playgroud)
FlightWrapper类看起来像这样(rootElement = flight s element = flight):
@XmlRootElement(name = "flights")
public class FlightWrapper {
private List<FlightDto> flights;
public FlightWrapper() {}
public FlightWrapper(List<FlightDto> flights) {
this.flights = flights;
}
@XmlElement(name = "flight")
public List<FlightDto> getFlights() {
return flights;
}
public void setFlights(List<FlightDto> flights) {
this.flights = flights;
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,当我调用returnAllFlights()时,它将以这种格式返回xml:
<FlightWrapper>
<flights>
<flights>
....
</flights>
<flights>
....
</flights>
</flights>
</FlightWrapper> …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 cytoscape 集成到角度应用程序中。
首先我下载的是:
npm i --save ngx-cytoscape cytoscape @types/cytoscape
Run Code Online (Sandbox Code Playgroud)
然后我将 Cytoscape 库导入到模块中
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ItemComponent } from './item/item.component';
import { CytoscapeModule } from 'ngx-cytoscape';
@NgModule({
imports: [
CommonModule,
CytoscapeModule
],
declarations: [
ItemComponent
]
})
export class DashboardModule { }
Run Code Online (Sandbox Code Playgroud)
并更新了 Angular.json
"scripts": [
"node_modules/jquery/dist/jquery.min.js",
"node_modules/bootstrap/dist/js/bootstrap.min.js",
"node_modules/cytoscape/dist/cytoscape.js"
]
Run Code Online (Sandbox Code Playgroud)
从现在开始我可以使用 cytoscape
这是我的 html
<div id="cy" class='row justify-content-center cytograph mt-2' *ngIf="graphData">
<ngx-cytoscape [elements]="graphData"></ngx-cytoscape>
</div>
Run Code Online (Sandbox Code Playgroud)
这是我的打字稿文件
@Component({
selector: 'app-item',
templateUrl: …Run Code Online (Sandbox Code Playgroud) 在docker中创建了一个网络
docker network create mysql-network
Run Code Online (Sandbox Code Playgroud)
然后我创建mysql镜像
docker container run -d -p 3306:3306 --net=mysql-network --name mysql-hibernate -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=test -v hibernate:/var/lib/mysql mysql
Run Code Online (Sandbox Code Playgroud)
这是我的应用程序属性
spring.jpa.hibernate.ddl-auto=create
useSSL=false
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL57Dialect
Run Code Online (Sandbox Code Playgroud)
我也尝试过
spring.datasource.url=jdbc:mysql://mysql-hibernate:3306/test
Run Code Online (Sandbox Code Playgroud)
但是启动时总是会报错
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:未知数据库“测试”
它怎么可能不知道数据库“test”?我在 docker 中指定了名称,如下所示
-e MYSQL_DATABASE=test
我缺少什么?
spring ×3
java ×2
spring-boot ×2
angular ×1
cytoscape.js ×1
docker ×1
excel ×1
oop ×1
performance ×1
typescript ×1