【问题标题】:Runtime exception is not serializable运行时异常不可序列化
【发布时间】:2018-12-04 03:36:16
【问题描述】:
SEVERE: Servlet.service() for servlet [Jersey Web Application] in context with path *** threw exception
[javax.ws.rs.ProcessingException: Error deserializing object from entity stream.] with root cause

当我抛出运行时异常时,在 Jax-rs 中抛出异常。

我有一个抛出运行时异常的普通函数。我想用异常映射器映射它,但由于一些奇怪的原因,上面的异常在我的运行时异常之前被抛出。有什么想法吗?

这是具有获取电话方法的类,但我的自定义例外:

import java.util.Base64;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Lob;
import javax.persistence.Transient;
import javax.ws.rs.ProcessingException;
import java.lang.StackTraceElement;

import com.server.messit.errorhandling.exceptions.DataNotFoundException;
import com.server.messit.errorhandling.exceptions.IncorrectValueException;
import com.server.messit.errorhandling.handler.ErrorChecker;

/**
 * @author Prasad
 *
 */
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class User {
    @Id
    @Column(nullable = false, unique = true)
    private String phone;


    /**
     * @return the phone
     */
    public String getPhone() {
        return phone;
    }
    /**
     * @param phone the phone to set
     */
    public void setPhone(String phone) {
        if(!ErrorChecker.isValidPhoneNumber(phone)) {
            throw new DataNotFoundException("Incorrect Phone Number");
        } 
        this.phone = phone;
    }
}

这里是异常类:

public class DataNotFoundException extends RuntimeException {

    /**
     * 
     */
    private static final long serialVersionUID = -1422751876879336366L;

    public DataNotFoundException() {
        super();
    }

    public DataNotFoundException(String message) {
        super(message);
    }
}

这里是异常映射器类:

import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import com.server.messit.errorhandling.handler.ErrorMessage;

/**
 * @author Prasad
 *
 */
@Provider
public class DataNotFoundExceptionMapper implements ExceptionMapper<DataNotFoundException>{

    @Override
    public Response toResponse(DataNotFoundException exception) {
        ErrorMessage errorMessage = new ErrorMessage(exception.getMessage(), 404, "Doc");
        return Response.status(Status.NOT_FOUND).entity(errorMessage).build();
    }

}

这里是抛出的异常:

Dec 04, 2018 5:52:51 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [Jersey Web Application] in context with path [/MessItServer] threw exception [javax.ws.rs.ProcessingException: Error deserializing object from entity stream.] with root cause
com.server.messit.errorhandling.exceptions.DataNotFoundException: Incorrect Phone Number
    at com.server.messit.core.User.setPhone(User.java:62)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.eclipse.yasson.internal.model.SetWithSetter.internalSetValue(SetWithSetter.java:27)
    at org.eclipse.yasson.internal.model.SetValueCommand.setValue(SetValueCommand.java:26)
    at org.eclipse.yasson.internal.model.ReflectionPropagation.setValue(ReflectionPropagation.java:67)
    at org.eclipse.yasson.internal.model.PropertyModel.setValue(PropertyModel.java:272)
    at org.eclipse.yasson.internal.serializer.ObjectDeserializer.lambda$getInstance$0(ObjectDeserializer.java:101)
    at java.util.LinkedHashMap.forEach(Unknown Source)
    at org.eclipse.yasson.internal.serializer.ObjectDeserializer.getInstance(ObjectDeserializer.java:95)
    at org.eclipse.yasson.internal.serializer.AbstractContainerDeserializer.deserialize(AbstractContainerDeserializer.java:62)
    at org.eclipse.yasson.internal.Unmarshaller.deserializeItem(Unmarshaller.java:57)
    at org.eclipse.yasson.internal.Unmarshaller.deserialize(Unmarshaller.java:50)
    at org.eclipse.yasson.internal.JsonBinding.deserialize(JsonBinding.java:45)
    at org.eclipse.yasson.internal.JsonBinding.fromJson(JsonBinding.java:85)
    at org.glassfish.jersey.jsonb.internal.JsonBindingProvider.readFrom(JsonBindingProvider.java:99)
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.invokeReadFrom(ReaderInterceptorExecutor.java:257)
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:236)
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:156)
    at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundReadFrom(MappableExceptionWrapperInterceptor.java:73)
    at org.gl

javax.ws.rs.ProcessingException 异常在我的异常之前抛出,因此我无法将我的异常映射到自定义 json 响应。

【问题讨论】:

  • stackoverflow.com/questions/30819988/… 这是否回答了您的问题?检查您是否使用默认构造函数。该问题还需要有关您的班级的更多信息。
  • 更新了更多数据的问题

标签: java exception serialization exception-handling


【解决方案1】:

我得到了答案。我不应该在我的数据模型类中抛出异常,即“用户”。 所以它给了我序列化错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-20
    • 2017-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多