|
From: Jeff S. <je...@in...> - 2011-09-08 20:15:25
|
[ugh, send failure]
On Thu, Sep 8, 2011 at 12:51 PM, Jeff Schnitzer <je...@in...> wrote:
>
> // Produces 'hello', which produces parse errors in browsers and ObjectMapper
> Object getThing() { return "hello"; }
To make this a little more concrete, I'd like to know how you folks
propose implementing this method:
public class NameResource {
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public String getNameForThing(@PathParam("id") long id) {
return actuallyLookupTheNameFor(id);
}
}
Here's how I've solved the problem, which feels like working around
broken behavior in Resteasy:
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class StringJacksonProvider implements MessageBodyWriter<String>
{
@Override
public boolean isWriteable(Class<?> aClass, Type type, Annotation[]
annotations, MediaType mediaType)
{
return MediaType.APPLICATION_JSON_TYPE.equals(mediaType) && aClass
== String.class;
}
@Override
public long getSize(String value, Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType)
{
return -1;
}
@Override
public void writeTo(String value, Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String,Object> httpHeaders, OutputStream entityStream)
{
try {
new ObjectMapper().writeValue(entityStream, value);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}
|