diff --git a/examples/restful-example/pom.xml b/examples/restful-example/pom.xml new file mode 100644 index 0000000..229166f --- /dev/null +++ b/examples/restful-example/pom.xml @@ -0,0 +1,34 @@ + + 4.0.0 + + com.chinaunicom.ebtp + mall-ebtp-cloud-parent + 0.0.1 + + + com.chinaunicom.mall.ebtp.cloud + restful-example + 0.0.1-SNAPSHOT + jar + + restful-example + + + + + com.chinaunicom.ebtp + mall-ebtp-cloud-mvc-starter + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/examples/restful-example/src/main/java/com/chinaunicom/mall/ebtp/cloud/restful/example/RestfulApplication.java b/examples/restful-example/src/main/java/com/chinaunicom/mall/ebtp/cloud/restful/example/RestfulApplication.java new file mode 100644 index 0000000..e572024 --- /dev/null +++ b/examples/restful-example/src/main/java/com/chinaunicom/mall/ebtp/cloud/restful/example/RestfulApplication.java @@ -0,0 +1,17 @@ +package com.chinaunicom.mall.ebtp.cloud.restful.example; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Import; + +import com.chinaunicom.mall.ebtp.cloud.mvc.starter.advice.BusinessExceptionHandlerAdvice; + +@SpringBootApplication +@Import(BusinessExceptionHandlerAdvice.class) +public class RestfulApplication { + + public static void main(String[] args) { + SpringApplication.run(RestfulApplication.class, args); + } + +} diff --git a/examples/restful-example/src/main/java/com/chinaunicom/mall/ebtp/cloud/restful/example/controller/SimpleController.java b/examples/restful-example/src/main/java/com/chinaunicom/mall/ebtp/cloud/restful/example/controller/SimpleController.java new file mode 100644 index 0000000..0e584aa --- /dev/null +++ b/examples/restful-example/src/main/java/com/chinaunicom/mall/ebtp/cloud/restful/example/controller/SimpleController.java @@ -0,0 +1,29 @@ +package com.chinaunicom.mall.ebtp.cloud.restful.example.controller; + +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.chinaunicom.mall.ebtp.cloud.mvc.starter.base.BaseResponse; + +@RestController +@RequestMapping(value = "/simple", method = RequestMethod.POST) +public class SimpleController { + + @RequestMapping("/hello") + public BaseResponse hello() { + return BaseResponse.success("Hello Spring Rest"); + } + + @RequestMapping("/echo/{message}") + public BaseResponse echo(@PathVariable String message) { + return BaseResponse.success(message); + } + + @RequestMapping("/error") + public String error() { + throw new RuntimeException("统一异常处理"); + } + +} diff --git a/examples/restful-example/src/main/resource/application.yml b/examples/restful-example/src/main/resource/application.yml new file mode 100644 index 0000000..0884131 --- /dev/null +++ b/examples/restful-example/src/main/resource/application.yml @@ -0,0 +1,2 @@ +server: + port: 8082 \ No newline at end of file diff --git a/examples/restful-example/src/test/java/com/chinaunicom/mall/ebtp/cloud/example/SimpleTest.java b/examples/restful-example/src/test/java/com/chinaunicom/mall/ebtp/cloud/example/SimpleTest.java new file mode 100644 index 0000000..490ea28 --- /dev/null +++ b/examples/restful-example/src/test/java/com/chinaunicom/mall/ebtp/cloud/example/SimpleTest.java @@ -0,0 +1,52 @@ +package com.chinaunicom.mall.ebtp.cloud.example; + +import java.nio.charset.Charset; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +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.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import com.chinaunicom.mall.ebtp.cloud.restful.example.RestfulApplication; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = RestfulApplication.class) +public class SimpleTest { + + private MockMvc mockMvc; + + private @Autowired WebApplicationContext wac; + + @Before + public void setupMockMvc() { + mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); + } + + @Test + public void testHello() throws Exception { + MvcResult result = mockMvc.perform(MockMvcRequestBuilders.post("/simple/hello")) + .andExpect(MockMvcResultMatchers.status().isOk()).andReturn(); + System.out.println(result.getResponse().getContentAsString(Charset.forName("UTF-8"))); + } + + @Test + public void testEcho() throws Exception { + MvcResult result = mockMvc.perform(MockMvcRequestBuilders.post("/simple/echo/test_echo_api")).andReturn(); + System.out.println(result.getResponse().getContentAsString(Charset.forName("UTF-8"))); + } + + @Test + public void testError() throws Exception { + MvcResult result = mockMvc.perform(MockMvcRequestBuilders.post("/simple/error")).andReturn(); + System.out.println(result.getResponse().getContentAsString(Charset.forName("UTF-8"))); + } + +}