SpringBoot入门demo(一)

先上代码

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配置文件

	4.0.0
	bootTest
	bootTest
	0.0.1-SNAPSHOT
	jar
	springBoot入门
	http://maven.apache.org
	
		UTF-8
	
	
	
		
			org.springframework.boot
			spring-boot-starter-web
			1.2.5.RELEASE
		
		
		
			junit
			junit
			3.8.1
			test
		
	
	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	
启动项目 看到这样的表示成功

SpringBoot入门demo(一)_第1张图片

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代码



 
   new document 
  
  
  
  
  
 

 
  
  


相应结果

{

username"hellow",
password"1234567890"
}

由于我们Application用上了@RestController注解所以我们不用在方法上写@ResponseBody



你可能感兴趣的:(springBoot)