Say you have the following as a RESTservice
If you had to figure out a way to override the Accept Header value and always generate JSON, how would you do it?
CXF interceptors allows you to achieve this elegantly.
Register a OutInterceptor in your beans.xml
@Path("/loc")
@GET
@Consumes
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public String getLocation(...)
{
..
}
This method produces XML or JSON based on the Accept Header that is passed in. If you had to figure out a way to override the Accept Header value and always generate JSON, how would you do it?
CXF interceptors allows you to achieve this elegantly.
Register a OutInterceptor in your beans.xml
public class CustomOutInterceptor extends AbstractPhaseInterceptor<Message>
{
public CustomOutInterceptor()
{
super(Phase.PRE_STREAM);
}
public void handleMessage(final Message message) throws Fault
{
final List<MediaType> mediaTypeL = new ArrayList<MediaType>();
//Return JSON always
mediaTypeL.add(MediaType.APPLICATION_JSON_TYPE);
message.getExchange().put(Message.ACCEPT_CONTENT_TYPE, mediaTypeL);
}
}