1、首先导入maven中的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.yuanfeng</groupId>
<artifactId>springboot</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<!-- springboot的项目,必须从这个项目继承 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<dependencies>
<!-- 添加springboot的web支持 springboot1.5.2 内部默认使用的spring版本是 4.3.7 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--JSP解析 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- 内嵌的tomcat插件 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
2、创建一个controller和启动springboot应用的main
package com.yuanfeng.controller;/**
* Created by yuanfeng on 2019/7/15 16:59
*/
import com.yuanfeng.bean.User;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
*@ClassName HelloWorldController
*@Description T0D0
*@Author yuanfeng
*@Date 2019/7/15 16:59
*@Version 1.0
**/
@Controller
@Scope("prototype")
@RequestMapping("/hi")
public class HelloWorldController {
@RequestMapping("/hello")
@ResponseBody
public User hello(){
System.out.println("我是控制器...");
User user = new User();
user.setAge(111);
user.setPassword("不分手的恋爱");
user.setUsername("汪苏泷");
return user;
}
}
package com.yuanfeng.controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
*@ClassName Main
*@Description T0D0
*@Author yuanfeng
*@Date 2019/7/15 17:05
*@Version 1.0
**/
@SpringBootApplication
public class Main{
public static void main(String[] args) {
SpringApplication.run(Main.class,args);
}
}
3、封装一个实体类User
package com.yuanfeng.bean; public class User { private String username; private String password; private Integer age; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "User [username=" + username + ", password=" + password + ", age=" + age + "]"; } }
4、访问结果
敲个小黑板:在Main文件和controller文件不在同一个包的时候会出现访问失败的问题,这个时候其实只是需要把Main文件提到外面即可!
最开始:
改正后:
两种放的方式: