空 JSON 响应不会触发 Jackson 中的错误
Empty JSON response doesn't trigger error in Jackson
我正在使用 Jackson 来解释我正在写的 API 的 JSON 回复。作为贯穿我的 API 的标准,我想从 API 向程序抛出错误,例如:
{"errorMessage":"No such username."}
所以我希望我的响应处理器首先检查响应是否只是单个 errorMessage 键,如果是,则处理错误,如果不是,则将其解释为它期望从该命令获得的任何响应。
所以这是我的代码:
public class ProcessingException extends Exception {
private String errorMessage;
public ProcessingException(){}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
}
然后,在我的响应处理程序中:
@Override
public void useResponse(InputStream in) throws IOException, ProcessingException {
// turn response into a string
java.util.Scanner s = new java.util.Scanner(in).useDelimiter("\A");
String response = s.hasNext() ? s.next() : "";
ProcessingException exception;
try {
// Attempt to interpret as an exception
exception = mapper.readValue(response, ProcessingException.class);
}
catch(IOException e) {
// Otherwise interpret it as expected. responseType() is an abstract TypeReference
// which is filled in by subclasses. useResponse() is also abstract. Each subclass
// represents a different kind of request.
Object responseObj = mapper.readValue(response, responseType());
useResponse(responseObj);
return;
}
// I needed this out of the try/catch clause because listener.errorResponse might
// actually choose to throw the passed exception to be dealt with by a higher
// authority.
if (listener!=null) listener.errorResponse(exception);
}
这工作得很好,除了在一种情况下 - 有些请求实际上不需要响应任何东西,所以它们 return {}
。由于某种原因,此响应完全贯穿 exception = mapper.readValue(response, ProcessingException.class);
行而没有触发 IOException,因此程序认为存在错误。但是当它试图读取错误是什么时,它会在尝试读取 exception.getErrorMessage()
时抛出 NullPointerException
,因为当然没有错误。
为什么将 {}
视为有效的 ProcessingException
对象?
Jackson 没有 bean 验证。但是您可以做的是将构造函数声明为 JsonCreator,它将用于实例化新对象,并且 check/throw 如果该字段为 null 则抛出异常:
class ProcessingException {
private String errorMessage;
@JsonCreator
public ProcessingException(@JsonProperty("errorMessage") String errorMessage) {
if (errorMessage == null) {
throw new IllegalArgumentException("'errorMessage' can't be null");
}
this.errorMessage = errorMessage;
}
// getters, setters and other methods
}
我正在使用 Jackson 来解释我正在写的 API 的 JSON 回复。作为贯穿我的 API 的标准,我想从 API 向程序抛出错误,例如:
{"errorMessage":"No such username."}
所以我希望我的响应处理器首先检查响应是否只是单个 errorMessage 键,如果是,则处理错误,如果不是,则将其解释为它期望从该命令获得的任何响应。
所以这是我的代码:
public class ProcessingException extends Exception {
private String errorMessage;
public ProcessingException(){}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
}
然后,在我的响应处理程序中:
@Override
public void useResponse(InputStream in) throws IOException, ProcessingException {
// turn response into a string
java.util.Scanner s = new java.util.Scanner(in).useDelimiter("\A");
String response = s.hasNext() ? s.next() : "";
ProcessingException exception;
try {
// Attempt to interpret as an exception
exception = mapper.readValue(response, ProcessingException.class);
}
catch(IOException e) {
// Otherwise interpret it as expected. responseType() is an abstract TypeReference
// which is filled in by subclasses. useResponse() is also abstract. Each subclass
// represents a different kind of request.
Object responseObj = mapper.readValue(response, responseType());
useResponse(responseObj);
return;
}
// I needed this out of the try/catch clause because listener.errorResponse might
// actually choose to throw the passed exception to be dealt with by a higher
// authority.
if (listener!=null) listener.errorResponse(exception);
}
这工作得很好,除了在一种情况下 - 有些请求实际上不需要响应任何东西,所以它们 return {}
。由于某种原因,此响应完全贯穿 exception = mapper.readValue(response, ProcessingException.class);
行而没有触发 IOException,因此程序认为存在错误。但是当它试图读取错误是什么时,它会在尝试读取 exception.getErrorMessage()
时抛出 NullPointerException
,因为当然没有错误。
为什么将 {}
视为有效的 ProcessingException
对象?
Jackson 没有 bean 验证。但是您可以做的是将构造函数声明为 JsonCreator,它将用于实例化新对象,并且 check/throw 如果该字段为 null 则抛出异常:
class ProcessingException {
private String errorMessage;
@JsonCreator
public ProcessingException(@JsonProperty("errorMessage") String errorMessage) {
if (errorMessage == null) {
throw new IllegalArgumentException("'errorMessage' can't be null");
}
this.errorMessage = errorMessage;
}
// getters, setters and other methods
}