0

在我的一个用例中,我想从 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

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

4

1 回答 1

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("/") public class 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);
}

}

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

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

于 2019-02-25T13:48:52.250 回答