您的位置:首页 > 编程语言 > Java开发

SpringBoot入门demo(一)

2017-04-28 17:13 615 查看
先上代码

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.sky.entity.SkyUserEntity;

/**
*
* @author HuangXinyu
*
* @Date 2017年4月26日 上午11:57:56
*
* @version v1.0
*
*/
@SpringBootApplication
@RestController
@RequestMapping("/app")
public class Application {

@RequestMapping("/")
public String index(){
return "Index Page";
}

@RequestMapping("/hello")
public String hello(){
return "Hello Word!";
}

@RequestMapping("/users/{username}")
public String userProfile(@PathVariable("username")String username){
return String.format("username %s", username);
}

@RequestMapping("/posts/{id}")
public String post(@PathVariable("id") int id){
return String.format("post %s", id);
}

@RequestMapping(value = "/login",method=RequestMethod.GET)
public String loginGet(){
return "Login Page";
}

@RequestMapping(value = "/login",method=RequestMethod.POST)
public SkyUserEntity loginPost(String username,String password){
SkyUserEntity po = new SkyUserEntity();
po.setUsername(username);
po.setPassword(password);
return po;
}

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

import java.io.Serializable;

/**
*
* @author HuangXinyu
* @Date 2017年4月28日 下午3:10:24
*
* @version v1.0
*
*/
@SuppressWarnings("serial")
public class SkyUserEntity implements Serializable{

private String username;

private String password;

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;
}

}


maven配置文件
<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>bootTest</groupId>
<artifactId>bootTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springBoot入门</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
启动项目 看到这样的表示成功


http://localhost:8080/app/
可以看到 Index Page
http://localhost:8080/app/hello
Hello Word!
http://localhost:8080/app/users/123232
username 123232
http://localhost:8080/app/posts/123232
post 123232

自己试试 post请求
http://localhost:8080/app/login
Login Page

上post请求的html代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<meta name="generator" content="editplus" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
</head>

<body>

<form enctype="multipart/form-data" action="http://localhost:8080/app/login" method="post">
<table>
<tr>
<td><label for="txtname">账号:</label></td>
<td><input type="text" id="txtname" name="username" /></td>
</tr>
<tr>
<td><label for="txtpswd">密码:</label></td>
<td><input type="password" id="txtpswd" name="password" /></td>
</tr>
<tr>
<td colspan=2>
<input type="reset" />
<input type="submit" />
</td>
</tr>
</table>
</form>
</body>
</html>


相应结果

{

username: "hellow",

password: "1234567890"

}

由于我们Application用上了@RestController注解所以我们不用在方法上写@ResponseBody
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: