重构springboot-restful工程

This commit is contained in:
yidao620 2018-03-04 16:40:52 +08:00
parent cdc08a0c66
commit 223ecf2c81
7 changed files with 238 additions and 100 deletions

View File

@ -40,6 +40,17 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId> <artifactId>spring-boot-starter-jetty</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -1,40 +0,0 @@
package com.xncoding.pos.controller;
import com.xncoding.pos.model.LoginParam;
import com.xncoding.pos.model.UnbindParam;
import com.xncoding.pos.model.BaseResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
/**
* 登录接口类
*/
@RestController
public class LoginController {
private static final Logger _logger = LoggerFactory.getLogger(LoginController.class);
@PostMapping("/login")
public BaseResponse<String> login(@RequestHeader(name = "Content-Type", defaultValue = "application/json") String contentType,
@RequestBody LoginParam loginParam) {
_logger.info("用户请求登录获取Token");
String username = loginParam.getUsername();
String password = loginParam.getPassword();
return new BaseResponse<>(true, "Login success", username + password);
}
@PostMapping("/unbind")
public BaseResponse<String> unbind(@RequestHeader(name = "Content-Type", defaultValue = "application/json") String contentType,
@RequestHeader(name = "Authorization", defaultValue = "token") String token,
@RequestBody UnbindParam unbindParam) {
_logger.info("解绑通知接口start");
String imei = unbindParam.getImei();
String location = unbindParam.getLocation();
return new BaseResponse<>(true, "解绑通知发送成功", "unbind");
}
}

View File

@ -1,7 +1,7 @@
package com.xncoding.pos.model; package com.xncoding.pos.model;
/** /**
* Controller的基础返回类 * 基础返回类
* *
* @author XiongNeng * @author XiongNeng
* @version 1.0 * @version 1.0

View File

@ -1,59 +0,0 @@
package com.xncoding.pos.model;
/**
* 登录认证接口参数
*
* @author XiongNeng
* @version 1.0
* @since 2018/1/9
*/
public class LoginParam {
/**
* 用户名
*/
private String username;
/**
* 密码
*/
private String password;
/**
* Application ID
*/
private String appid;
/**
* IMEI码
*/
private String imei;
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 String getAppid() {
return appid;
}
public void setAppid(String appid) {
this.appid = appid;
}
public String getImei() {
return imei;
}
public void setImei(String imei) {
this.imei = imei;
}
}

View File

@ -0,0 +1,47 @@
package com.xncoding.pos.model;
/**
* 用户
*
* @author XiongNeng
* @version 1.0
* @since 2018/3/4
*/
public class User {
private Long id;
private String name;
private Integer age;
public User() {
}
public User(Long id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}

View File

@ -0,0 +1,45 @@
package com.xncoding.pos.util;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
/**
* JacksonUtil
*
* @author XiongNeng
* @version 1.0
* @since 2018/3/4
*/
public class JacksonUtil {
private static ObjectMapper mapper = new ObjectMapper();
public static String bean2Json(Object obj) {
try {
return mapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
e.printStackTrace();
return null;
}
}
// public static <T> T json2Bean(String jsonStr, Class<T> objClass) {
// try {
// return mapper.readValue(jsonStr, objClass);
// } catch (IOException e) {
// e.printStackTrace();
// return null;
// }
// }
public static <T> T json2Bean(String jsonStr, TypeReference<T> typeReference) {
try {
return mapper.readValue(jsonStr, typeReference);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}

View File

@ -0,0 +1,134 @@
package com.xncoding.pos;
import com.fasterxml.jackson.core.type.TypeReference;
import com.xncoding.pos.model.BaseResponse;
import com.xncoding.pos.model.User;
import com.xncoding.pos.util.JacksonUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
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.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.hamcrest.Matchers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.hamcrest.MatcherAssert.assertThat;
/**
* ApplicationTests
*
* @author XiongNeng
* @version 1.0
* @since 2018/3/4
*/
@AutoConfigureMockMvc
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ApplicationTests {
@Autowired
private MockMvc mvc;
@Test
public void testUserController() throws Exception {
// 测试UserController
RequestBuilder request;
// 1get查一下user列表应该为空
request = get("/users/");
MvcResult result = mvc.perform(request)
.andExpect(status().isOk())
.andReturn();
String content = result.getResponse().getContentAsString();
BaseResponse<List<User>> response = JacksonUtil.json2Bean(content, new TypeReference<BaseResponse<List<User>>>() {});
assertThat(response.isSuccess(), is(true));
assertThat(response.getMsg(), is("查询列表成功"));
assertThat(((List) response.getData()).size(), is(0));
// 2post提交一个user
request = post("/users/")
.param("id", "1")
.param("name", "测试大师")
.param("age", "20");
result = mvc.perform(request)
.andExpect(status().isOk())
.andReturn();
content = result.getResponse().getContentAsString();
BaseResponse<String> response1 = JacksonUtil.json2Bean(content, new TypeReference<BaseResponse<String>>() {});
assertThat(response1.isSuccess(), is(true));
assertThat(response1.getMsg(), is("新增成功"));
// 3get获取user列表应该有刚才插入的数据
request = get("/users/");
result = mvc.perform(request)
.andExpect(status().isOk())
.andReturn();
content = result.getResponse().getContentAsString();
BaseResponse<List<User>> response2 = JacksonUtil.json2Bean(content, new TypeReference<BaseResponse<List<User>>>() {});
assertThat(response2.isSuccess(), is(true));
assertThat(response2.getMsg(), is("查询列表成功"));
assertThat((response2.getData()).size(), is(1));
// 4put修改id为1的user
request = put("/users/1")
.param("name", "测试终极大师")
.param("age", "30");
result = mvc.perform(request)
.andExpect(status().isOk())
.andReturn();
content = result.getResponse().getContentAsString();
BaseResponse<String> response3 = JacksonUtil.json2Bean(content, new TypeReference<BaseResponse<String>>() {});
assertThat(response3.isSuccess(), is(true));
assertThat(response3.getMsg(), is("更新成功"));
// 5get一个id为1的user
request = get("/users/1");
result = mvc.perform(request)
.andExpect(status().isOk())
.andReturn();
content = result.getResponse().getContentAsString();
BaseResponse<User> response4 = JacksonUtil.json2Bean(content, new TypeReference<BaseResponse<User>>() {});
assertThat(response4.isSuccess(), is(true));
assertThat(response4.getMsg(), is("查询成功"));
User user = response4.getData();
assertThat(user.getId(), is(1L));
assertThat(user.getName(), is("测试终极大师"));
// 6del删除id为1的user
request = delete("/users/1");
result = mvc.perform(request)
.andExpect(status().isOk())
.andReturn();
content = result.getResponse().getContentAsString();
BaseResponse<String> response5 = JacksonUtil.json2Bean(content, new TypeReference<BaseResponse<String>>() {});
assertThat(response5.isSuccess(), is(true));
assertThat(response5.getMsg(), is("删除成功"));
// 7get查一下user列表应该为空
request = get("/users/");
result = mvc.perform(request)
.andExpect(status().isOk())
.andReturn();
content = result.getResponse().getContentAsString();
BaseResponse<List<User>> response6 = JacksonUtil.json2Bean(content, new TypeReference<BaseResponse<List<User>>>() {});
assertThat(response6.isSuccess(), is(true));
assertThat(response6.getMsg(), is("查询列表成功"));
assertThat((response6.getData()).size(), is(0));
}
}