Spring Boot 快速配置

Spring Boot應用的快速構建

本文使用maven方式快速構建Spring Boot應用html

幾分鐘教程

這裏, 我藉助IDEA快速創建一個maven項目 (你也能夠經過手動或者eclipse建立)java

1.1 New Project

1.1.1 建立一個新項目, 使用maven的方式

Spring Boot 快速配置

1.1.2 填寫一些應用相關的信息, GroupId, ArtifactId

Spring Boot 快速配置

1.1.3 填寫項目保存的本地路徑, 點擊Finish

Spring Boot 快速配置

1.1.4 如上操做, 建立完成一個空的Maven項目

Spring Boot 快速配置

1.2 引入Spring Boot的依賴

1.2.1 編輯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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>info.chiwm</groupId>
    <artifactId>log4j2</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--  映入spring boot 依賴 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

1.2.2 編寫項目啓動的主入口代碼

./src/java/main 目錄新建目錄, 例如 info/chiwm/boot . 全部的代碼都在該目錄下編輯, 這裏定義爲 代碼根目錄web

建立java文件 Application.java 做爲入口文件, 編碼以下spring

package info.chiwm.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author chiwm@kuyumall.com
 * @ClassName: Application
 * @Description:
 * @date 2018/1/3 上午11:29
 */
@SpringBootApplication(scanBasePackages = "info.chiwm.boot")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

能夠直接Application.java中編寫controller業務邏輯而且啓動.apache

1.2.3 編寫 controller 層業務邏輯

但這裏我將controller模塊的邏輯編排在相對應的目錄下, 能夠使示例代碼組織得更加明瞭json

在 `代碼根目錄` 下建立目錄 `controller`, 在該目錄下建立示例controller , `JsonController.java`
package info.chiwm.boot.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author wanminchi@gmail.com
 * @ClassName: JsonController
 * @Description:
 * @date 2018/1/3 下午1:11
 */

@RestController
@RequestMapping("/json")
public class JsonController {

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ResponseBody
    public String getJson(HttpServletRequest req, HttpServletResponse res) {
        return "{\"name\":\"chi\"}";
    }
}

1.3 啓動項目

在IDEA中, RUN Application.java便可啓動.bash

.   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.9.RELEASE)

2018-01-03 13:35:23.092  INFO 78070 --- [           main] info.chiwm.boot.Application              : Starting Application on chiwanmindeMacBook-Pro.local with PID 78070 (/Users/chiwanmin/code/java/boot/target/classes started by chiwanmin in /Users/chiwanmin/code/java/boot)
2018-01-03 13:35:23.096  INFO 78070 --- [           main] info.chiwm.boot.Application              : No active profile set, falling back to default profiles: default
2018-01-03 13:35:23.192  INFO 78070 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@74a10858: startup date [Wed Jan 03 13:35:23 CST 2018]; root of context hierarchy
2018-01-03 13:35:25.083  INFO 78070 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2018-01-03 13:35:25.092  INFO 78070 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-01-03 13:35:25.093  INFO 78070 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.23
2018-01-03 13:35:25.155  INFO 78070 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-01-03 13:35:25.155  INFO 78070 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1968 ms
2018-01-03 13:35:25.258  INFO 78070 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2018-01-03 13:35:25.261  INFO 78070 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-01-03 13:35:25.261  INFO 78070 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-01-03 13:35:25.261  INFO 78070 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-01-03 13:35:25.261  INFO 78070 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-01-03 13:35:25.645  INFO 78070 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@74a10858: startup date [Wed Jan 03 13:35:23 CST 2018]; root of context hierarchy
2018-01-03 13:35:25.712  INFO 78070 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/json/get],methods=[GET]}" onto public java.lang.String info.chiwm.boot.controller.JsonController.getJson(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-01-03 13:35:25.715  INFO 78070 --- [           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-01-03 13:35:25.715  INFO 78070 --- [           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-01-03 13:35:25.740  INFO 78070 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-01-03 13:35:25.741  INFO 78070 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-01-03 13:35:25.776  INFO 78070 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-01-03 13:35:25.912  INFO 78070 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-01-03 13:35:25.993  INFO 78070 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2018-01-03 13:35:25.999  INFO 78070 --- [           main] info.chiwm.boot.Application              : Started Application in 3.522 seconds (JVM running for 4.575)
2018-01-03 13:55:15.625  INFO 78070 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-01-03 13:55:15.626  INFO 78070 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2018-01-03 13:55:15.660  INFO 78070 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 34 ms

服務端啓動, Maven方式打包, 命令啓動也可.. app

相關文章
相關標籤/搜索