Skip to content

Commit

Permalink
dubbo-samples-rest-springmvc-1 (#1121)
Browse files Browse the repository at this point in the history
  • Loading branch information
QuYao-Fighting authored Aug 15, 2024
1 parent 4131d1f commit 46a945c
Show file tree
Hide file tree
Showing 9 changed files with 786 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20231013</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand All @@ -78,6 +91,19 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgs>
<compilerArg>-proc:none</compilerArg>
</compilerArgs>
<!--新增-->
<parameters>true</parameters>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> 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<String, List<User>> formData);

// HashMap<String, User> -> 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<User> postList(@RequestBody List<User> users);


// add more methods
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<String> result = defaultClient.get()
// .uri("http://localhost:50052/demo/hello?name=world")
// .header("Content-type", "application/json")
// .retrieve()
// .toEntity(String.class);
// return result.getBody();
// }
}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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 + "\"}";
}

}
Loading

0 comments on commit 46a945c

Please sign in to comment.