SpringBoot简单入门程序
2019-07-15 17:50
274 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_40903237/article/details/95992753
1、首先导入maven中的pom.xml
[code]<?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
[code]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; } }
[code]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
[code]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文件提到外面即可!
最开始:
改正后:
两种放的方式:
相关文章推荐
- Spring Boot + Security + Mybatis 简单权限控制(入门 + 记录篇)
- Spring Boot 编写入门程序
- spring boot+mybatis+mysql 简单入门
- 【SpringBoot】的快速入门程序
- 【spring boot】spring boot简单配置web小程序的步骤
- Spring学习之路-SpringBoot简单入门
- IDEA SpringBoot入门与速查—简单输出
- 利用SpringBoot简单快速搭建WEB项目入门
- SpringBoot入门——IDEA简单Demo
- SpringBoot入门——Thymeleaf简单使用
- Spring Boot入门程序(2)
- SpringBoot简单入门
- 【springboot 入门篇】第1篇 第一个spring-boot程序(多种搭建方式)
- springboot入门,简单能跑的项目快速搭建
- 最简单的 Spring Boot 入门指南
- SpringBoot简单入门
- Spring Boot 入门 (一)——建一个最简单的springboot项目
- spring boot 入门-使用spring boot构建简单应用
- Spring学习日志-简单springboot入门启动
- SpringBoot简单入门(一)