diff --git a/README.md b/README.md index 374f78c..e298eee 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ You define the api meta data in classes' static code blocks, then it was collect static Mapping petStatus = $(text(oneOf(Arrays.asList("available", "pending", "sold")))) .desc("pet status in the store").example("available").$$; static Mapping pet = $(mapping( - field("id", $(vLong()).desc("pet id").example(gen("petId").or(gen(() -> new Faker().number().randomNumber()))).$$), + field("id", $(vLong()).desc("pet id").example(gen("petId").or(gen(() -> faker.number().randomNumber()))).$$), field("name", $(text(required())).desc("pet name").$$), field("category", attach(required()).to($(mapping( field("id", vLong(required())), diff --git a/example/java-jaxrs/src/main/java/com/example/resource/PetResource.java b/example/java-jaxrs/src/main/java/com/example/resource/PetResource.java index d92444c..fbb73e2 100644 --- a/example/java-jaxrs/src/main/java/com/example/resource/PetResource.java +++ b/example/java-jaxrs/src/main/java/com/example/resource/PetResource.java @@ -43,14 +43,16 @@ @Path("/pet") @Produces({"application/json", "application/xml"}) public class PetResource { - static PetData petData = new PetData(); + private static PetData petData = new PetData(); + private static Faker faker = new Faker(); + private ResourceBundle bundle = ResourceBundle.getBundle("bind-messages"); private Messages messages = (key) -> bundle.getString(key); static Mapping petStatus = $(text(oneOf(Arrays.asList("available", "pending", "sold")))) .desc("pet status in the store").example("available").$$; static Mapping pet = $(mapping( - field("id", $(longv()).desc("pet id").example(gen("petId").or(gen(() -> new Faker().number().randomNumber()))).$$), + field("id", $(longv()).desc("pet id").example(gen("petId").or(gen(() -> faker.number().randomNumber()))).$$), field("name", $(text(required())).desc("pet name").$$), field("category", $(mapping( field("id", longv(required())), diff --git a/src/main/java/com/github/tminglei/swagger/fake/DataWriterImpl.java b/src/main/java/com/github/tminglei/swagger/fake/DataWriterImpl.java index 2b7eb61..a6735be 100644 --- a/src/main/java/com/github/tminglei/swagger/fake/DataWriterImpl.java +++ b/src/main/java/com/github/tminglei/swagger/fake/DataWriterImpl.java @@ -2,6 +2,7 @@ import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; import java.io.IOException; import java.io.Writer; @@ -17,14 +18,15 @@ public class DataWriterImpl implements DataWriter { private static final String FORMAT_JSON = "application/json"; private static final String FORMAT_XML = "application/xml"; + private static final ObjectMapper objectMapper = new ObjectMapper() + .setSerializationInclusion(JsonInclude.Include.NON_NULL) + .configure(SerializationFeature.INDENT_OUTPUT, true); + @Override public void write(Writer writer, String format, DataProvider provider) throws IOException { switch (format.toLowerCase()) { case FORMAT_JSON: - String dataJson = new ObjectMapper() - .setSerializationInclusion(JsonInclude.Include.NON_NULL) - .writer().withDefaultPrettyPrinter() - .writeValueAsString(provider.get()); + String dataJson = objectMapper.writeValueAsString(provider.get()); writer.write(dataJson); break; case FORMAT_XML: