For testing purposed I used a message body reader and writer implementation
for the JSON media type that is using GSON.

The annotations and class declaration looks like:

@Component(scope = PROTOTYPE)
@JaxrsApplicationSelect("(osgi.jaxrs.name=*)")
@JaxrsExtension
@JaxrsMediaType(APPLICATION_JSON)
public class GsonMessageBodyReaderWriter<T> implements
MessageBodyReader<T>, MessageBodyWriter<T> { ... }

I see that this class is used for media type JSON and PLAIN_TEXT.

So I created another message body reader for media type plain text and a
higher service ranking (just for testing).

@Component(scope = PROTOTYPE)
@ServiceRanking(100)
@JaxrsApplicationSelect("(" + JaxrsWhiteboardConstants.JAX_RS_NAME + "=*)")
@JaxrsExtension
@JaxrsMediaType(MediaType.TEXT_PLAIN)
public class PlainTextMediaTypeExtension<T> implements
MessageBodyReader<T>, MessageBodyWriter<T> { ... }

If both components are available I would assume the
PlainTextMediaTypeExtension is used for the media type plain text and the
GsonMessageBodyReaderWriter is used for media type json (because of the
JaxrsMediaType annotation.
But now PlainTextMediaTypeExtension is used for plain text AND JSON.

I tested JAX-RS Whiteboard implementation v1.0.5 and v1.0.6.



Am Mo., 13. Jan. 2020 um 15:31 Uhr schrieb Markus Rathgeb <
[email protected]>:

> Hi,
>
> I run into trouble on generating a simple plain text reply.
> If my response contains a string as entity and the endpoint is marked to
> generate a plain text media type I would assume that the body of the
> response contains exactly that string without any additional characters.
>
> If my observation has been correct the response string entity is placed
> into quotes.
>
> @GET
> @Path("foo")
> @Produces(MediaType.TEXT_PLAIN)
> public Response foo() {
>     return Response.ok("foo").build();
> }
>
> I would expect (because of "text plain") that the response body is
> foo
> But it is
> "foo"
>
> $ curl -v 'http://127.0.0.1:8080/rest/test/foo'
> *   Trying 127.0.0.1:8080...
> * TCP_NODELAY set
> * Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
> > GET /rest/test/foo HTTP/1.1
> > Host: 127.0.0.1:8080
> > User-Agent: curl/7.65.0
> > Accept: */*
> >
> * Mark bundle as not supporting multiuse
> < HTTP/1.1 200 OK
> < Date: Mon, 13 Jan 2020 14:30:28 GMT
> < Content-Type: text/plain
> < Transfer-Encoding: chunked
> <
> * Connection #0 to host 127.0.0.1 left intact
> "foo"
>
> Can you please explain what I am doing wrong?
>

Reply via email to