zoukankan      html  css  js  c++  java
  • Spring Boot 之 RESTfull API简单项目的快速搭建(一)

    1.创建项目

    2.创建 controller

    IndexController.java

    package com.roncoo.example.controller;
    
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;
    
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.roncoo.example.bean.User;
    
    @RestController
    @RequestMapping(value = "/index")
    public class IndexController {
    
    	@RequestMapping
    	public String index() {
    		return "hello world";
    	}
    
    	@RequestMapping(value = "get")
    	public Map<String, String> get(@RequestParam String name) {
    		Map<String, String> map = new HashMap<String, String>();
    		map.put("name", name);
    		map.put("value", "hello world");
    
    		return map;
    	}
    	
    	@RequestMapping(value = "find/{id}/{name}")
    	public User get(@PathVariable int id, @PathVariable String name) {
    		User u = new User();
    		u.setId(id);
    		u.setName(name);
    		u.setDate(new Date());
    		return u;
    	}
    }

    3.创建 bean -- 实体类 User

    User.java

    package com.roncoo.example.bean;
    
    import java.util.Date;
    
    public class User {
    	private int id;
    	private String name;
    	private Date date;
    
    	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 Date getDate() {
    		return date;
    	}
    
    	public void setDate(Date date) {
    		this.date = date;
    	}
    
    }

    4.测试

    SpringBootDemo21ApplicationTests.java

    package com.roncoo.example;
    
    import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
    
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    import org.springframework.test.web.servlet.MockMvc;
    import org.springframework.test.web.servlet.RequestBuilder;
    import org.springframework.test.web.servlet.setup.MockMvcBuilders;
    
    import com.roncoo.example.controller.IndexController;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class SpringBootDemo21ApplicationTests {
    	
    	private MockMvc mvc;
    	
    	@Before
    	public void befor() {
    		this.mvc = MockMvcBuilders.standaloneSetup(new IndexController()).build();
    	}
    
    	@Test
    	public void contextLoads() throws Exception {
    		RequestBuilder req = get("/index");
    		mvc.perform(req).andExpect(status().isOk()).andExpect(content().string("hello world"));
    	}
    
    }

    5.项目目录

  • 相关阅读:
    springmvc
    POJ 3683 Priest John's Busiest Day
    POJ 3678 Katu Puzzle
    HDU 1815 Building roads
    CDOJ UESTC 1220 The Battle of Guandu
    HDU 3715 Go Deeper
    HDU 3622 Bomb Game
    POJ 3207 Ikki's Story IV
    POJ 3648 Wedding
    HDU 1814 Peaceful Commission
  • 原文地址:https://www.cnblogs.com/crazycode2/p/10347043.html
Copyright © 2011-2022 走看看