1. 程式人生 > >入門:Springboot官方demo及開發get介面

入門:Springboot官方demo及開發get介面

首先是下載demo,地址:https://start.spring.io/,在Search for dependencies輸入:web下載

然後配置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>com.xykj</groupId>
	<artifactId>springdemo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

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

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.4.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</artifactId>
		</dependency>

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

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

		<!-- Thymeleaf模板引擎,渲染Html頁面 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>

		<!-- 新增 tomcat 的支援 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<!-- <scope>provided</scope> -->
		</dependency>

		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
			<!-- <scope>provided</scope> -->
		</dependency>

		
	

		

		
		<!--Json庫的依賴 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.1.43</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
	<!-- <repositories> <repository> <id>spring-milestone</id> <url>https://repo.spring.io/libs-release</url> 
		</repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-milestone</id> 
		<url>https://repo.spring.io/libs-release</url> </pluginRepository> </pluginRepositories> -->

</project>

首次進入:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@SpringBootApplication
@Controller
@EnableAutoConfiguration
@ComponentScan("com.course")
public class DemoApplication {

	@RequestMapping("/")
	@ResponseBody
	public String greeting() {
		return "Hello World!";
	}

	@RequestMapping("/home")
	@ResponseBody
	public String getHome() {
		return "Hello Home World!";
	}

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}

注意配置maven,不會的請出門右拐。學習一下.JDK使用的是1.8

啟動這個專案,正常的main方法啟動。

開發一個返回cookies資訊的get介面,首先建立一個Application類,這個類是一個入口類,還要在resources檔案下,新建一個配置必須寫這樣的名字,才會被springboot識別到application.properties,埠號配置規定是這麼寫的,server.port=${port:8899},springboot會自動載入這個配置。

package com.course.server;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

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

/** 這個註解是告訴application類的,我是需要被掃描的類 */
@RestController
public class MyGetMethod {

	@RequestMapping(value = "/getCookies", method = RequestMethod.GET)
	public String getCookies(HttpServletResponse response) {
		/**
		 * HttpServletRequest 裝請求資訊的類* HttpServletResponse 裝響應資訊的類
		 */
		Cookie cookie = new Cookie("login", "true");
		response.addCookie(cookie);
		return "恭喜你獲得了cookies資訊成功";
	}

}