gpt4 book ai didi

error-handling - 来自 Jersey 的Dropwizard错误消息

转载 作者:行者123 更新时间:2023-12-04 05:03:33 27 4
gpt4 key购买 nike

我得到下面的日志输出。我猜拖放向导正在尝试出错,但是它使用的Jersey JAX-RS实现不知道如何格式化错误?我怎么看?

ERROR [2014-10-01 08:08:55,875] com.sun.jersey.spi.container.ContainerResponse: A message body writer for Java class io.dropwizard.jersey.errors.ErrorMessage, and Java type class io.dropwizard.jersey.errors.ErrorMessage, and MIME media type text/plain was not found.
The registered message body writers compatible with the MIME media type are:
*/* ->
com.sun.jersey.core.impl.provider.entity.FormProvider
com.sun.jersey.core.impl.provider.entity.StringProvider
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
com.sun.jersey.core.impl.provider.entity.FileProvider
com.sun.jersey.core.impl.provider.entity.InputStreamProvider
com.sun.jersey.core.impl.provider.entity.DataSourceProvider
com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.ReaderProvider
com.sun.jersey.core.impl.provider.entity.DocumentProvider
com.sun.jersey.core.impl.provider.entity.StreamingOutputProvider
com.sun.jersey.core.impl.provider.entity.SourceProvider$SourceWriter
com.sun.jersey.server.impl.template.ViewableMessageBodyWriter
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider
text/plain ->
com.sun.jersey.core.impl.provider.entity.StringProvider
com.sun.jersey.core.impl.provider.entity.ReaderProvider

FWIW我打的方法签名是:
@POST
@UnitOfWork
@Path("/update")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
public String updateResults(ResultsUpdate results) {...}

最佳答案

您需要一个Serialiszer用于ErrorMessage。如果使用Eclipse Strg + Shift + T并搜索“JsonProcessingExceptionMapper”。此Exception映射器希望使用实体ErrorMessage建立响应,但是您没有为此的映射器。

您有两种选择:

  • 删除此异常映射器并添加您的自定义异常映射器
    没有实体
  • 您创建一个消息正文编写器。

  • 选项1:
    在您的run()中添加以下方法:
    private void removeDefaultExceptionMappers(boolean deleteDefault,Environment environment)
    {
    if(deleteDefault){
    ResourceConfig jrConfig = environment.jersey().getResourceConfig();
    Set<Object> dwSingletons = jrConfig.getSingletons();
    List<Object> singletonsToRemove = new ArrayList<Object>();

    for (Object singletons : dwSingletons) {
    if (singletons instanceof ExceptionMapper && !singletons.getClass().getName().contains("DropwizardResourceConfig")) {
    singletonsToRemove.add(singletons);
    }
    }

    for (Object singletons : singletonsToRemove) {
    LOG.info("Deleting this ExceptionMapper: " + singletons.getClass().getName());
    jrConfig.getSingletons().remove(singletons);
    }
    }
    }

    这将删除默认情况下在DW中添加的所有exeption映射器。现在,您可以添加您真正想要的所有exeption映射器。就我而言:
        environment.jersey().register(new ConstraintViolationExceptionMapper());
    environment.jersey().register(new CustomJsonProcessingExceptionMapper());
    environment.jersey().register(new EarlyEofExceptionMapper());

    现在,编写没有实体的自定义CustomJsonProcessingExceptionMapper:
    @Provider
    public class CustomJsonProcessingExceptionMapper implements ExceptionMapper<JsonProcessingException> {

    private static final Logger LOG = LoggerFactory.getLogger(CustomJsonProcessingExceptionMapper.class);

    @Override
    public Response toResponse(JsonProcessingException exception) {
    /*
    * If the error is in the JSON generation, it's a server error.
    */
    if (exception instanceof JsonGenerationException) {
    LOG.warn("Error generating JSON", exception);
    return Response.serverError().build();
    }

    final String message = exception.getOriginalMessage();

    /*
    * If we can't deserialize the JSON because someone forgot a no-arg constructor, it's a
    * server error and we should inform the developer.
    */
    if (message.startsWith("No suitable constructor found")) {
    LOG.error("Unable to deserialize the specific type", exception);
    return Response.serverError().build();
    }

    /*
    * Otherwise, it's those pesky users.
    */
    LOG.debug("Unable to process JSON (those pesky users...)", exception);
    return Response.status(Response.Status.BAD_REQUEST)
    .build();
    }

    }

    选项2:
    您为ErrorMessage创建一个序列化器/消息正文编写器。为此,请尝试以下操作:
    @Provider
    @Produces(MediaType.TEXT_PLAIN)
    public class ErrorMessageBodyWriter implements MessageBodyWriter<ErrorMessage> {

    private static final Logger LOG = LoggerFactory.getLogger(ErrorMessageBodyWriter.class);

    @Override
    public boolean isWriteable(
    Class<?> type,
    Type genericType,
    Annotation[] annotations,
    MediaType mediaType)
    {
    return ValidationErrorMessage.class.isAssignableFrom(type);
    }

    @Override
    public long getSize(
    ErrorMessage t,
    Class<?> type,
    Type genericType,
    Annotation[] annotations,
    MediaType mediaType)
    {
    return -1;
    }

    @Override
    public void writeTo(
    ErrorMessage t,
    Class<?> type,
    Type genericType,
    Annotation[] annotations,
    MediaType mediaType,
    MultivaluedMap<String, Object> httpHeaders,
    OutputStream entityStream) throws IOException, WebApplicationException
    {
    String message = t.getMessage();
    entityStream.write(message.getBytes(Charsets.UTF_8));
    LOG.info(message);
    }

    }

    添加您的run():
    // Serializer
    environment.jersey().register(new ErrorMessageBodyWriter());

    希望这可以帮助 :-)

    关于error-handling - 来自 Jersey 的Dropwizard错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26138516/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com