diff --git a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-springmvc/pom.xml b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-springmvc/pom.xml index 02afc0554d..dda55bb2a3 100644 --- a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-springmvc/pom.xml +++ b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-springmvc/pom.xml @@ -70,6 +70,19 @@ spring-boot-starter-test test + + + + org.json + json + 20231013 + + + javax.ws.rs + javax.ws.rs-api + test + + @@ -78,6 +91,19 @@ org.springframework.boot spring-boot-maven-plugin + + org.apache.maven.plugins + maven-compiler-plugin + + + -proc:none + + + true + 17 + 17 + + diff --git a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-springmvc/src/main/java/org/apache/dubbo/rest/demo/DemoService.java b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-springmvc/src/main/java/org/apache/dubbo/rest/demo/DemoService.java index ee849e9c2c..9c6ee7f62a 100644 --- a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-springmvc/src/main/java/org/apache/dubbo/rest/demo/DemoService.java +++ b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-springmvc/src/main/java/org/apache/dubbo/rest/demo/DemoService.java @@ -16,15 +16,98 @@ */ package org.apache.dubbo.rest.demo; +import org.apache.dubbo.rest.demo.pojo.User; +import org.springframework.http.MediaType; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PatchMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; +import java.util.HashMap; +import java.util.List; @RequestMapping("/demo") public interface DemoService { - @RequestMapping(method = RequestMethod.GET, value = "/hello") - String sayHello(@RequestParam("name") String name); + @GetMapping(value = "get/param") + public String getParam(@RequestParam(value = "id")String id); + + @GetMapping(value = "get/variable/{id}") + public String getVariable(@PathVariable(value = "id")String id); + + @GetMapping(value = "get/muchParam") + public String getMuchParam(@RequestParam(value = "id")String id, @RequestParam(value = "name")String name); + + @GetMapping(value = "get/muchVariable/{id}/{name}") + public String getMuchVariable(@PathVariable(value = "id")String id, @PathVariable(value = "name")String name); + + @GetMapping(value = "get/reg/{name:[a-z-]+}-{version:\\d\\.\\d\\.\\d}{ext:\\.[a-z]+}") + public String getReg(@PathVariable(value = "name")String name, @PathVariable(value = "version")String version, @PathVariable(value = "ext")String ext); + + @PostMapping(value = "post/body") + public String postBody(@RequestBody String name); + + @PostMapping(value = "post/param") + public String postParam(@RequestParam(value = "id")String id); + + @PostMapping(value = "post/variable/{id}") + public String postVariable(@PathVariable(value = "id")String id); + + @PostMapping(value = "post/useConsumes", consumes = "application/json") + public String postUseConsumes(@RequestBody String id); + + @RequestMapping(value = "/error", method = RequestMethod.GET, consumes = MediaType.TEXT_PLAIN_VALUE) + String error(); + + @PostMapping(value = "post/useParams", params = "myParam=myValue") + public String postUseParams(@RequestBody String id); + + @GetMapping(value = "get/head/{id}", headers = "myHeader=myValue") + public String getHead(@PathVariable(value = "id")String id); + + @PostMapping(value = "post/useConsumes/formData", consumes = "application/x-www-form-urlencoded") + // 使用MultiValueMap 会显示反序列化失败 ? + // 但是使用hashmap又不支持 因为声明为 application/x-www-form-urlencoded + public String postUseConsumesFormData(@RequestBody MultiValueMap formData); + + // content type为"application/x-www-form-urlencoded"时body的类型必须是MultiValueMap类型 + @PostMapping(value = "post/useConsumes/user", consumes = "application/x-www-form-urlencoded", produces = MediaType.TEXT_PLAIN_VALUE) + String postUseConsumesUser(@RequestBody MultiValueMap> formData); + + // HashMap -> JSON + @PostMapping(value = "post/map/user", consumes = "application/json") + public String postMapUser(@RequestBody String formData); + + @PutMapping("/put/update/{id}") + public String putUpdateId(@PathVariable String id); + + @DeleteMapping("/delete/{id}") + public String deleteId(@PathVariable(value = "id")String id); + + @PatchMapping("patch/{id}") + public String patchById(@PathVariable String id, @RequestBody String patchData); + + @RequestMapping(method = RequestMethod.GET, value = "/primitive") + int primitiveInt(@RequestParam("a") int a, @RequestParam("b") int b); + + @RequestMapping(method = RequestMethod.GET, value = "/primitiveLong") + long primitiveLong(@RequestParam("a") long a, @RequestParam("b") Long b); + + @RequestMapping(method = RequestMethod.GET, value = "/primitiveByte") + long primitiveByte(@RequestParam("a") byte a, @RequestParam("b") Long b); + + @RequestMapping(method = RequestMethod.POST, value = "/primitiveShort") + long primitiveShort(@RequestParam("a") short a, @RequestParam("b") Long b, @RequestBody int c); + + @PostMapping(value = "/post/list", consumes = MediaType.ALL_VALUE) + public List postList(@RequestBody List users); + - // add more methods } diff --git a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-springmvc/src/main/java/org/apache/dubbo/rest/demo/consumer/Task.java b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-springmvc/src/main/java/org/apache/dubbo/rest/demo/consumer/Task.java index c2635fd671..1f7df8af41 100644 --- a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-springmvc/src/main/java/org/apache/dubbo/rest/demo/consumer/Task.java +++ b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-springmvc/src/main/java/org/apache/dubbo/rest/demo/consumer/Task.java @@ -30,22 +30,21 @@ public class Task implements CommandLineRunner { @Override public void run(String... args) throws Exception { - System.out.println("Receive result ======> " + proxyHello()); - System.out.println("Receive rest result ======> " + restHello()); - - } - - private String proxyHello() { - return demoService.sayHello("world"); +// System.out.println("Receive result ======> " + proxyHello()); +// System.out.println("Receive rest result ======> " + restHello()); } - private String restHello() { - RestClient defaultClient = RestClient.create(); - ResponseEntity result = defaultClient.get() - .uri("http://localhost:50052/demo/hello?name=world") - .header("Content-type", "application/json") - .retrieve() - .toEntity(String.class); - return result.getBody(); - } +// private String proxyHello() { +// return demoService.sayHello("world"); +// } +// +// private String restHello() { +// RestClient defaultClient = RestClient.create(); +// ResponseEntity result = defaultClient.get() +// .uri("http://localhost:50052/demo/hello?name=world") +// .header("Content-type", "application/json") +// .retrieve() +// .toEntity(String.class); +// return result.getBody(); +// } } diff --git a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-springmvc/src/main/java/org/apache/dubbo/rest/demo/expansion/filter/FilterService.java b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-springmvc/src/main/java/org/apache/dubbo/rest/demo/expansion/filter/FilterService.java new file mode 100644 index 0000000000..3dfb7c128b --- /dev/null +++ b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-springmvc/src/main/java/org/apache/dubbo/rest/demo/expansion/filter/FilterService.java @@ -0,0 +1,13 @@ +package org.apache.dubbo.rest.demo.expansion.filter; + +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +@RequestMapping(value = "/filter") +public interface FilterService { + @GetMapping(value = "/get/{name}", consumes = "application/json", produces = MediaType.TEXT_PLAIN_VALUE) + public String filterGet(@PathVariable(value = "name") String name); +} diff --git a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-springmvc/src/main/java/org/apache/dubbo/rest/demo/expansion/filter/FilterServiceImpl.java b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-springmvc/src/main/java/org/apache/dubbo/rest/demo/expansion/filter/FilterServiceImpl.java new file mode 100644 index 0000000000..01aa4a036b --- /dev/null +++ b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-springmvc/src/main/java/org/apache/dubbo/rest/demo/expansion/filter/FilterServiceImpl.java @@ -0,0 +1,12 @@ +package org.apache.dubbo.rest.demo.expansion.filter; +import org.apache.dubbo.config.annotation.DubboService; + +@DubboService +public class FilterServiceImpl implements FilterService{ + + @Override + public String filterGet(String name) { + return name; + } + +} diff --git a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-springmvc/src/main/java/org/apache/dubbo/rest/demo/pojo/User.java b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-springmvc/src/main/java/org/apache/dubbo/rest/demo/pojo/User.java new file mode 100644 index 0000000000..9c4c2df352 --- /dev/null +++ b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-springmvc/src/main/java/org/apache/dubbo/rest/demo/pojo/User.java @@ -0,0 +1,87 @@ +package org.apache.dubbo.rest.demo.pojo; +import java.io.Serializable; +import java.util.Objects; + +// 需要 implements Serializable +public class User implements Serializable{ + + private Long id; + + private String name; + + private Integer age; + + public User(Long id, String name){ + this.id = id; + this.name = name; + } + + public User(Long id, String name, Integer age){ + this.id = id; + this.name = name; + this.age = age; + } + + public User(){ + + } + + 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; + } + + public static User getInstance() { + User user = new User(); + user.setAge(18); + user.setName("dubbo"); + user.setId(404l); + return user; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + User user = (User) o; + return Objects.equals(id, user.id) && Objects.equals(name, user.name) && Objects.equals(age, user.age); + } + + @Override + public int hashCode() { + return Objects.hash(id, name, age); + } + + @Override + public String toString() { + return "User{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + '}'; + } + + public String stringToJson(){ + return "{\"id\":\"" + this.id + "\", \"name\":\"" + this.name + "\"}"; + } + +} diff --git a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-springmvc/src/main/java/org/apache/dubbo/rest/demo/provider/DemoServiceImpl.java b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-springmvc/src/main/java/org/apache/dubbo/rest/demo/provider/DemoServiceImpl.java index d90361577b..6b6fdc2106 100644 --- a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-springmvc/src/main/java/org/apache/dubbo/rest/demo/provider/DemoServiceImpl.java +++ b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-springmvc/src/main/java/org/apache/dubbo/rest/demo/provider/DemoServiceImpl.java @@ -16,15 +16,146 @@ */ package org.apache.dubbo.rest.demo.provider; +import com.alibaba.fastjson2.TypeReference; import org.apache.dubbo.config.annotation.DubboService; import org.apache.dubbo.rest.demo.DemoService; +import org.apache.dubbo.rest.demo.pojo.User; +import org.json.JSONObject; +import org.springframework.util.MultiValueMap; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import java.util.HashMap; +import java.util.List; @DubboService public class DemoServiceImpl implements DemoService { + private static final String prefix = "hello "; @Override - public String sayHello(String name) { - return "Hello " + name; + public String getParam(String id) { + return prefix + id; } + @Override + public String getVariable(String id) { + return prefix + id; + } + + @Override + public String getMuchParam(String id, String name) { + return prefix + id + " " + name; + } + + @Override + public String getMuchVariable(String id, String name) { + return prefix + id + " " + name; + } + + @Override + public String getReg(String name, String version, String ext) { + return prefix + name + " " + version + " " + ext; + } + + @Override + public String postBody(String name_json) { + JSONObject jsonObject = new JSONObject(name_json); + return prefix + jsonObject.getString("name"); + } + + @Override + public String postParam(String id) { + return prefix + id; + } + + @Override + public String postVariable(String id) { + return prefix + id; + } + + @Override + public String postUseConsumes(String id) { + JSONObject jsonObject = new JSONObject(id); + return prefix + jsonObject.getString("id"); + } + + @Override + public String postUseParams(String id) { + JSONObject jsonObject = new JSONObject(id); + return prefix + jsonObject.getString("id"); + } + + @Override + public String getHead(String id) { + return prefix + id; + } + + @Override + public String postUseConsumesFormData(MultiValueMap formData) { + return prefix + formData.get("id").get(0) + " " + formData.get("name").get(0); + } + + @Override + public String postMapUser(String formData) { + HashMap userHashMap = com.alibaba.fastjson2.JSONObject.parseObject(formData, new TypeReference>() { + @Override + public HashMap parseObject(String text) { + return super.parseObject(text); + } + }); + + return prefix + userHashMap.get("user1").getId() + " " + userHashMap.get("user2").getId(); + } + + @Override + public String putUpdateId(@PathVariable String id){ + return prefix + id; + } + + @Override + public String deleteId(@PathVariable(value = "id")String id){ + return prefix + id; + } + + @Override + public String patchById(@PathVariable String id, @RequestBody String patchData){ + JSONObject jsonObject = new JSONObject(patchData); + String name = jsonObject.getString("name"); + + return prefix + id + " " + name; + } + + @Override + public String error() { + throw new RuntimeException(); + } + + @Override + public String postUseConsumesUser(@RequestBody MultiValueMap> formData){ + return prefix + formData.get("user1").get(0).get(0).getName(); + } + + @Override + public int primitiveInt(int a, int b) { + return a + b; + } + + @Override + public long primitiveLong(long a, Long b) { + return a + b; + } + + @Override + public long primitiveByte(byte a, Long b) { + return a + b; + } + + @Override + public long primitiveShort(short a, Long b, int c) { + return a + b; + } + + @Override + public List postList(List users){ + return users; + } } diff --git a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-springmvc/src/main/resources/application.yml b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-springmvc/src/main/resources/application.yml index 7596034adb..2d332cda3c 100644 --- a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-springmvc/src/main/resources/application.yml +++ b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-springmvc/src/main/resources/application.yml @@ -24,3 +24,4 @@ dubbo: protocol: name: tri port: 50052 + serialization: fastjson2 diff --git a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-springmvc/src/test/java/org/apache/dubbo/rest/demo/test/ConsumerIT.java b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-springmvc/src/test/java/org/apache/dubbo/rest/demo/test/ConsumerIT.java index 38646cabce..d3a8684749 100644 --- a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-springmvc/src/test/java/org/apache/dubbo/rest/demo/test/ConsumerIT.java +++ b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-springmvc/src/test/java/org/apache/dubbo/rest/demo/test/ConsumerIT.java @@ -16,16 +16,28 @@ */ package org.apache.dubbo.rest.demo.test; +import com.alibaba.fastjson2.JSON; import org.apache.dubbo.config.annotation.DubboReference; import org.apache.dubbo.rest.demo.DemoService; +import org.apache.dubbo.rest.demo.expansion.filter.FilterService; +import org.apache.dubbo.rest.demo.pojo.User; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.HttpEntity; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestClient; -import org.springframework.web.client.RestTemplate; + +import java.lang.reflect.Type; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; import static org.springframework.http.MediaType.APPLICATION_JSON; @@ -33,25 +45,415 @@ @RunWith(SpringRunner.class) public class ConsumerIT { private static final String providerAddress = System.getProperty("dubbo.address", "localhost"); + private static final String urlPrefix = "http://" + providerAddress + ":50052"; + private static final String prefix = "hello "; @DubboReference private DemoService demoService; + @DubboReference + private FilterService filterService; + + @Test + public void getParam(){ + String id = "123"; + String res = demoService.getParam(id); + Assert.assertEquals(prefix + id, res); + } + + @Test + public void RestGetParam(){ + String id = "123"; + RestClient restClient = RestClient.create(); + ResponseEntity responseEntity = restClient.get() + .uri(urlPrefix + "/demo/get/param?id=" + id) + .header("Content-Type", "application/json") + .retrieve() + .toEntity(String.class); + + Assert.assertEquals("\"" + prefix + id + "\"", responseEntity.getBody()); + } + + @Test + public void getVariable(){ + String id = "123"; + String res = demoService.getVariable(id); + Assert.assertEquals(prefix + id, res); + } + + @Test + public void RestGetVariable(){ + String id = "123"; + RestClient restClient = RestClient.create(); + ResponseEntity responseEntity = restClient.get() + .uri(urlPrefix + "/demo/get/variable/{id}", id) + .header("Content-Type", "application/json") + .retrieve() + .toEntity(String.class); + + Assert.assertEquals("\"" + prefix + id + "\"", responseEntity.getBody()); + } + + @Test + public void getMuchParam(){ + String id = "123", name = "test"; + String res = demoService.getMuchParam(id, name); + Assert.assertEquals(prefix + id + " " + name, res); + } + + @Test + public void RestGetMuchParam(){ + String id = "123", name = "test"; + RestClient restClient = RestClient.create(); + ResponseEntity responseEntity = restClient.get() + .uri(urlPrefix + "/demo/get/muchParam?id=" + id + "&name=" + name) + .header("Content-Type", "application/json") + .retrieve() + .toEntity(String.class); + + Assert.assertEquals("\"" + prefix + id + " " + name + "\"", responseEntity.getBody()); + } + + @Test + public void getMuchVariable(){ + String id = "123", name = "test"; + String res = demoService.getMuchVariable(id, name); + Assert.assertEquals(prefix + id + " " + name, res); + } + + @Test + public void RestGetMuchVariable(){ + String id = "123", name = "test"; + RestClient restClient = RestClient.create(); + ResponseEntity responseEntity = restClient.get() + .uri(urlPrefix + "/demo/get/muchVariable/{id}/{name}", id, name) + .header("Content-Type", "application/json") + .retrieve() + .toEntity(String.class); + + Assert.assertEquals("\"" + prefix + id + " " + name + "\"", responseEntity.getBody()); + } + + @Test + public void getReg(){ + String name = "test", version = "2.2.1", ext = ".txt"; + String res = demoService.getReg(name, version, ext); + Assert.assertEquals(prefix + name + " " + version + " " + ext, res); + } + + @Test + public void RestGetReg(){ + String name = "test", version = "2.2.1", ext = ".txt"; + RestClient restClient = RestClient.create(); + ResponseEntity responseEntity = restClient.get() + .uri(urlPrefix + "/demo/get/reg/{name}-{version}{ext}", name, version, ext) + .header("Content-Type", "application/json") + .retrieve() + .toEntity(String.class); + + // 因为已经解析了json 那么不需要再变成json格式了 + Assert.assertEquals(prefix + name + " " + version + " " + ext, responseEntity.getBody()); + } + + @Test + public void postBody(){ + String name = "123"; + String requestBody = "{\"name\": \"" + name + "\"}"; + String res = demoService.postBody(requestBody); + Assert.assertEquals(prefix + name, res); + } + + @Test + public void RestPostBody(){ + String name = "name"; + String requestBody = "{\"name\": \"" + name + "\"}"; + RestClient restClient = RestClient.create(); + ResponseEntity responseEntity = restClient.post() + .uri(urlPrefix + "/demo/post/body") + .header("content-type", "application/json") + .body(requestBody) + .retrieve() + .toEntity(String.class); + + Assert.assertEquals("\"" + prefix + name + "\"", responseEntity.getBody()); + } + @Test - public void test() { - String result = demoService.sayHello("world"); - Assert.assertEquals("Hello world", result); + public void postParam(){ + String name = "123"; + String res = demoService.postParam(name); + Assert.assertEquals(prefix + name, res); } @Test - public void testRest() { - RestClient defaultClient = RestClient.create(); - ResponseEntity result = defaultClient.get() - .uri("http://" + providerAddress + ":50052/demo/hello?name=world") - .header("Content-type", "application/json") + public void RestPostParam(){ + String id = "name"; + RestClient restClient = RestClient.create(); + ResponseEntity responseEntity = restClient.post() + .uri(urlPrefix + "/demo/post/param?id=" + id) + .contentType(APPLICATION_JSON) .retrieve() .toEntity(String.class); - // FIXME - Assert.assertEquals("\"Hello world\"", result.getBody()); + + Assert.assertEquals("\"" + prefix + id + "\"", responseEntity.getBody()); } + + @Test + public void postVariable(){ + String name = "123"; + String res = demoService.postVariable(name); + Assert.assertEquals(prefix + name, res); + } + + @Test + public void RestPostVariable(){ + String id = "name"; + RestClient restClient = RestClient.create(); + ResponseEntity responseEntity = restClient.post() + .uri(urlPrefix + "/demo/post/variable/{id}", id) + .contentType(APPLICATION_JSON) + .retrieve() + .toEntity(String.class); + + Assert.assertEquals("\"" + prefix + id + "\"", responseEntity.getBody()); + } + + @Test + public void postUseConsumes(){ + String id = "123"; + String requestBody = "{\"id\": \"" + id + "\"}"; + String res = demoService.postUseConsumes(requestBody); + Assert.assertEquals(prefix + id, res); + } + + @Test + public void RestUseConsumes(){ + String id = "name"; + String requestBody = "{\"id\":\"" + id + "\"}"; + RestClient restClient = RestClient.create(); + ResponseEntity responseEntity = restClient.post() + .uri(urlPrefix + "/demo/post/useConsumes") + .header("content-type", "application/json") + .body(requestBody) + .retrieve() + .toEntity(String.class); + + Assert.assertEquals("\"" + prefix + id + "\"", responseEntity.getBody()); + } + + @Test + public void postUseParams(){ + String id = "123"; + String requestBody = "{\"id\":\"" + id + "\"}"; + String res = demoService.postUseParams(requestBody); + Assert.assertEquals(prefix + id, res); + } + + @Test + public void RestUseParams(){ + String id = "name"; + String requestBody = "{\"id\":\"" + id + "\"}"; + RestClient restClient = RestClient.create(); + ResponseEntity responseEntity = restClient.post() + .uri(urlPrefix + "/demo/post/useParams?myParam=myValue") + .contentType(APPLICATION_JSON) + .body(requestBody) + .retrieve() + .toEntity(String.class); + + Assert.assertEquals("\"" + prefix + id + "\"", responseEntity.getBody()); + } + + @Test + public void getHead(){ + String id = "123"; + String res = demoService.getHead(id); + Assert.assertEquals(prefix + id, res); + } + + @Test + public void RestGetHead(){ + String id = "123"; + RestClient restClient = RestClient.create(); + ResponseEntity responseEntity = restClient.get() + .uri(urlPrefix + "/demo/get/head/{id}", id) + .header("Content-Type", "application/json") + .header("myHeader", "myValue") + .retrieve() + .toEntity(String.class); + + Assert.assertEquals("\"" + prefix + id + "\"", responseEntity.getBody()); + } + + + @Test + public void RestPostUseConsumesFormData(){ + String id = "123", name = "John"; + + MultiValueMap map = new LinkedMultiValueMap<>(); + map.add("id", id); + map.add("name", name); + + RestClient restClient = RestClient.create(); + ResponseEntity responseEntity = restClient.post() + .uri(urlPrefix + "/demo/post/useConsumes/formData") + .header("Content-Type", "application/x-www-form-urlencoded") + .body(map) + .retrieve() + .toEntity(String.class); + + Assert.assertEquals("\"" + prefix + id + " " + name + "\"", responseEntity.getBody()); + } + + @Test + public void postUser(){ + HashMap map = new HashMap(); + map.put("user1", new User(123L, "nick")); + map.put("user2", new User(456L, "lick")); + + String json = JSON.toJSONString(map); + + Assert.assertEquals(prefix + "123" + " 456", demoService.postMapUser(json)); + } + + // 出现问题 + @Test + public void RestPostUser(){ + HashMap map = new HashMap(); + map.put("user1", new User(123L, "nick")); + map.put("user2", new User(456L, "lick")); + + String json = JSON.toJSONString(map); + + RestClient restClient = RestClient.create(); + ResponseEntity responseEntity = restClient.post() + .uri(urlPrefix + "/demo/post/map/user") + .header("content-type", "application/json") + .body(json) + .retrieve() + .toEntity(String.class); + + Assert.assertEquals("\"" + prefix + "123" + " 456" + "\"", responseEntity.getBody()); + } + + @Test + public void putUpdateId(){ + String id = "123"; + Assert.assertEquals(prefix + id, demoService.putUpdateId(id)); + } + + + @Test + public void RestPutUpdateId(){ + String id = "123"; + RestClient restClient = RestClient.create(); + HttpEntity response = restClient.put() + .uri(urlPrefix + "/demo/put/update/{id}", id) + .retrieve() + .toEntity(String.class); + Assert.assertEquals("\"" + prefix + id + "\"", response.getBody()); + } + + @Test + public void deleteId(){ + String id = "123"; + Assert.assertEquals(prefix + id, demoService.deleteId(id)); + } + + + @Test + public void RestDeleteId(){ + String id = "123"; + + RestClient restClient = RestClient.create(); + HttpEntity response = restClient.delete() + .uri(urlPrefix + "/demo/delete/{id}", id) + .retrieve() + .toEntity(String.class); + Assert.assertEquals("\"" + prefix + id + "\"", response.getBody()); + } + + @Test + public void patchById(){ + String id = "123"; + Assert.assertEquals(prefix + id + " jack", demoService.patchById(id, new User(123L, "jack").stringToJson())); + } + + + @Test + public void RestPatchById(){ + String id = "123"; + String requestBody = new User(12L, "jack").stringToJson(); + + RestClient restClient = RestClient.create(); + HttpEntity response = restClient.patch() + .uri(urlPrefix + "/demo/patch/{id}", id) + .contentType(APPLICATION_JSON) + .body(requestBody) + .retrieve() + .toEntity(String.class); + Assert.assertEquals("\"" + prefix + id + " jack" +"\"", response.getBody()); + } + + @Test + public void primitive(){ + Assert.assertEquals(1 + 2, demoService.primitiveInt(1, 2)); + + Assert.assertEquals(1L + 2L, demoService.primitiveLong(1L, 2L)); + + Assert.assertEquals(1 + 2L, demoService.primitiveByte((byte) 1, 2L)); + + Assert.assertEquals(3L, demoService.primitiveShort((short) 1, 2L, 1)); + } + + @Test + public void filterGet(){ + String name = "123"; + Assert.assertEquals(name, filterService.filterGet(name)); + } + + @Test + public void RestFilterGet(){ + String name = "123"; + RestClient restClient = RestClient.create(); + HttpEntity response = restClient.get() + .uri(urlPrefix + "/filter/get/{name}", name) + .header("content-type", "application/json") + .retrieve() + .toEntity(String.class); + + Assert.assertEquals(name, response.getBody()); + } + + @Test + public void postList(){ + List userList = new LinkedList<>(); + userList.add(new User(123L, "jack")); + userList.add(new User(345L, "mack")); + + Assert.assertEquals(userList, demoService.postList(userList)); + } + + @Test + public void RestPostList(){ + List userList = new LinkedList<>(); + userList.add(new User(123L, "jack", 123)); + userList.add(new User(345L, "mack", 123)); + + RestClient restClient = RestClient.create(); + HttpEntity> response = restClient.post() + .uri(urlPrefix + "/demo/post/list") + .contentType(APPLICATION_JSON) + .body(userList) + .retrieve() + .toEntity(new ParameterizedTypeReference>() { + @Override + public Type getType() { + return super.getType(); + } + }); + + Assert.assertEquals(userList, response.getBody()); + } + }