Jersey - content negotiation for error handling

Jakub Bochenski

I'd like to allow clients to select an error response format using HTTP content negotiation.

E.g. given an endpoint

@Produces("application/json")
class MyService {

 @GET
 public String getSomething() {
    if (currentTimeMilis() % 2 == 0) throw new MyException();

    return "{ hello:\"world\" }";
 }

and exception mapper:

class MyExceptionMapper implements ExceptionMapper<MyException> {

    @Override
    public Response toResponse(MyException ex) {

        return status(BAD_REQUEST)
                .entity(new MyDto(randomUUID(), ex.getMessage()))
                .build();
    }

} 

and having two body writers, MyBodyWriter and standard JacksonJsonProvider.

I'd like to invoke one of the writers depending on the contents of Accept header, e.g.

  • Accept: application/json -> invokes JacksonJsonProvider
  • Accept: application/vnd-mycompany-error, application/json -> invokes MyBodyWriter

I had tried different approaches but all of them fail because the matched HttpRule implies an application/json content type.

The only workaround I've found is to inject the request headers into ExceptionMapper and set the content type explicitly there -- but I don't like it.

zyexal

Maybe just a workaround, but ...
You could try to use a ContainerResponseFilter, get all accpeted MediaTypes from the ContainerRequestContext and limit the response MediaType by reset the entity with ContainerResponseContext.setEntity.

There might be better solutions!
Used jersey 2.12:

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.List;

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.Provider;

import path.to.MyException;

@Provider
public class EntityResponseFilter implements ContainerResponseFilter {

    @Override
    public void filter( ContainerRequestContext reqc , ContainerResponseContext resc ) throws IOException {
        List<MediaType> mediaTypes = reqc.getAcceptableMediaTypes();
        MediaType mediaType = new MediaType("application", "vnd-mycompany-error");
        if( mediaTypes.contains( mediaType) && resc.getEntity() instanceof MyDao /* or MyDto, or null, or whatever */) {   
            resc.setEntity( resc.getEntity(), new Annotation[0], mediaType );
        }
        // ...
    }
}

Hope this was helpfull somehow:)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Content negotiation to return HTML

From Dev

PhalconPHP: content negotiation?

From Dev

Restlet Content Type Negotiation

From Dev

Content negotiation using WebSharper

From Dev

PhalconPHP: content negotiation?

From Dev

Spring boot controller content negotiation

From Dev

error:Algorithm negotiation fail with SharpSSH

From Dev

Spring MVC Content Negotiation throwing ClassCastException

From Dev

SupportedMediaTypes not used when doing content negotiation

From Dev

Why not use content negotiation to return JSON object?

From Dev

How to Use Content Negotiation in Spring Data Rest?

From Dev

How to disable content negotiation for Spring Actuators?

From Dev

Content Negotiation when looking at different output formats

From Dev

Content negotiation for File Download In Web API

From Dev

Is there a simple way to solve content negotiation in Spring boot

From Dev

How to Use Content Negotiation in Spring Data Rest?

From Dev

Content negotiation not working with respond_to stanza

From Dev

What is performing Transparent Content Negotiation in Apache

From Dev

Cross Domain Negotiation request error with SignalR

From Dev

Python pysft/paramiko 'EOF during negotiation' error

From Dev

ISAKMP negotiation error connecting to VPN from China?

From Dev

Correct handling of background calls in jersey

From Dev

Does HTTP content negotiation respect media type parameters

From Dev

spring cloud config-server .properties content-negotiation failing

From Dev

Is there better approach to Accept Header based content negotiation in Spring API?

From Dev

Why would HTTP content negotiation be preferred to explicit parameters in an API scenario?

From Dev

Is HTTP content negotiation being used by browsers and servers in practice?

From Dev

How to add csv MimeType to spring content-negotiation?

From Dev

git -- handling frozen content

Related Related

  1. 1

    Content negotiation to return HTML

  2. 2

    PhalconPHP: content negotiation?

  3. 3

    Restlet Content Type Negotiation

  4. 4

    Content negotiation using WebSharper

  5. 5

    PhalconPHP: content negotiation?

  6. 6

    Spring boot controller content negotiation

  7. 7

    error:Algorithm negotiation fail with SharpSSH

  8. 8

    Spring MVC Content Negotiation throwing ClassCastException

  9. 9

    SupportedMediaTypes not used when doing content negotiation

  10. 10

    Why not use content negotiation to return JSON object?

  11. 11

    How to Use Content Negotiation in Spring Data Rest?

  12. 12

    How to disable content negotiation for Spring Actuators?

  13. 13

    Content Negotiation when looking at different output formats

  14. 14

    Content negotiation for File Download In Web API

  15. 15

    Is there a simple way to solve content negotiation in Spring boot

  16. 16

    How to Use Content Negotiation in Spring Data Rest?

  17. 17

    Content negotiation not working with respond_to stanza

  18. 18

    What is performing Transparent Content Negotiation in Apache

  19. 19

    Cross Domain Negotiation request error with SignalR

  20. 20

    Python pysft/paramiko 'EOF during negotiation' error

  21. 21

    ISAKMP negotiation error connecting to VPN from China?

  22. 22

    Correct handling of background calls in jersey

  23. 23

    Does HTTP content negotiation respect media type parameters

  24. 24

    spring cloud config-server .properties content-negotiation failing

  25. 25

    Is there better approach to Accept Header based content negotiation in Spring API?

  26. 26

    Why would HTTP content negotiation be preferred to explicit parameters in an API scenario?

  27. 27

    Is HTTP content negotiation being used by browsers and servers in practice?

  28. 28

    How to add csv MimeType to spring content-negotiation?

  29. 29

    git -- handling frozen content

HotTag

Archive