有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java运行时异常不可序列化

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中抛出的异常

我有一个普通函数,它会引发运行时异常。我想用异常映射器映射它,但出于某种奇怪的原因,在我的运行时异常之前抛出了上面的异常。知道吗

下面是具有get phone方法的类,但我的自定义例外:

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响应


共 (0) 个答案