|
1 | | -Conjure-Java |
2 | | -======= |
3 | | -The java implementation of Conjure generator for java. |
| 1 | +# Conjure-Java  [](https://opensource.org/licenses/Apache-2.0) |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | +_CLI to generate Java POJOs and interfaces from [Conjure API definitions](https://github.com/palantir/conjure)._ |
| 7 | + |
| 8 | +## Usage |
| 9 | + |
| 10 | +The recommended way to use conjure-java is via a build tool like [gradle-conjure](https://github.com/palantir/gradle-conjure). However, if you don't want to use gradle-conjure, there is also an executable which conforms to [RFC 002](https://github.com/palantir/conjure/blob/develop/rfc/002-contract-for-conjure-generators.md), published on [bintray](https://bintray.com/palantir/releases/conjure-java). |
| 11 | + |
| 12 | + Usage: conjure-java generate <input> <output> [...options] |
| 13 | + |
| 14 | + --objects [boolean] Generate POJOs for Conjure type definitions |
| 15 | + --jersey [boolean] Generate jax-rs annotated interfaces for client or server-usage |
| 16 | + --retrofit [boolean] Generate retrofit interfaces for streaming/async clients |
| 17 | + |
| 18 | +## Example generated objects |
| 19 | + |
| 20 | +Conjure-java objects are always immutable and thread-safe. Fields are never null; instead, Java 8 Optionals are used. JSON serialization is handled using [Jackson](https://github.com/FasterXML/jackson) annotations. |
| 21 | + |
| 22 | +- **Conjure object: [ManyFieldExample](./conjure-java-core/src/integrationInput/java/com/palantir/product/ManyFieldExample.java)** |
| 23 | + |
| 24 | + Objects can only be instantiated using the builder pattern, or by deserializing from JSON. |
| 25 | + |
| 26 | + ```java |
| 27 | + ManyFieldExample example = ManyFieldExample.builder() |
| 28 | + .string("foo") |
| 29 | + .integer(123) |
| 30 | + .optionalItem("bar") |
| 31 | + .addAllItems(iterable) |
| 32 | + .build(); |
| 33 | + |
| 34 | + // or using Jackson (via com.palantir.remoting3:jackson-support) |
| 35 | + ObjectMapper mapper = ObjectMappers.newServerObjectMapper(); |
| 36 | + ManyFieldExample fromJson = mapper.readValue("{\"string\":\"foo\", ...}", ManyFieldExample.class); |
| 37 | + ``` |
| 38 | + |
| 39 | +- **Conjure union: [UnionTypeExample](./conjure-java-core/src/integrationInput/java/com/palantir/product/UnionTypeExample.java)** |
| 40 | + |
| 41 | + Union types can be one of a few variants. To interact with a union value, users should use the `.accept` method and define a Visitor that handles each of the possible variants, including the possibility of an unknown variant. |
| 42 | + |
| 43 | + ```java |
| 44 | + Foo output = unionTypeExample.accept(new Visitor<Foo>() { |
| 45 | + |
| 46 | + public Foo visitStringExample(StringExample value) { |
| 47 | + // your logic here! |
| 48 | + } |
| 49 | + |
| 50 | + public Foo visitSet(Set<String> value) {} |
| 51 | + |
| 52 | + // ... |
| 53 | + |
| 54 | + public Foo visitUnknown(String unknownType) {} |
| 55 | + |
| 56 | + }); |
| 57 | + ``` |
| 58 | + |
| 59 | + Visitors may seem clunky in Java, but they have the upside of compile-time assurance that you've handled all the possible variants. If you upgrade an API dependency and the API author added a new variant, the Java compiler will force you to explicitly deal with this new variant. We intentionally avoid `switch` statements and `instanceof` checks for this exact reason. |
| 60 | +
|
| 61 | +- **Conjure enum: [EnumExample](./conjure-java-core/src/integrationInput/java/com/palantir/product/EnumExample.java)** |
| 62 | +
|
| 63 | + Enums are subtly different from regular Java enums because they tolerate deserializing unknown values. This is important because it ensures server authors can add new variants to an enum without causing runtime errors for consumers that use older API jars. |
| 64 | +
|
| 65 | + ```java |
| 66 | + EnumExample one = EnumExample.ONE; |
| 67 | + System.out.println(one); // prints: 'ONE' |
| 68 | +
|
| 69 | + EnumExample fromJson = mapper.readValue("\"XYZ\"", EnumExample.class); |
| 70 | + System.out.println(fromJson); // prints: 'XYZ' |
| 71 | + ``` |
| 72 | +
|
| 73 | +- **Conjure alias: [StringAliasExample](./conjure-java-core/src/integrationInput/java/com/palantir/product/StringAliasExample.java)** |
| 74 | +
|
| 75 | + Aliases have exactly the same JSON representation as their inner type, so they are useful for making error-prone function signatures more bulletproof: |
| 76 | +
|
| 77 | + ```diff |
| 78 | + -doSomething(String, String, String); |
| 79 | + +doSomething(ProductId, UserId, EmailAddress); |
| 80 | + ``` |
| 81 | +
|
| 82 | +## Example Jersey interfaces |
| 83 | +
|
| 84 | +Conjure-java generates Java interfaces with [JAX-RS](http://jax-rs-spec.java.net/) annotations, so they can be used on the client side or on the server-side. |
| 85 | +
|
| 86 | +- Example jersey interface: [EteService](./conjure-java-core/src/integrationInput/java/com/palantir/product/EteService.java) |
| 87 | +
|
| 88 | +For **client-side usage**, use [http-remoting](https://github.com/palantir/http-remoting#jaxrs-clients) which configures Feign with sensible defaults. |
| 89 | +
|
| 90 | +For **server-side usage**, you need a [Jersey](https://jersey.github.io/)-compatible server. We recommend Dropwizard (which is based on Jetty), but Grizzly, Tomcat, etc should also work fine! Use [http-remoting's jersey-servers](https://github.com/palantir/http-remoting#jersey-servers) to configure Jackson and Exception mappers appropriately. |
| 91 | + |
| 92 | + |
| 93 | +## Example Retrofit interfaces |
| 94 | + |
| 95 | +As an alternative to the JAX-RS interfaces above, conjure-java can generate equivalent interfaces with [Retrofit2](http://square.github.io/retrofit/) annotations. These clients are useful if you want to stream binary data or make non-blocking async calls: |
| 96 | + |
| 97 | +- Example retrofit2 interface: [EteServiceRetrofit](./conjure-java-core/src/integrationInput/java/com/palantir/product/EteServiceRetrofit.java) |
| 98 | + |
| 99 | + ```java |
| 100 | + @GET("./base/binary") |
| 101 | + @Streaming |
| 102 | + Call<ResponseBody> binary(@Header("Authorization") AuthHeader authHeader); |
| 103 | + ``` |
| 104 | + |
| 105 | +You can also supply the `--retrofitCompletableFutures` flag if you prefer Java 8 CompletableFutures instead of OkHttp's Call. |
| 106 | +
|
| 107 | +## Contributing |
| 108 | +
|
| 109 | +For instructions on how to set up your local development environment, check out the [Contributing document][./CONTRIBUTING.md]. |
0 commit comments