1. 程式人生 > >【死磕springboot2.0】springboot基於web開發

【死磕springboot2.0】springboot基於web開發

宣告,使用 maven3.5.4,springboot2.0,JDK8 ,idea2018.2

模組目錄結構:
在這裡第三方圖片描述

main 主方法:

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

model實體類:

/**
 * @auther SyntacticSugar
 * @data 2018/11/11 0011下午 10:03
 */
public class User { private String name; private int age; private String pass; // getter setter ..... }

controller層,定義了單個物件,list集合,以及傳參情況下的 get 、post請求;

package com.neo.springbootweb.web;
/**
 * @auther SyntacticSugar
 * @data 2018/11/11 0011下午 10:04
 */

//  bug一次,   @controller需要配合@requestbody
@RestController public class WebController { // 定義method 、 path @RequestMapping(name = "/getUser", method = RequestMethod.POST) public User getUser() { User user = new User(); user.setAge(12); user.setName("小明"); user.setPass("123456"); return user;
} // getUsers 獲取list 集合 @RequestMapping(value = "/getUsers",method = RequestMethod.POST) public List<User> getUsers() { ArrayList<User> users = new ArrayList<>(); User user1 = new User(); user1.setName("neo"); user1.setAge(30); user1.setPass("neo123"); users.add(user1); User user2 = new User(); user2.setName("小明"); user2.setAge(12); user2.setPass("123456"); users.add(user2); return users; } /** * 以下兩種方式都可以;@GetMapping("get/{name}") 簡化寫法 * @param name * @return */ // @RequestMapping(value = "get/{name}",method = RequestMethod.GET) @GetMapping("get/{name}") public String getname(@PathVariable("name") String name ){ return name; } }

test方法,前兩個單元測試 返回 json格式的字串,controller層第三個方法測試,在瀏覽器 url 訪問;http://localhost:8080/get/hello
返回hello說明成功:

package com.neo.springbootweb;

import com.neo.springbootweb.web.WebController;
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.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

@RunWith(SpringRunner.class)
@SpringBootTest
public class WebApplicationTests {

//MockMvcBuilders.standaloneSetup(...) 中的引數需要匯入 controller的例項
    private MockMvc mockMvc;
    @Before
    public void  setUp(){
        mockMvc= MockMvcBuilders.standaloneSetup(new WebController()).build();
    }
    /**
     * .andReturn().getResponse().getContentAsString()
     * 獲取請求的返回資訊
     * @throws Exception
     */
    @Test
    public void getUser() throws Exception {
        String contentAsString = mockMvc.perform(MockMvcRequestBuilders.post("/getUser")).andReturn().getResponse().getContentAsString();
        System.out.println(contentAsString);
    }
    @Test
    public void getUsers() throws Exception {
        String contentAsString = mockMvc.perform(MockMvcRequestBuilders.post("/getUsers")).andReturn().getResponse().getContentAsString();
        System.out.println(contentAsString);
    }
}