1、创建maven项目
2、修改pom.xml文件
<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>afei.springboot</groupId>
<artifactId>SpringBoot_Self</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.19.RELEASE</version>
<relativePath></relativePath>
</parent>
<dependencies>
<!-- 导入web模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
3、service包
接口EmployeeService
package afei.springboot.service;
import afei.springboot.entities.Employee;
public interface EmployeeService {
/**
* 根据员工id查询员工
* @return
*/
Employee getEmployee();
}
子包impl,实现类EmployeeServiceImpl
package afei.springboot.service.impl;
import org.springframework.stereotype.Service;
import afei.springboot.entities.Department;
import afei.springboot.entities.Employee;
import afei.springboot.service.EmployeeService;
@Service
public class EmployeeServiceImpl implements EmployeeService {
@Override
public Employee getEmployee() {
return new Employee("afei", 2, new Department("dev",2));
}
}
4、handler包
EmployeeHandler
package afei.springboot.handler;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
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;
import org.springframework.web.bind.annotation.RestController;
import afei.springboot.entities.Department;
import afei.springboot.entities.Employee;
import afei.springboot.service.EmployeeService;
/*
* @RestController=@Controller+@ResponseBody
*/
@RestController
public class EmployeeHandler {
@Autowired
private EmployeeService employeeService;
/*
* 1、@RestController=@Controller+@ResponseBody
* 2、由于加了注解@ResponseBody,所以服务器会以字符串的方式直接返回对象
* 但是SpringBoot集成了jackson模块,会将字符串以json串的形式返回
*/
@RequestMapping("/getEmp")
public Object getEmployee() {
//create object
Employee emp = new Employee("afei",1,new Department("dev",1));
//create a map,put the object into it
Map<String,Object> map = new HashMap<>();
map.put("emp", emp);
return map;
}
@RequestMapping("/getEmp1")
public Object getEmployee1() {
//get a Employee object by Autowired
Employee emp = employeeService.getEmployee();
//create a map,put the object into it
Map<String,Object> map = new HashMap<>();
map.put("emp", emp);
return map;
}
}
5、springboot/SpringBootSelfApplication.java
package afei.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
/*
* @ComponentScan
* 设置自动扫描的包,如果不设置默认扫描当前类所在的包及其子包
*/
@ComponentScan(basePackages="afei.springboot")
@SpringBootApplication
public class SpringBootSelfApplication {
/*
* resources/application.properties里面的server.context-path=/
* 即在浏览器端发送请求的时候不用再加项目名称,直接发送请求名称即可
* eg:http://localhost:8080/getEmp
*/
public static void main(String[] args) {
SpringApplication.run(SpringBootSelfApplication.class, args);
}
}
6、resources/application.properties
server.context-path=/
server.port=8080
server.session.timeout=60
server.tomcat.max-threads=800
server.tomcat.uri-encoding=UTF-8