Skip to content
Merged
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
7 changes: 7 additions & 0 deletions jaxrs/paramconverter/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
= JAX-RS ParamConverter and ParamConverterProvider

This example demonstrate the use of a +ParamConverter+ / +ParamConverterProvider+ to set the request parameters in a user bean
that has no constructor with a single +String+ argument, nor +fromValue(String)+ or +valueOf(String)+ methods.
The +ParamConverter+ is registered by a +ParamConverterProvider+ annotated with +@Provider+.

The +ParamConverter+ applies to JAX-RS Resource Method parameters annotated with +@MatrixParam+, +@QueryParam+, +@PathParam+, +@CookieParam+ and +@HeaderParam+.
15 changes: 15 additions & 0 deletions jaxrs/paramconverter/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<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>org.javaee7.jaxrs</groupId>
<artifactId>jaxrs-samples</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<groupId>org.javaee7.jaxrs</groupId>
<artifactId>paramconverter</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.javaee7.jaxrs.paramconverter;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

/**
* @author Arun Gupta
*/
@ApplicationPath("webresources")
public class MyApplication extends Application {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.javaee7.jaxrs.paramconverter;

/**
* @author Xavier Coulon
*
*/
public class MyBean {

private String value;

/**
* @return the value
*/
public String getValue() {
return value;
}

/**
* @param value the value to set
*/
public void setValue(String value) {
this.value = value;
}

@Override
public String toString() {
return getValue();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.javaee7.jaxrs.paramconverter;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import javax.ws.rs.ext.ParamConverter;
import javax.ws.rs.ext.ParamConverterProvider;
import javax.ws.rs.ext.Provider;

/**
* @author Xavier Coulon
*/
@Provider
public class MyBeanConverterProvider implements ParamConverterProvider {

@Override
public <T> ParamConverter<T> getConverter(Class<T> clazz, Type type, Annotation[] annotations) {
if (clazz.getName().equals(MyBean.class.getName())) {

return new ParamConverter<T>() {

@SuppressWarnings("unchecked")
@Override
public T fromString(String value) {
MyBean bean = new MyBean();
bean.setValue(value);
return (T) bean;
}

@Override
public String toString(T bean) {
return ((MyBean)bean).getValue();
}

};
}
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.javaee7.jaxrs.paramconverter;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import javax.ws.rs.ext.ParamConverter;
import javax.ws.rs.ext.ParamConverterProvider;
import javax.ws.rs.ext.Provider;

/**
* @author Xavier Coulon
*
*/
@Provider
public class MyConverterProvider implements ParamConverterProvider {

@Override
public <T> ParamConverter<T> getConverter(final Class<T> rawType, final Type genericType,
final Annotation[] annotations) {
if (rawType.getName().equals(MyBean.class.getName())) {
return new ParamConverter<T>() {

@Override
public T fromString(String value) {
MyBean myBean = new MyBean();
myBean.setValue(value);
return rawType.cast(myBean);
}

@Override
public String toString(T myBean) {
if (myBean == null) {
return null;
}
return myBean.toString();
}
};
}
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.javaee7.jaxrs.paramconverter;

import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

/**
* @author Arun Gupta
* @author Xavier coulon
*/
@Path("/endpoint")
public class MyResource {

@GET
@Produces(MediaType.TEXT_PLAIN)
public String getWithQuery(@DefaultValue("bar") @QueryParam("search") MyBean myBean) {
return myBean.getValue();
}

@GET
@Path("/{id}")
@Produces(MediaType.TEXT_PLAIN)
public String getByPath(@PathParam("id") MyBean myBean) {
return myBean.getValue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.javaee7.jaxrs.paramconverter;

import static org.junit.Assert.assertEquals;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
* @author Arun Gupta
* @author Xavier Coulon
*/
@RunWith(Arquillian.class)
public class MyResourceTest {

@Deployment(testable = false)
public static WebArchive createDeployment() {
return ShrinkWrap.create(WebArchive.class)
.addClasses(MyApplication.class, MyResource.class, MyBeanConverterProvider.class, MyBean.class);
}
private static WebTarget target;

@ArquillianResource
private URL base;

@Before
public void setUpClass() throws MalformedURLException {
Client client = ClientBuilder.newClient();
target = client.target(URI.create(new URL(base, "webresources/endpoint").toExternalForm()));
}

@Test
public void testRequestWithQueryParam() {
String r = target.queryParam("search", "foo").request().get(String.class);
assertEquals("foo", r);
}

@Test
public void testRequestWithNoQueryParam() {
String r = target.request().get(String.class);
assertEquals("bar", r);
}

@Test
public void testRequestWithPathParam() {
String r = target.path("/foo").request().get(String.class);
assertEquals("foo", r);
}

}
1 change: 1 addition & 0 deletions jaxrs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<module>link</module>
<module>mapping-exceptions</module>
<!--<module>moxy</module>-->
<module>paramconverter</module>
<module>readerwriter</module>
<module>readerwriter-json</module>
<module>request-binding</module>
Expand Down