Spring catch and errors

0

next ...

I'm using spring for a private project and wanted to know the best way to catch errors.

I have my repository classes:

@Repository
public class PrimeiraClasseDaoImpl implements PrimeiraClasseDao {

    @PersistenceContext
    private EntityManager em;

    public void salvarObjeto(final ObjetoTeste objetoTeste) {
        em.persist(objetoTeste);    
    }

... outros métodos....

@Repository
public class SegundaClasseDaoImpl implements SegundaClasseDao {

    @PersistenceContext
    private EntityManager em;

    public void salvarObjeto(final ObjetoTeste objetoTeste) {
        em.persist(objetoTeste);    
    }

... outros métodos....

I also have the service class:

@Service
@Transactional
public class PrimeiraClasseServiceImpl implements PrimeiraClasseService {

    @Autowired
    private PrimeiraClasseDao primeiraClasseDao;

    @Autowired
    private SegundaClasseDao segundaClasseDao;

    public void salvarObjeto(PrimeiraClasse primeiraClasse) {

        segundaClasseDao.salvarObjeto(primeiraClasse.getSegundaClasse());
        primeiraClasseDao.salvarObjeto(primeiraClasse);
    }

I want to change the void of these methods to return success or error, I thought about creating an object and fixing the error ...

But I wanted to know how I capture the errors and stick to that object.

I do not know if you have any other way to work, how do you work in this situation?

    
asked by anonymous 25.08.2015 / 19:43

1 answer

2

The classic way is to drop an exception and leave the point of the code that invokes its methods of the class annotated with @Service, to figure out what to do with it. If you are working in a web application, an interesting way is to isolate the treatments in a class annotated with @ControllerAdvice, which functions as an interceptor (aspect) focused on your classes annotated with @Controller. Any exception not handled directly in the controller can be directed to specific methods of the class annotated with @ControllerAdvice.

    
28.10.2015 / 12:58