pom.xml
4.0.0 com.example demo 0.0.1-SNAPSHOT war demo Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.5.8.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat provided org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-devtools true org.springframework.boot spring-boot-maven-plugin true
项目文件目录
模拟控制层代码
package com.example.controller;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import com.example.pojo.Student;/** * spring boot demo * @author XSWL pengfei.xiong * @date 2017年11月17日 */@RestController //相当于@Controller和@RequestBody 返回json@EnableAutoConfigurationpublic class HelloSpringBoot { @RequestMapping("/hello") public String showHello(){ return "Hello SpringBoot!"; } @RequestMapping("/pojo") public Student showStudent(){ Student student = new Student(1, "show", 20); return student; } @RequestMapping("/map") public MapshowMap(){ Map map = new HashMap (); map.put("username", "张无忌"); map.put("age", 15); return map; } @RequestMapping("/list") public List showList(){ List list = new ArrayList (); Student student01 = new Student(1, "show", 20); Student student02 = new Student(1, "show", 20); list.add(student01); list.add(student02); return list; }}
如果对于上面的注解有疑问就看下这个博客:
实体类 就不用说了啊
package com.example.pojo;import java.io.Serializable;public class Student implements Serializable{ private int id; private String name; private int age; public Student() { super(); } public Student(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}
最后是程序的入口
package com.example.demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import com.example.controller.HelloSpringBoot;@SpringBootApplicationpublic class DemoApplication{ // 项目入口 public static void main(String[] args) { SpringApplication.run(HelloSpringBoot.class, args); //运行之后在浏览器中访问:http://localhost:11086/hello 默认端口8080,我这是修改后的路径 }}
补充下:修改端口在application.properties文件中加一句话: server.port=11086
demo源码下载 整合mybatis三层文章 :