Created
June 11, 2015 18:45
-
-
Save punya/387c16f3f12a426f4b70 to your computer and use it in GitHub Desktop.
Feigning lists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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