Skip to content

Commit

Permalink
Merge pull request #550 from liweinan/resteasy838
Browse files Browse the repository at this point in the history
RESTEASY-838 Add JSON MIME type to @Formatted to "pretty" print JSON output
  • Loading branch information
patriot1burke committed Aug 18, 2014
2 parents 6850417 + 2eda81f commit 8a6f0b3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
@@ -0,0 +1,15 @@
package org.jboss.resteasy.annotations.providers.jackson;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @author <a href="mailto:l.weinan@gmail.com">Weinan Li</a>
*/
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Formatted
{
}
Expand Up @@ -8,11 +8,11 @@
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.util.LRUMap;
import com.fasterxml.jackson.jaxrs.cfg.AnnotationBundleKey;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import com.fasterxml.jackson.jaxrs.json.JsonEndpointConfig;
import com.fasterxml.jackson.jaxrs.util.ClassKey;
import org.jboss.resteasy.annotations.providers.jackson.Formatted;
import org.jboss.resteasy.annotations.providers.NoJackson;
import org.jboss.resteasy.util.FindAnnotation;

Expand Down Expand Up @@ -131,15 +131,28 @@ public void writeTo(Object value, Class<?> type, Type genericType, Annotation[]
ClassAnnotationKey key = new ClassAnnotationKey(type, annotations);
JsonEndpointConfig endpoint;
endpoint = _writers.get(key);

// not yet resolved (or not cached any more)? Resolve!
if (endpoint == null) {
ObjectMapper mapper = locateMapper(type, mediaType);
endpoint = _configForWriting(mapper, annotations);
// and cache for future reuse
ObjectMapper mapper = locateMapper(type, mediaType);
endpoint = _configForWriting(mapper, annotations);

// and cache for future reuse
_writers.put(key, endpoint);
}

ObjectWriter writer = endpoint.getWriter();
ObjectWriter writer = endpoint.getWriter();
boolean withIndentOutput = false; // no way to replace _serializationConfig

// we can't cache this.
if (annotations != null) {
for (Annotation annotation : annotations) {
if (annotation.annotationType().equals(Formatted.class)) {
withIndentOutput = true;
break;
}
}
}

/* 27-Feb-2009, tatu: Where can we find desired encoding? Within
* HTTP headers?
Expand All @@ -149,7 +162,7 @@ public void writeTo(Object value, Class<?> type, Type genericType, Annotation[]

try {
// Want indentation?
if (writer.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
if (writer.isEnabled(SerializationFeature.INDENT_OUTPUT) || withIndentOutput) {
jg.useDefaultPrettyPrinter();
}
// 04-Mar-2010, tatu: How about type we were given? (if any)
Expand Down
Expand Up @@ -8,6 +8,7 @@
import java.io.InputStreamReader;
import javax.xml.bind.annotation.*;

import org.jboss.resteasy.annotations.providers.jackson.Formatted;
import org.jboss.resteasy.annotations.providers.NoJackson;
import org.jboss.resteasy.annotations.providers.jaxb.json.BadgerFish;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
Expand Down Expand Up @@ -148,7 +149,16 @@ public Product getProduct()
return new Product(333, "Iphone");
}

@GET
@GET
@Produces("application/json")
@Path("/formatted/{id}")
@Formatted
public Product getFormattedProduct()
{
return new Product(333, "Iphone");
}

@GET
@Produces("application/json")
public Product[] getProducts()
{
Expand Down Expand Up @@ -233,6 +243,21 @@ public void testJacksonString() throws Exception

}

@Test
public void testFormattedJacksonString() throws Exception
{
WebTarget target = client.target(generateURL("/products/formatted/333"));
Response response = target.request().get();
String entity = response.readEntity(String.class);
System.out.println(entity);
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals(("{\n" +
" \"name\" : \"Iphone\",\n" +
" \"id\" : 333\n" +
"}"), entity);
response.close();
}

@Test
public void testXmlString() throws Exception
{
Expand Down

0 comments on commit 8a6f0b3

Please sign in to comment.