与REST的Guice模块集成问题

时间:2017-04-22 20:41:35

标签: rest guice

与REST的Guice模块集成问题 我已经定义了一个基于AOP guice的模块,但是当我尝试与REST代码集成时,methodInvocation.proceed retun null。 什么可能是解决这个问题的最佳方法。

Define AOP Guice based module as below 

@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.METHOD)
@interface NotOnWeekends {}
public class WeekendBlocker implements MethodInterceptor {

  public Object invoke(MethodInvocation invocation) throws Throwable {

    Calendar today = new GregorianCalendar();

    if (today.getDisplayName(DAY_OF_WEEK, LONG, ENGLISH).startsWith("S")) {

      throw new IllegalStateException(

          invocation.getMethod().getName() + " not allowed on weekends!");
    }

    return invocation.proceed();

  }

}

public class NotOnWeekendsModule extends AbstractModule {
  protected void configure() {

    bindInterceptor(Matchers.any(), Matchers.annotatedWith(NotOnWeekends.class), 

        new WeekendBlocker());
  }
}

But I tried to Integrate this with my REST API 


    public class WorkerBean implements Worker {

    @Autowired
    private WorkerRepository workerRepository;

    @Override
    @NotOnWeekends
    public Collection<Worker> findAll() {

        Collection<Worker> workers = workerRepository.findAll();

        return workers;
    }


@RestController
public class WorkerController {


    @RequestMapping(
            value = "/api/workers",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Collection<Worker>> getWorkers() {


        Worker worker = Guice.createInjector(new NotOnWeekendsModule()).getInstance(Worker.class);

        Collection<Worker> worker = worker.findAll(); // return null 

        ....
    }

0 个答案:

没有答案