1、配置项目pom.xml打包方式为war包,默认为jar包
2、设置主程序
3、配置tomcat启动
4、启动类继承 SpringBootServletInitializer,重写configure方法
1、Application.java
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
5、注意项目访问需要把静态页面放到tomcat web app 下的root中,并且项目访问需要加上项目名称(webapp下的文件名称)
6、修改自己代码中前台访问后台的页面地址 加上项目名端口等
注意打包时,不执行测试代码
org.apache.maven.plugins maven-dependency-plugin 3.1.2 org.apache.maven.plugins maven-surefire-plugin 2.22.2 true
附完整代码
1、Application.java
package com.dr.archive.common;
import com.dr.archive.common.config.ArchiveConfig;
import com.dr.framework.core.orm.support.mybatis.spring.boot.autoconfigure.EnableAutoMapper;
import com.dr.framework.util.Constants;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.client.RestTemplate;
import java.nio.charset.StandardCharsets;
/**
* 系统启动入口
*
* @author dr
*/
@SpringBootApplication(scanBasePackages = {Constants.PACKAGE_NAME, "com.dr.archive"}, scanBasePackageClasses = Application.class)
@EnableConfigurationProperties(ArchiveConfig.class)
@EnableAutoMapper(basePackages = {Constants.PACKAGE_NAME, "com.dr.archive"})
@EnableScheduling
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
System.out.println("成功");
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
/**
* httpClient
*
* @return
*/
@Bean
public RestTemplate restTemplate() {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setReadTimeout(10000);
// 设置超时
requestFactory.setConnectTimeout(10000);
//利用复杂构造器可以实现超时设置,内部实际实现为 HttpClient
RestTemplate restTemplate = new RestTemplate(requestFactory);
for (HttpMessageConverter messageConverter : restTemplate.getMessageConverters()) {
if (messageConverter instanceof StringHttpMessageConverter) {
((StringHttpMessageConverter) messageConverter).setDefaultCharset(StandardCharsets.UTF_8);
}
}
return restTemplate;
}
}
2、pom.xml(项目的)
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3、http.js(自己项目用的,别人没用)
import axios from 'axios'
import NProgress from "nprogress";
import 'nprogress/nprogress.css';
import qs from 'qs'
import util from '../components/login/util'
import {Message} from "element-ui";
/**
* 默认配置项
*/
const defaultCfg = {
baseURL: 'archive_commons-1.0-SNAPSHOT/api', timeout: 5000,
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest(data) {
NProgress.start()
return qs.stringify(data)
},
transformResponse(data) {
NProgress.done()
try {
data = JSON.parse(data)
} catch (e) {
data = {success: false, message: '数据格式不正确'}
}
return data
}
}
let httpInstance;
/**
* 初始化默认http客户端
* @param base
* @returns {AxiosInstance}
*/
export function initHttp(cfg = defaultCfg) {
httpInstance = axios.create(cfg)
return httpInstance;
}
/**
* 获取默认的http客户端
* @returns {*}
*/
export function http() {
if (!httpInstance) {
initHttp()
}
return httpInstance;
}
/**
* 这里会被扫描到,用来初始化http请求
* @param vue
* @param router
*/
export default (vue, router, store) => {
const instance = http()
//拦截所有的请求,添加token
instance.interceptors.request.use(config => {
const header = config.headers
header['$token'] = header['dauth'] = util.getToken()
return config;
}, err => {
return Promise.reject(err);
})
instance.interceptors.response.use(
response => {
if (response.data.message === 'needLogin') {
router.replace({path: 'login', query: {redirect: router.currentRoute.fullPath}})
} else {
return response
}
},
err => {
//routeLogin(options)
let message = `服务器错误【${err}】`
if (err && err.message.startsWith('timeout')) {
message = `网络请求超时,请稍候重试!`
}
Message.error(message)
return Promise.resolve({data: {success: false, message}})
})
store.$http = vue.prototype.$http = instance
store.$post = vue.prototype.$post = instance.post
store.$get = vue.prototype.$get = instance.get
store.$router = router
//拦截跳转,添加加载进度
router.beforeEach((route, redirect, next) => {
NProgress.start()
next()
})
router.afterEach(() => {
NProgress.done()
})
}