Pass URI path to JAX-RS providers

I recently implemented the JAX-RS Rest service. I created a JIBX provider that allows you to decouple and sort between XML and Java types. I would also like to indicate the version of my service by specifying the version in the URL. Versions will include the message binding version used to marshal and untie Java types.

Therefore, it is necessary that the version be passed to the JIBX manufacturer and therefore the URL containing the version. However, the provider interfaces ( MessageBodyWriterand MessageBodyReader) do not provide the URI path in their methods interface.

The following is the method signature writeTo()for the interface MessageBodyWriter:

writeTo(Object, Type, Annotation[], MediaType, MultivaluedMap, OutputStream)

This method parameter does not contain the uri path, so the jibx custom provider cannot know which version of the message binding it should use for Java-type marshalling. Is there any way around this?

+3
source share
2 answers

If you need something more JAX-RS than HttpServletRequest, you can enter javax.ws.rs.core.UriInfo.

public class MyProvider implements MessageBodyWriter {
    @javax.ws.rs.core.Context
    javax.ws.rs.core.UriInfo uriInfo;
}

I assume that you are using @ javax.ws.rs.PathParam to capture the path parameter. Then you can use UriInfo.getPathParameters (). You can also return to UriInfo.getPathSegments () to get the information you are looking for. This saves you from having to parse the request URI yourself. Any JAX-RS implementation should be able to do this.

+8

URI , @Context .

,

public class CustomProvider implements MessageBodyWriter
{

    @Context HttpServletRequest request;

    ....
}

. , , , , , , , .

0

Source: https://habr.com/ru/post/1754495/


All Articles