Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add jaxrs annotation use sample cases #1163

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -63,9 +66,30 @@ public List<String> testMapForm(MultivaluedMap<String, String> 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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> testMapForm(MultivaluedMap<String,String> 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<User[]> response = RestClient.create().post()
.uri("http://" + providerAddress +":50052/complex/array")
Expand Down Expand Up @@ -153,7 +152,6 @@ public void testStringMap() throws Exception {

@Test
public void testHeader() throws Exception {

ResponseEntity<String> response = RestClient.create().get()
.uri("http://" + providerAddress +":50052/complex/testMapHeader")
.header("Content-type", "application/json")
Expand Down Expand Up @@ -194,7 +192,6 @@ public void testMapForm() throws Exception {

@Test
public void testXml() throws Exception {
// TODO xml
String str = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><person><name>1</name></person>";
Person person = new Person("1");

Expand All @@ -220,4 +217,49 @@ public void testXml() throws Exception {
Assert.assertEquals(person,result);
}

@Test
public void testCookie(){
ResponseEntity<String> 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<String> 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<String> 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<String, String> map = new LinkedMultiValueMap<>();
map.add("name","Li");
ResponseEntity<String> 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());
}

}
Loading