springboot创建
idea
创建项目时选择 Spring Initiali ,如图 所示。
填写项目信息
选择项目的包
使用官网Spring Initializr
链接:https://start.spring.io/
选择合适版本和需要的jar包创建
idea中创建maven项目
将src,resources和pom.xml文件复制到新建的项目当中。
maven-reimport
启动项目测试
springboot分包和application.properties配置(tomcat)
配置resources下面的application.properties文件
application.properties
#tomcat start
server.port=8081
server.error.path=/error
server.servlet.session.timeout=1
server.servlet.context-path=/chapter02
server.tomcat.uri-encoding=utf-8
server.tomcat.max-threads=500
server.tomcat.basedir=/home/sang/tmp
#tomcat end
server.ssl.key-store=sang.p12
server.ssl.key-alias=tomcathttps
server.ssl.key-store-password=123456
#server.ssl.key-store-type=PKCS12
创建tomcatConfig文件配置完成后,在浏览器中输入http://localhost:8081/chapter02/hello,就会自动重定向到
https://localhost:8081/chapter02/hello 上。
TomcatConfig.java
package org.sang.config;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Created by sang on 2018/7/4.
*/
@Configuration
public class TomcatConfig {
@Bean
TomcatServletWebServerFactory tomcatServletWebServerFactory() {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(){
@Override
protected void postProcessContext(Context context) {
SecurityConstraint constraint = new SecurityConstraint();
constraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
constraint.addCollection(collection);
context.addConstraint(constraint);
}
};
factory.addAdditionalTomcatConnectors(createTomcatConnector());
return factory;
}
private Connector createTomcatConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(8080);
connector.setSecure(false);
connector.setRedirectPort(8081);
return connector;
}
}
springboot各组件详细配置(application.properties)
https://docs.spring.io/spring-boot/docs/
选择对应版本的
进入
https://docs.spring.io/spring-boot/docs/2.1.5.RELEASE/reference/html/
搜索
Appendices
点击 Common application properties
完整链接
https://docs.spring.io/spring-boot/docs/2.1.5.RELEASE/reference/html/common-application-properties.html