增加了简单Restful的demo
This commit is contained in:
34
examples/restful-example/pom.xml
Normal file
34
examples/restful-example/pom.xml
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.chinaunicom.ebtp</groupId>
|
||||||
|
<artifactId>mall-ebtp-cloud-parent</artifactId>
|
||||||
|
<version>0.0.1</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<groupId>com.chinaunicom.mall.ebtp.cloud</groupId>
|
||||||
|
<artifactId>restful-example</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>restful-example</name>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- 普通rest工程只需要导入 mvc 配置包 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.chinaunicom.ebtp</groupId>
|
||||||
|
<artifactId>mall-ebtp-cloud-mvc-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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("统一异常处理");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,2 @@
|
|||||||
|
server:
|
||||||
|
port: 8082
|
@ -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")));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user