![](/img/trans.png)
[英]How to set the HTTP STATUS code to other than 500 in CXF Out Fault Interceptor?
[英]Set Status code 500
我正在使用rest API,在其中通过发布XML文件发出发布请求时,如果成功接收到文件,则应发送响应200 OK;如果XML文件缺少任何标记,则应发送响应200;如果使用GET,则应发送500错误。而不是POST。 我能够执行前两种情况200和400,不确定如何在代码中添加500。 在这里发布我的代码,请提出建议。
if (dataFile==null) {
try {
response.sendError(HttpServletResponse.SC_BAD_REQUEST,
"Failed to process content of file.");
}
catch (IOException e1) {
LOGGER.error("Failed to send response due to error:
" + e1.getMessage());
}
}
else{
response.setStatus(HttpServletResponse.SC_OK);
response.addHeader("Location", getResourceUri(dataFileMetadata,
request).toString());
从您的代码示例中尚不清楚您要在此处执行的操作,但是我想您正在谈论的是这样的内容:
@POST
@Path("/some/path")
@Consumes(MediaType.APPLICATION_XML)
public Response post(String someXML) {
try {
if (isMissingTag(someXML)) {
return Response.status(Status.BAD_REQUEST).entity("error message goes here").build();
}
// do stuff
return Response.ok().build();
} catch (Exception e) {
LOG.error("error message goes here", e);
return Response.serverError().entity("error message goes here").build();
}
}
如果有人尝试使用GET来访问您的服务,则无需返回任何内容,因为您首先不会将其发布为端点。
如果您对编写宁静的Web服务感兴趣,我强烈建议阅读jersey用户指南 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.