Discussion:
Guice module integration issue with REST
pradrone dev
2017-04-23 09:04:53 UTC
Permalink
Guice module integration issue with REST I have define one AOP guice based
module, but when I tried to integrate with REST code,
methodInvocation.proceed retun null. What might be best way to solve this
issue.

Define AOP Guice based module as below

@Retention <https://github.com/Retention>(RetentionPolicy.RUNTIME)
@Target <https://github.com/Target>(ElementType.METHOD)
@interface <https://github.com/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 <https://github.com/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

....
}
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/cb641bf9-7ada-4021-aa73-20cfe1e15787%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Stephan Classen
2017-04-24 07:57:21 UTC
Permalink
I think you problem is that you are mixing spring an guice (in an
inappropriate way).

I reproduced your above example (by the way, can you tell me from which
library you use Worker and WorkerRepository?).

The code is missing the binding for the worker. I added it.

Next problem is the @Autowire. This is a spring annotation and Guice
will not react to it.
If you replace it with @Inject the sample code is working as expected.
(By the way I would recommend to favor constructor injection over field
injection).

If this does not help. I would suggest you create a minimal reproducible
case and publish it on a github repo so we can fiddle around with it.
Post by pradrone dev
Guice module integration issue with REST I have define one AOP guice
based module, but when I tried to integrate with REST code,
methodInvocation.proceed retun null. What might be best way to solve
this issue.
Define AOP Guice based module as below
@Retention <https://github.com/Retention>(RetentionPolicy.RUNTIME)
@Target <https://github.com/Target>(ElementType.METHOD)
@interface <https://github.com/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
Collection<Worker> findAll() { Collection<Worker> workers =
workerRepository.findAll(); return workers; } |
@RestController <https://github.com/RestController>
public class WorkerController {
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 .... }|
--
You received this message because you are subscribed to the Google
Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-guice/cb641bf9-7ada-4021-aa73-20cfe1e15787%40googlegroups.com
<https://groups.google.com/d/msgid/google-guice/cb641bf9-7ada-4021-aa73-20cfe1e15787%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/33550657-d897-01db-58c2-e9d99c50b8b2%40gmx.ch.
For more options, visit https://groups.google.com/d/optout.
pradrone dev
2017-04-24 15:54:41 UTC
Permalink
Thanks it does work but now methodInvocation.proceed return [] (empty
object)
Post by pradrone dev
Guice module integration issue with REST I have define one AOP guice based
module, but when I tried to integrate with REST code,
methodInvocation.proceed retun null. What might be best way to solve this
issue.
Define AOP Guice based module as below
@Retention <https://github.com/Retention>(RetentionPolicy.RUNTIME)
@Target <https://github.com/Target>(ElementType.METHOD)
@interface <https://github.com/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 <https://github.com/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
....
}
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/3b14c34f-68b3-47fa-9bcd-d6af73c464dc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
pradrone dev
2017-04-24 15:56:21 UTC
Permalink
Thanks it does work but now methodInvocation.proceed return [] (empty
object) where it should return list values.
Any idea for such behaviour ?
Post by pradrone dev
Guice module integration issue with REST I have define one AOP guice based
module, but when I tried to integrate with REST code,
methodInvocation.proceed retun null. What might be best way to solve this
issue.
Define AOP Guice based module as below
@Retention <https://github.com/Retention>(RetentionPolicy.RUNTIME)
@Target <https://github.com/Target>(ElementType.METHOD)
@interface <https://github.com/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 <https://github.com/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
....
}
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/fb70b5e4-27e5-4296-95ef-06c84fc6c0fb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Stephan Classen
2017-04-24 17:08:44 UTC
Permalink
Set a breakpoint or add a log statement at the end of the WorkerBean.findAll() to see what this method returns and if it is differen from what you see returned by the methodInvocation.proceed call
Post by pradrone dev
Thanks it does work but now methodInvocation.proceed return [] (empty
object) where it should return list values.
Any idea for such behaviour ?
Post by pradrone dev
Guice module integration issue with REST I have define one AOP guice
based
Post by pradrone dev
module, but when I tried to integrate with REST code,
methodInvocation.proceed retun null. What might be best way to solve
this
Post by pradrone dev
issue.
Define AOP Guice based module as below
@Retention <https://github.com/Retention>(RetentionPolicy.RUNTIME)
@Target <https://github.com/Target>(ElementType.METHOD)
@interface <https://github.com/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"))
{
Post by pradrone dev
throw new IllegalStateException(
invocation.getMethod().getName() + " not allowed on
weekends!");
Post by pradrone dev
}
return invocation.proceed();
}
}
public class NotOnWeekendsModule extends AbstractModule {
protected void configure() {
bindInterceptor(Matchers.any(),
Matchers.annotatedWith(NotOnWeekends.class),
Post by pradrone dev
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 <https://github.com/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);
Post by pradrone dev
Collection<Worker> worker = worker.findAll(); // return null
....
}
--
You received this message because you are subscribed to the Google
Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-guice/fb70b5e4-27e5-4296-95ef-06c84fc6c0fb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/E13AB7A0-169D-447E-A951-F3BB32AB9182%40gmx.ch.
For more options, visit https://groups.google.com/d/optout.
pradrone dev
2017-04-25 07:49:55 UTC
Permalink
Can you please guide me steps how to Integrate Guice with Spring ? Tried
few more stuff but it is unclear ?

For your reference attaching sample problem. Waiting for you suggestion and
inputs.

Guice Integration .
// Collection<Greeting> greetings = greetingService.findAll();

GreetingServiceBean greetingservicebean =
InjectorInstance.getInstance().getInstance(GreetingServiceBean.class);

Collection<Greeting> greetings = greetingservicebean.findAll();

Thanks Alot,
Post by Stephan Classen
Set a breakpoint or add a log statement at the end of the
WorkerBean.findAll() to see what this method returns and if it is differen
from what you see returned by the methodInvocation.proceed call
Post by pradrone dev
Thanks it does work but now methodInvocation.proceed return [] (empty
object) where it should return list values.
Any idea for such behaviour ?
Post by pradrone dev
Guice module integration issue with REST I have define one AOP guice
based module, but when I tried to integrate with REST code,
methodInvocation.proceed retun null. What might be best way to solve this
issue.
Define AOP Guice based module as below
@Retention <https://github.com/Retention>(RetentionPolicy.RUNTIME)
@Target <https://github.com/Target>(ElementType.METHOD)
@interface <https://github.com/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 <https://github.com/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
....
}
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/ee30385f-bf60-47ba-af5e-4f5af1bdfe2e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Stephan Classen
2017-04-25 10:04:02 UTC
Permalink
Sorry I have no Spring experience what so ever. So I guess I will not be
of any help here.

Hopefully someone else can jump in.
Post by pradrone dev
Can you please guide me steps how to Integrate Guice with Spring ?
Tried few more stuff but it is unclear ?
For your reference attaching sample problem. Waiting for you
suggestion and inputs.
Guice Integration .
// Collection<Greeting> greetings = greetingService.findAll();
GreetingServiceBean greetingservicebean =
InjectorInstance.getInstance().getInstance(GreetingServiceBean.class);
Collection<Greeting> greetings = greetingservicebean.findAll();
Thanks Alot,
Set a breakpoint or add a log statement at the end of the
WorkerBean.findAll() to see what this method returns and if it is
differen from what you see returned by the
methodInvocation.proceed call
Am 24. April 2017 17:56:21 MESZ schrieb pradrone dev
Thanks it does work but now methodInvocation.proceed return []
(empty object) where it should return list values.
Any idea for such behaviour ?
Guice module integration issue with REST I have define one
AOP guice based module, but when I tried to integrate with
REST code, methodInvocation.proceed retun null. What might
be best way to solve this issue.
Define AOP Guice based module as below
@Retention
<https://github.com/Retention>(RetentionPolicy.RUNTIME)
@Target <https://github.com/Target>(ElementType.METHOD)
@interface <https://github.com/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
@NotOnWeekends public Collection<Worker> findAll() {
Collection<Worker> workers = workerRepository.findAll();
return workers; } |
@RestController <https://github.com/RestController> public
class WorkerController {
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 .... }|
-- You received this message because you are subscribed to the Google
Groups "google-guice" group. To unsubscribe from this group and stop
receiving emails from it, send an email to
https://groups.google.com/group/google-guice. To view this discussion
on the web visit
https://groups.google.com/d/msgid/google-guice/ee30385f-bf60-47ba-af5e-4f5af1bdfe2e%40googlegroups.com
<https://groups.google.com/d/msgid/google-guice/ee30385f-bf60-47ba-af5e-4f5af1bdfe2e%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+***@googlegroups.com.
To post to this group, send email to google-***@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/b27d492f-244c-7ba2-c189-7603c3d539e6%40gmx.ch.
For more options, visit https://groups.google.com/d/optout.
Loading...