将参数从REST API应用程序属性传递到已注册的ApplicationEventListener

时间:2019-02-25 11:05:17

标签: spring-boot spring-jersey

在我的一个用例中,我想从Spring boot Rest API的应用程序资源属性中传递多个参数,并且应该由注册的ApplicationEventListener或Jersey的ApplicationEvent监听此参数。可以请一些身体建议我如何实现这一目标。以下是我的代码库

package com.learning;
import org.springframework.boot.context.ApplicationPidFileWriter;
import org.springframework.context.ConfigurableApplicationContext;
public class ABCApplication {

public static void main(String args[])
{
    ConfigurableApplicationContext context = 
    SpringApplication.run(ABCApplication.class, args);
    context.addApplicationListener(new ApplicationPidFileWriter());
    context.addApplicationListener(new TestingListener());
}
}

这是我的监听器,应触发该监听器并从Rest API接收这些参数。

  import org.glassfish.jersey.server.monitoring.ApplicationEvent;
  import org.glassfish.jersey.server.monitoring.ApplicationEventListener;

  public class TestingListener implements ApplicationEventListener {

  @Override
  public void onEvent(ApplicationEvent event) {

    if (event.getType() == 
   ApplicationEvent.Type.INITIALIZATION_APP_FINISHED) {
        final ResourceModel resourceModel = event.getResourceModel();
        final ResourceLogDetails logDetails = new 
       ResourceLogDetails();
        resourceModel.getResources().stream().forEach((resource) -> {    
       logDetails.addEndpointLogLines(getLinesFromResource(resource));
        });
        logDetails.log();
    }
}

附加我的资源

   @Api
   @Path("/resource")
   public static class MyResource {

    @GET
    @Path("/get/path")
    public String getMethod() {
        return "get";
    }

    @POST
    public void post(String entity) {
    }

}

所以我期望的输出是

  GET  resource/get/path
  POST resource

但这是我的业务逻辑,我想在这里获得更多的值,这些值应该来自应用程序资源属性文件。

1 个答案:

答案 0 :(得分:0)

好吧,因为对于我而言,尚不清楚您需要如何以及如何管理通风孔,我举一个标准的例子:

要在Spring Boot中发布事件,您可以创建一个管理发布的服务:

@Component
public class CustomSpringEventPublisherService {
    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    public void publishCustomEvent(final String message) {
        System.out.println("Publishing custom event. ");
        CustomSpringEvent customSpringEvent = new CustomSpringEvent(this, message);
        applicationEventPublisher.publishEvent(customSpringEvent);
    }
}

CustomSpingEvent是一个POJO,其中包含您的有效负载和源:

public class CustomSpringEvent extends ApplicationEvent {
    private String message;

    public CustomSpringEvent(Object source, String message) {
        super(source);
        this.message = message;
    }
    public String getMessage() {
        return message;
    }
}

然后您可以像这样实现侦听器:

@Component
public class AnnotationDrivenContextStartedListener {

    @EventListener
    public void handleContextStart(CustomSpringEvent event) {
        System.out.println("A custom event and his payload ->"+event.getMessage());
    }
}

现在,就您而言,您可以实现所示的侦听器,并在控制器中放置发布者服务: @RestController @RequestMapping(“ /”) 公共类YourController {

 private final CustomSpringEventPublisherService customSpringEventPublisherService;

public YourController(CustomSpringEventPublisherService customSpringEventPublisherService) {
    this.customSpringEventPublisherService = customSpringEventPublisherService;
}


@GetMapping(value = "/resource")
public ResponseEntity<PublicationDto> getResource() {
    // Create CustomSpringEvent
    CustomSpringEvent customSpringEvent = new CustomSpringEvent(...)

    this.customSpringEventPublisherService.publishCustomEvent(customSpringEvent);
}

}

这实际上是概念性工作,未经测试。

希望它可以帮助您了解其工作原理