增加了简单Restful的demo

This commit is contained in:
Administrator
2020-10-28 22:45:05 +08:00
parent 54527c6d3d
commit 03b37a2732
5 changed files with 134 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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<String> hello() {
return BaseResponse.success("Hello Spring Rest");
}
@RequestMapping("/echo/{message}")
public BaseResponse<String> echo(@PathVariable String message) {
return BaseResponse.success(message);
}
@RequestMapping("/error")
public String error() {
throw new RuntimeException("统一异常处理");
}
}

View File

@ -0,0 +1,2 @@
server:
port: 8082

View File

@ -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")));
}
}