入门:Springboot官方demo及开发get接口

源码:https://github.com/KempBible/spring-boot-code/tree/master/simpledemo,欢迎点我

首先是下载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方法启动。

直接启动,然后访问http://localhost:8080/ 就可以显示出 Hello World!

在浏览器中输入:http://localhost:8080/home


开发一个返回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信息成功";
	}

}

在浏览器中输入:http://localhost:8080/getCookies

猜你喜欢

转载自blog.csdn.net/rocling/article/details/82807151
今日推荐