使用IntelliJ IDEA 開發工具搭建spring-boot項目筆記(一)

   (一)  寫在前面

  •        McroService微服務可以說是當前互聯網公司最火的技術了,筆者最近也開始一邊學習spring-boot和spring-cloud技術,一邊在家搭建Spring-boot項目。這裏我選擇用intelliJ IDEA開發工具是有一定原因的,筆者最近兩年在公司開發使用的java開發工具一直是eclipse,前端用的是visual studio。但是我發現用eclipse搭建的spring-boot項目,不配置應用上下文application-context.xml啓動不來,而且沒有日誌,博客上參考了不少解決方案還是沒有解決。而要在spring-boot項目根目錄下運行maven指令: mvn spring-boot:run 才能運行起來。看了很多搭建spring-boot項目採用的都是intelliJ IDEA, 於是也轉而採用intelliJ IDEA,沒想到很順利就把自己的spring-boot的demo項目啓動起來了。廢話不再多說,下面貼上自己的項目搭建步驟截圖和代碼

  (二)搭建和啓動步驟並開發簡單的Restful風格接口

    (1) 首先,我們訪問:http://start.spring.io/,選擇構建工具 Maven,採用編程語言 Java,輸入如圖中的信息,我使用的spring-boot是2.0.5  版本,在Search for dependencies下的搜索輸入框中輸入Web並作爲選中的依賴, 點擊下面的Generate Project按鈕下載項目壓縮包

(2)解壓項目後使用intelliJ IDEA開發工具導入到工作目錄,導入過程截圖如下:選擇File-->New-->Project from Existing Source 從解壓後的項目中導入spring-boot-demo項目,選擇Maven Project file後點擊OK按鈕即可

(3)啓動類DemoApplication.java與pom.xml爲spring-io生成項目時自動生成,代碼如下

ackage com.example.demo;

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

@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}
<?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>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>demo</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.5.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

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

(4)新建一個TestController類,並寫兩簡單的restful風格的接口,代碼如下

package com.example.demo.com.example.demo.controller;

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

@RestController
@RequestMapping(value="/restful")
public class TestController {
    @RequestMapping(value="/test",method= RequestMethod.GET)
    public String testRestInterface(@RequestParam("name") String name){
        return "Hello:" + name;
    }

    @RequestMapping(value="/index",produces = "text/plain;charset=utf-8")
    public String index(){
        return "Hello Spring Boot!";
    }
}

(5) 點擊Run菜單下的Debug 'DemoApplication' 啓動項目

項目啓動成功的日誌信息如下,下面那個圖案是屬於spring-boot項目啓動時特有的

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

2018-09-22 00:54:30.747  INFO 10808 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication on heshengfu1211 with PID 10808 (D:\myproject\demo\target\classes started by HP in D:\myproject\demo)
2018-09-22 00:54:30.750  INFO 10808 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2018-09-22 00:54:30.803  INFO 10808 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@45ac5f9b: startup date [Sat Sep 22 00:54:30 GMT+08:00 2018]; root of context hierarchy
2018-09-22 00:54:32.036  INFO 10808 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-09-22 00:54:32.067  INFO 10808 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-09-22 00:54:32.068  INFO 10808 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.34
2018-09-22 00:54:32.074  INFO 10808 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : Loaded APR based Apache Tomcat Native library [1.2.17] using APR version [1.6.3].
2018-09-22 00:54:32.074  INFO 10808 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
2018-09-22 00:54:32.074  INFO 10808 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
2018-09-22 00:54:33.138  INFO 10808 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : OpenSSL successfully initialized [OpenSSL 1.0.2o  27 Mar 2018]
2018-09-22 00:54:33.271  INFO 10808 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-09-22 00:54:33.271  INFO 10808 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2472 ms
2018-09-22 00:54:33.356  INFO 10808 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-09-22 00:54:33.364  INFO 10808 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-09-22 00:54:33.364  INFO 10808 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-09-22 00:54:33.364  INFO 10808 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-09-22 00:54:33.364  INFO 10808 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-09-22 00:54:33.545  INFO 10808 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-22 00:54:33.808  INFO 10808 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@45ac5f9b: startup date [Sat Sep 22 00:54:30 GMT+08:00 2018]; root of context hierarchy
2018-09-22 00:54:33.873  INFO 10808 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/restful/index],produces=[text/plain;charset=utf-8]}" onto public java.lang.String com.example.demo.com.example.demo.controller.TestController.index()
2018-09-22 00:54:33.876  INFO 10808 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/restful/test],methods=[GET]}" onto public java.lang.String com.example.demo.com.example.demo.controller.TestController.testRestInterface(java.lang.String)
2018-09-22 00:54:33.879  INFO 10808 --- [           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.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-09-22 00:54:33.879  INFO 10808 --- [           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.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-09-22 00:54:33.909  INFO 10808 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-22 00:54:33.909  INFO 10808 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-09-22 00:54:34.043  INFO 10808 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-09-22 00:54:34.087  INFO 10808 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-09-22 00:54:34.090  INFO 10808 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 3.786 seconds (JVM running for 4.879)
(6)postman調用GET請求的接口,請求頭Headers設置Content-Type:application/json,調用過程截圖如下

響應狀態Status=200, Body中輸出了 Hello: Spring-boot表示接口調用成功

(7) chrom瀏覽器中輸入http://localhost:8080/restful/index 在瀏覽器端調用另一個返回text/plain格式用於瀏覽器端顯示的接口,截圖如下,一樣調用成功

本人後續還將繼續更新自己的spring-boot demo項目,下一篇博客講開始採用JPA數據源連接mysql數據庫,實現通過調用GET和POST請求接口實現數據集庫數據的查詢、插入、更新和刪除。本文參考鏈接如下:

黃朝兵的達人課

輕輕鬆鬆學習SpringBoot2

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章