对于传统的Web项目,可能大部分都要部署到web容器中,如Tomcat。Spring Boot提供了一种超级简单的部署方式,就是直接将应用打成jar包,在生产上只需要执行java -jar就可以运行了。

一般情况下,如果我们的应用只是作为一个服务、工具类、API的形式存在,那么我们一般将其打包成jar包。而如果我们的应用是一个Web应用,都是打成war包,进行发布,同时如果我们的服务器是Tomcat等轻量级服务器,一般都打成war包进行发布;

我们在Spring Boot整合Mybatis实例一文中已经描述过如何整合Spring Boot和Mybatis,本文我们在上文的基础上来看一下如何将Spring Boot项目打成Jar包并部署运行。项目结构如下:


这里写图片描述

首先我们需要开发一个控制器来接受我们的Http请求并在浏览器上显示对应的消息。所以我们先定义一个服务接口BlogService,该接口只有一个方法,即获取数据列表:

public interface BlogService  {
    List<BlogType> queryBlogType();
}

我们再定义一个接口的实现类,名为BlogServiceIml,该类实现BlogService接口,具体如下:

@Service
public class BlogServiceIml implements BlogService {
    @Autowired
    private BlogTypeMapper blogTypeMapper;

    /**
     * 获取列表
     * @return
     */
    @Override
    public List<BlogType> queryBlogType() {
        return blogTypeMapper.getAll();
    }
}

注意,定义一个服务类时一定要加上@Service注解。

接下来我们定义一个控制器(Controller),该控制器接受一个请求并以Json的格式返回数据。

@RestController
@RequestMapping(value="/api/blog")
public class BlogController {
    @Autowired
    private BlogService blogService;

    @RequestMapping(value="/blogTypes")
    public List<BlogType> blogTypes(HttpServletRequest request, HttpServletResponse response){
        return blogService.queryBlogType();
    }
}

准备工作就绪,接下来我们开始打包。
第一步:我们需要在pom.xml添加spring-boot-maven-plugin

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

第二步,保存pom.xml,进入pom.xml所在的目录,运行mvn clean package -Dmaven.test.skip=true命令打包。

F:\MyProject\IntelliJ\mybatisxml>mvn clean package -Dmaven.test.skip=true
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building mybatisxml 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ mybatisxml ---
[INFO] Deleting F:\MyProject\IntelliJ\mybatisxml\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mybatisxml ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ mybatisxml ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 6 source files to F:\MyProject\IntelliJ\mybatisxml\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mybatisxml ---
[INFO] Not copying test resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ mybatisxml ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ mybatisxml ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ mybatisxml ---
[INFO] Building jar: F:\MyProject\IntelliJ\mybatisxml\target\mybatisxml-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:1.5.9.RELEASE:repackage (default) @ mybatisxml ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.768 s
[INFO] Finished at: 2018-04-12T12:09:31+08:00
[INFO] Final Memory: 33M/220M
[INFO] ------------------------------------------------------------------------

第三步,打包完成,进入Target目录,使用java -jar命令,运行应用

F:\MyProject\IntelliJ\mybatisxml>cd target

F:\MyProject\IntelliJ\mybatisxml\target>java -jar mybatisxml-0.0.1-SNAPSHOT.jar
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.9.RELEASE)

2018-04-12 12:11:59.946  INFO 8220 --- [           main] c.l.mybatisxml.MybatisxmlApplication     : Starting MybatisxmlApplication v0.0.1-SNAPSHOT on user-PC with PID 8220 (F:\MyProject\IntelliJ\mybatisxml\target\mybatisxml-0.0.1-SNAPSHOT.jar started by user in F:\MyProject\IntelliJ\mybatisxml\target)
2018-04-12 12:11:59.956  INFO 8220 --- [           main] c.l.mybatisxml.MybatisxmlApplication     : No active profile set, falling back to default profiles: default
2018-04-12 12:12:00.596  INFO 8220 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@69663380: startup date [Thu Apr 12 12:12:00 CST 2018]; root of context hierarchy
2018-04-12 12:12:03.192  INFO 8220 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2018-04-12 12:12:03.222  INFO 8220 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-04-12 12:12:03.222  INFO 8220 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.23
2018-04-12 12:12:03.372  INFO 8220 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-04-12 12:12:03.372  INFO 8220 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2776 ms
2018-04-12 12:12:03.642  INFO 8220 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2018-04-12 12:12:03.652  INFO 8220 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-04-12 12:12:03.652  INFO 8220 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-04-12 12:12:03.652  INFO 8220 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-04-12 12:12:03.652  INFO 8220 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-04-12 12:12:04.860  INFO 8220 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@69663380: startup date [Thu Apr 12 12:12:00 CST 2018]; root of context hierarchy
2018-04-12 12:12:05.000  INFO 8220 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/api/blog/blogTypes]}" onto public java.util.List<com.learningspringboot.mybatisxml.entity.BlogType> com.learningspringboot.mybatisxml.ronRestApi.BlogController.blogTypes(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-04-12 12:12:05.000  INFO 8220 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-04-12 12:12:05.010  INFO 8220 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-04-12 12:12:05.090  INFO 8220 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-04-12 12:12:05.097  INFO 8220 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-04-12 12:12:05.165  INFO 8220 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-04-12 12:12:05.225  WARN 8220 --- [           main] .t.AbstractTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
2018-04-12 12:12:06.233  INFO 8220 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-04-12 12:12:06.369  INFO 8220 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2018-04-12 12:12:06.382  INFO 8220 --- [           main] c.l.mybatisxml.MybatisxmlApplication     : Started MybatisxmlApplication in 7.126 seconds (JVM running for 7.786)

程序启动成功之后,在浏览器输入http://localhost:8080/api/blog/blogTypes,如果得到如下结果则表示打包启动成功。


这里写图片描述

本文源码见: https://download.csdn.net/download/zyhlwzy/10343462

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐