Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

296
Visualizações
Restlet error response format in JSON

Im using the restlet framework to manager a projects API. It seems that by default error responses are formatted in HTML. How can I change that so that by default ALL error responses are in JSON format?

I've tried adding a custom converter which works great for the entity responses but not for error responses.

We have 110+ endpoints that support application/json so ideally I would like to just set the default errors to always return as JSON. The default converter works for all methods that return an actual entity.

@Get("json")
@Produces("application/json")
public User represent() {
    ...
    return result;
}

But the ResourceException thrown by this method returns HTML.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

If you are sure about the format your service is going to produce then you can annotate your service class with @Produces annotation at class level. Then you will not be required to define the same for each and every method.

Also, once @Produces is defined at class level and you want to change response format for a particular method then you can annotate that particular method for other format.

Try Below code..

public Response represent(){
try{

}catch(Exception ex){
    return Response.status(500)
            .entity(new ExceptionMessage("500", ex.getMessage()))
            .type(MediaType.APPLICATION_JSON).
            build();
}
return Response.status(Response.Status.OK).entity(result).build();

}

And have below Model class for exception message.

@XmlRootElement
class ExceptionMessage{
    private String statusCode;
    private String errorMessage;

    public ExceptionMessage() {
    }

    public ExceptionMessage(String statusCode, String errorMessage) {
        this.statusCode = statusCode;
        this.errorMessage = errorMessage;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public String getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(String statusCode) {
        this.statusCode = statusCode;
    }

}

This is the link dedicated to Restlet.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda