From b37e1eda9d43d41dc70f4c0e728d1302ecb12eac Mon Sep 17 00:00:00 2001 From: fanlobu <2840697550@qq.com> Date: Thu, 20 Jun 2024 22:14:30 +0800 Subject: [PATCH 1/3] add(): add jaxrs annotation use sample cases --- .../complex/ComplexParamReqServiceImpl.java | 24 ++++++++++ .../complex/ComplexParamRequestService.java | 31 ++++++++++-- .../rest/demo/test/ComplexParamRequestIT.java | 47 ++++++++++++++++++- 3 files changed, 96 insertions(+), 6 deletions(-) diff --git a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-jaxrs/src/main/java/org/apache/dubbo/rest/demo/complex/ComplexParamReqServiceImpl.java b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-jaxrs/src/main/java/org/apache/dubbo/rest/demo/complex/ComplexParamReqServiceImpl.java index b8393be1e6..c8628adedb 100644 --- a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-jaxrs/src/main/java/org/apache/dubbo/rest/demo/complex/ComplexParamReqServiceImpl.java +++ b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-jaxrs/src/main/java/org/apache/dubbo/rest/demo/complex/ComplexParamReqServiceImpl.java @@ -21,7 +21,10 @@ import org.apache.dubbo.rest.demo.pojo.Person; import org.apache.dubbo.rest.demo.pojo.User; +import javax.ws.rs.core.Cookie; +import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.UriInfo; import java.util.List; import java.util.Map; import java.util.Set; @@ -63,9 +66,30 @@ public List testMapForm(MultivaluedMap params) { return params.values().stream().flatMap(List::stream).toList(); } + @Override + public String testHeader(HttpHeaders headers) { + return headers.getHeaderString("name"); + } + + @Override + public String testUriInfo(UriInfo uriInfo) { + return uriInfo.getPath(); + } + + @Override + public String testForm(Person person) { + return person.getName(); + } + + @Override public Person testXml(Person person) { return person; } + @Override + public String testCookie(Cookie cookie) { + return cookie.getValue(); + } + } diff --git a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-jaxrs/src/main/java/org/apache/dubbo/rest/demo/complex/ComplexParamRequestService.java b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-jaxrs/src/main/java/org/apache/dubbo/rest/demo/complex/ComplexParamRequestService.java index a154f43015..80f331586c 100644 --- a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-jaxrs/src/main/java/org/apache/dubbo/rest/demo/complex/ComplexParamRequestService.java +++ b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-jaxrs/src/main/java/org/apache/dubbo/rest/demo/complex/ComplexParamRequestService.java @@ -28,8 +28,12 @@ import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.Cookie; +import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.UriInfo; import java.util.List; import java.util.Map; import java.util.Set; @@ -64,15 +68,34 @@ public interface ComplexParamRequestService { @Produces(MediaType.TEXT_PLAIN) String testMapHeader(@HeaderParam("headers") String headerMap); + + @POST + @Path("/xml") + @Consumes(MediaType.TEXT_XML) + @Produces(MediaType.APPLICATION_XML) + Person testXml(Person person); + + + @GET + @Path("/cookie") + @Produces(MediaType.TEXT_PLAIN) + String testCookie(@Context Cookie cookie); @POST @Path("/testMapForm") @Produces({MediaType.APPLICATION_JSON}) @Consumes({MediaType.APPLICATION_FORM_URLENCODED}) List testMapForm(MultivaluedMap params); + @GET + @Path("/httpHeader") + String testHeader(@Context HttpHeaders headers); + + @GET + @Path("/uri") + @Produces(MediaType.TEXT_PLAIN) + String testUriInfo(@Context UriInfo uriInfo); @POST - @Path("/xml") - @Consumes(MediaType.TEXT_XML) - @Produces(MediaType.APPLICATION_XML) - Person testXml(Person person); + @Path("/annoForm") + @Produces(MediaType.TEXT_PLAIN) + String testForm(@org.jboss.resteasy.annotations.Form Person person); } diff --git a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-jaxrs/src/test/java/org/apache/dubbo/rest/demo/test/ComplexParamRequestIT.java b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-jaxrs/src/test/java/org/apache/dubbo/rest/demo/test/ComplexParamRequestIT.java index c77cd12685..0312651281 100644 --- a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-jaxrs/src/test/java/org/apache/dubbo/rest/demo/test/ComplexParamRequestIT.java +++ b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-jaxrs/src/test/java/org/apache/dubbo/rest/demo/test/ComplexParamRequestIT.java @@ -153,7 +153,6 @@ public void testStringMap() throws Exception { @Test public void testHeader() throws Exception { - ResponseEntity response = RestClient.create().get() .uri("http://" + providerAddress +":50052/complex/testMapHeader") .header("Content-type", "application/json") @@ -194,7 +193,6 @@ public void testMapForm() throws Exception { @Test public void testXml() throws Exception { - // TODO xml String str = "1"; Person person = new Person("1"); @@ -220,4 +218,49 @@ public void testXml() throws Exception { Assert.assertEquals(person,result); } + @Test + public void testCookie(){ + ResponseEntity response = RestClient.create().get() + .uri("http://" + providerAddress + ":50052/complex/cookie") + .header("Content-type", "application/json") + .header( "cookie","cookie=1") + .retrieve() + .toEntity(String.class); + Assert.assertEquals("1",response.getBody()); + } + + @Test + public void testHttpHeader(){ + ResponseEntity response = RestClient.create().get() + .uri("http://" + providerAddress + ":50052/complex/httpHeader") + .header( "Content-type","text/plain") + .header( "name","world") + .retrieve() + .toEntity(String.class); + Assert.assertEquals("world", response.getBody()); + } + + @Test + public void testUri(){ + ResponseEntity response = RestClient.create().get() + .uri("http://" + providerAddress + ":50052/complex/uri") + .header("Content-type", "application/json") + .retrieve() + .toEntity(String.class); + Assert.assertEquals("/complex/uri", response.getBody()); + } + + @Test + public void testAnnoFrom(){ + MultiValueMap map = new LinkedMultiValueMap<>(); + map.add("name","Li"); + ResponseEntity response = RestClient.create().post() + .uri("http://" + providerAddress + ":50052/complex/annoForm") + .contentType(MediaType.APPLICATION_FORM_URLENCODED) + .body(map) + .retrieve() + .toEntity(String.class); + Assert.assertEquals("Li", response.getBody()); + } + } From 367a4b5a414156a2682574a22a9f275285040b3b Mon Sep 17 00:00:00 2001 From: fanlobu <2840697550@qq.com> Date: Sat, 22 Jun 2024 21:16:15 +0800 Subject: [PATCH 2/3] fix(): fix ci --- .../org/apache/dubbo/rest/demo/test/ComplexParamRequestIT.java | 1 - 1 file changed, 1 deletion(-) diff --git a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-jaxrs/src/test/java/org/apache/dubbo/rest/demo/test/ComplexParamRequestIT.java b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-jaxrs/src/test/java/org/apache/dubbo/rest/demo/test/ComplexParamRequestIT.java index 0312651281..e800f69643 100644 --- a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-jaxrs/src/test/java/org/apache/dubbo/rest/demo/test/ComplexParamRequestIT.java +++ b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-jaxrs/src/test/java/org/apache/dubbo/rest/demo/test/ComplexParamRequestIT.java @@ -123,7 +123,6 @@ public void testSet() throws Exception { @Test public void testArray() throws Exception { - User[] array = {new User(1L,"1",1),new User(2L,"2",2)}; ResponseEntity response = RestClient.create().post() .uri("http://" + providerAddress +":50052/complex/array") From 66d0ec711cd71cf3b9622b05ca0f0b962f0751d7 Mon Sep 17 00:00:00 2001 From: fanlobu <2840697550@qq.com> Date: Tue, 25 Jun 2024 13:39:40 +0800 Subject: [PATCH 3/3] fix(): fix ci test --- .../dubbo/rest/demo/routine/BasicParamRequestService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-jaxrs/src/main/java/org/apache/dubbo/rest/demo/routine/BasicParamRequestService.java b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-jaxrs/src/main/java/org/apache/dubbo/rest/demo/routine/BasicParamRequestService.java index cc48eb107f..c3fb4ab5ba 100644 --- a/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-jaxrs/src/main/java/org/apache/dubbo/rest/demo/routine/BasicParamRequestService.java +++ b/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-jaxrs/src/main/java/org/apache/dubbo/rest/demo/routine/BasicParamRequestService.java @@ -23,7 +23,7 @@ import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; - +@Path("/") public interface BasicParamRequestService { @GET @Path("/primitive")