在Apache Camel API中接收空错误响应
我是Apache骆驼的新手。我的要求是,每当发生自定义时,我都想设置自定义主体和响应代码,并将其作为响应发送给用户。 我已经使用Onexception块来捕获自定义例外:
onException(CustomException.class)
.handled(true)
.to("direct:customErrorHandler");
我的CustomErrorhandler是不同类“ customErrorhandler”的路由: -
@Component
public class CustomExceptionHandler extends RouteBuilder {
@Autowired
private CommonExceptionProcessor commonExceptionProcessor;
@Override
public void configure() throws Exception {
JacksonDataFormat jackErrorResponse = new JacksonDataFormat(jacksonConfig.jsonObjectMapper(), Error.class);
from("direct:customErrorHandler")
.routeId("customErrorHandler-Route")
.log(LoggingLevel.DEBUG, this.getClass().getName(), "Handling Error")
.process(commonExceptionProcessor)
.marshal(jackErrorResponse)
}
}
我的常见型号处理器: -
@Component
public class CommonExceptionProcessor implements Processor {
@Override
public void process(Exchange exchange) throws Exception {
CustomException ex = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, CustomException.class);
Error e = new Error();
e.setMessage(ex.getMessage());
e.setStatus("FAILED");
exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, ex.getCode());
exchange.getIn().setBody(e,Error.class);
}
}
但是,每当发生此例外,我都会使用垃圾范围的状态代码空白响应。请帮忙。
期望的回应:
{
"message":"customException occured",
"status":"FAILED"
}
I am new to Apache Camel. My requirement is that, whenever a custom exception occur, i want to set a custom Body and response code and send it back to the user as response.
I have used OnException Block to catch the custom exception :
onException(CustomException.class)
.handled(true)
.to("direct:customErrorHandler");
My customErrorHandler is a route in a different class "CustomErrorhandler":-
@Component
public class CustomExceptionHandler extends RouteBuilder {
@Autowired
private CommonExceptionProcessor commonExceptionProcessor;
@Override
public void configure() throws Exception {
JacksonDataFormat jackErrorResponse = new JacksonDataFormat(jacksonConfig.jsonObjectMapper(), Error.class);
from("direct:customErrorHandler")
.routeId("customErrorHandler-Route")
.log(LoggingLevel.DEBUG, this.getClass().getName(), "Handling Error")
.process(commonExceptionProcessor)
.marshal(jackErrorResponse)
}
}
My Common expection processor:-
@Component
public class CommonExceptionProcessor implements Processor {
@Override
public void process(Exchange exchange) throws Exception {
CustomException ex = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, CustomException.class);
Error e = new Error();
e.setMessage(ex.getMessage());
e.setStatus("FAILED");
exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, ex.getCode());
exchange.getIn().setBody(e,Error.class);
}
}
But whenever this exception is occuring, i am getting empty response with garbage statusCode. please help.
Expectated Response:
{
"message":"customException occured",
"status":"FAILED"
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论