1

I've implemented REST Controller methods and they work fine when there is no aspects implemented. After I implement aspects, controller methods still execute fine in background(i.e DELETE request actually deletes entries), but when i'm testing responses with Postman, i don't get any responses. Aspects do work, and txt file gets updated when methods get invoked.

@RestController public class MovieScreeningREST { @Autowired private MovieScreeningService movieScreeningService; @GetMapping("/movies") public List<MovieScreening> findAll(){ return movieScreeningService.findAll(); } @GetMapping("/movies/genre={movieGenre}") public List<MovieScreening> findAllByMovieGenre(@PathVariable String movieGenre){ return movieScreeningService.findAllByMovieGenre(movieGenre); } @GetMapping("/movies/minimum-tickets={amount}") public List<MovieScreening> findAllByTicketsSoldGreaterThanEqual(@PathVariable int amount){ return movieScreeningService.findAllByTicketsSoldGreaterThanEqual(amount); } @GetMapping("/movies/maximum-screenings={amount}") public List<MovieScreening> findAllByScreeningsNumberLessThanEqual(@PathVariable int amount){ return movieScreeningService.findAllByScreeningsNumberLessThanEqual(amount); } @GetMapping("/movies/id={id}") public MovieScreening findById(@PathVariable int id){ return movieScreeningService.findById(id); } @PostMapping("/movies") public MovieScreening addMovieScreening(@RequestBody MovieScreening movieScreening){ return movieScreeningService.save(movieScreening); } @PutMapping("/movies") public MovieScreening editMovieScreening(@RequestBody MovieScreening movieScreening){ return movieScreeningService.save(movieScreening); } @DeleteMapping("/movies/id={id}") public String deleteMovieScreening(@PathVariable int id){ return movieScreeningService.deleteById(id); } 

}

Aspect class

@EnableAspectJAutoProxy @Aspect @Component public class Logger { @Pointcut("execution(* asss.pj.projekat_bioskop.controller.*.*(..))") public void allRESTMethods(){}; @Around("allRESTMethods()") public void blabla(ProceedingJoinPoint pjp) { //@Before String methodName = pjp.getSignature().getName(); String before = "*** Attempting " + methodName + " method ***"; writeLog(before); try { //@AfterReturning pjp.proceed(); String success = "\n*** Method " + methodName + " has succeeded"; writeLog(success); } catch (Throwable throwable){ //@AfterThrowing String failed = "\n*** Method " + methodName + " has failed. " + throwable.getMessage() + " .***"; writeLog(failed); } //@After String after = "\n"; writeLog(after); } public void writeLog(String log){ File file = new File("filmovi_izvestaj.txt"); try (Writer wr = new FileWriter(file, true)){ wr.write(log); } catch(FileNotFoundException fe){ fe.printStackTrace(); } catch(IOException ioe){ ioe.printStackTrace(); } } 

}

Postman

1 Answer 1

2

@Around Advice

The value returned by the around advice is the return value seen by the caller of the method.

Your advice is not returning anything which prevents the calling method to get the actual return value. Modify your around advice method to return Object to complete the flow correctly.

Example

@Around("allRESTMethods()") public Object blabla(ProceedingJoinPoint pjp) { //@Before String methodName = pjp.getSignature().getName(); String before = "*** Attempting " + methodName + " method ***"; writeLog(before); Object retVal = null; try { //@AfterReturning retVal = pjp.proceed(); String success = "\n*** Method " + methodName + " has succeeded"; writeLog(success); } catch (Throwable throwable){ //@AfterThrowing String failed = "\n*** Method " + methodName + " has failed. " + throwable.getMessage() + " .***"; writeLog(failed); } //@After String after = "\n"; writeLog(after); return retVal; } 
Sign up to request clarification or add additional context in comments.

1 Comment

It is amazing and kind of unexpected that a Spring AOP @Around advice returning void even matches methods returning something else. In AspectJ I would expect the aspect not even being triggered. This seems to be one of the subtle semantic differences between Spring AOP and AspectJ, even though the former uses the latter's library for matching pointcuts. I do wonder why the Spring folks made it different and whether that was a deliberate choice. A non-matching advice would be better for debugging than a matching one returning null.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.