Skip to content

Instantly share code, notes, and snippets.

@punya
Created June 11, 2015 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save punya/387c16f3f12a426f4b70 to your computer and use it in GitHub Desktop.
Save punya/387c16f3f12a426f4b70 to your computer and use it in GitHub Desktop.
Feigning lists
apply plugin: 'java'
repositories {
jcenter()
}
def feign = { "com.netflix.feign:feign-$it:6.1.2" }
dependencies {
compile feign("jackson"), feign("jaxrs")
testCompile 'junit:junit:4.12'
testCompile 'org.assertj:assertj-core:3.0.0'
}
package testo;
import com.fasterxml.jackson.annotation.JsonTypeName;
@JsonTypeName("cartesian")
public class CartesianPoint implements Point {
public double x, y;
public CartesianPoint(double x, double y) {
this.x = x;
this.y = y;
}
}
package testo;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.List;
public interface FakeService {
@Consumes(MediaType.APPLICATION_JSON)
@POST
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
void doSomething(List<Point> args);
@Consumes(MediaType.APPLICATION_JSON)
@POST
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
void doSomething(Point[] args);
}
package testo;
import feign.Feign;
import feign.jackson.JacksonModule;
import feign.jaxrs.JAXRSModule;
import org.junit.Test;
import java.util.Arrays;
public class FakeServiceTests {
private static final Point[] POINTS = new Point[] {
new CartesianPoint(2.0, 3.0),
new PolarPoint(2.0, 3.0)
};
private final FakeService service = Feign.create(
FakeService.class, "http://localhost:8000",
new JAXRSModule(), new JacksonModule());
@Test
public void testList() {
service.doSomething(Arrays.asList(POINTS));
}
@Test
public void testArray() {
service.doSomething(POINTS);
}
}
package testo;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.WRAPPER_OBJECT)
@JsonSubTypes({
@JsonSubTypes.Type(CartesianPoint.class),
@JsonSubTypes.Type(PolarPoint.class)
})
public interface Point {
// marker-only
}
package testo;
import com.fasterxml.jackson.annotation.JsonTypeName;
@JsonTypeName("polar")
public class PolarPoint implements Point {
public double r, θ;
public PolarPoint(double r, double θ) {
this.r = r;
this.θ = θ;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment