gpt4 book ai didi

spring - 如何使用Spring Boot处理服务上的异常?

转载 作者:行者123 更新时间:2023-12-03 08:46:02 27 4
gpt4 key购买 nike

我有以下服务:

@Service
public class MyService {

private static final Logger LOG = LoggerFactory.getLogger(MyService.class);
private ObjectMapper objectMapper = new ObjectMapper();

@JmsListener(destination = "queue")
public void receiveMessage(String message) throws JMSException, IOException {
LOG.info("Got message: " + message);
MyObject obj = objectMapper.readValue(message, MyObject.class);
LOG.info("Object Name " + obj.getName());
}
}

如果 receiveMessage引发错误,我想将此错误记录到日志文件中。如何使用Spring Boot做到这一点?

最佳答案

试试这个

@Service
public class MyService {

private static final Logger LOG = LoggerFactory.getLogger(MyService.class);
private ObjectMapper objectMapper = new ObjectMapper();

@JmsListener(destination = "queue")
public void receiveMessage(String message) throws JMSException, IOException {
try{
LOG.info("Got message: " + message);
MyObject obj = objectMapper.readValue(message, MyObject.class);
LOG.info("Object Name " + obj.getName());
}catch (JMSException | IOException e){
LOG.error(e.getMesage(),e);
throw e;
}
}
}

关于spring - 如何使用Spring Boot处理服务上的异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54026895/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com