Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 反应堆弹簧的异常处理_Java_Spring_Spring Mvc_Reactor_Project Reactor - Fatal编程技术网

Java 反应堆弹簧的异常处理

Java 反应堆弹簧的异常处理,java,spring,spring-mvc,reactor,project-reactor,Java,Spring,Spring Mvc,Reactor,Project Reactor,我用的是反应堆2和弹簧4。以下是我拥有的典型代码—一个消费者使用存储库 @Consumer public class ApplicationService { @Selector(value="/applications/id", type = SelectorType.URI) @ReplyTo public Application byApplicationId(String id) throws ApplicationNotFoundException {

我用的是反应堆2和弹簧4。以下是我拥有的典型代码—一个
消费者
使用存储库

@Consumer
public class ApplicationService {

   @Selector(value="/applications/id", type = SelectorType.URI)
   @ReplyTo
   public Application byApplicationId(String id) throws ApplicationNotFoundException {
      Application app = appRepo.findOne(id);
      if(app == null) 
        throw new ApplicationNotFoundException("Application `" + id + "` could not be found.");
      return app;
   }
}
然后我有一个控制器,它将请求传递到
eventBus
,我将请求传递到该总线并返回一个
Promise

@RestController
@RequestMapping("/applications")
public class ApplicationsController {
   @RequestMapping(value = "/{id}", method = GET, produces = APPLICATION_JSON_VALUE)
   public Promise<Event<Application>> byApplicationId(@PathVariable final String id) {
      final Promise<Event<Application>> p = Promises.prepare(env);
      eventBus.sendAndReceive("/applications/id", Event.wrap(id), p);
      return p;
   }

}
问题是:

  • 我是否以错误的方式使用Reactor和
    eventBus
    ?如果是这样,正确的方法是什么

  • 也许这个功能还没有实现


  • 我想我在Spring应用程序中重新评估了使用Reactor的策略

    现在我的控制器看起来像

    @RestController
    public class GreetingController {
    
        @Autowired
        private GreetingService greetingService;
    
        @RequestMapping("/greeting")
        public Promise<ResponseEntity<?>> greeting(final @RequestParam(value = "name", defaultValue = "World") String name) {
            return greetingService.provideGreetingFor(name).map(new Function<Greeting, ResponseEntity<?>>() {
                @Override
                public ResponseEntity<?> apply(Greeting t) {
                    return new ResponseEntity<>(t, HttpStatus.OK);
                }
            }).onErrorReturn(WrongNameException.class, new Function<WrongNameException, ResponseEntity<?>>() {
                @Override
                public ResponseEntity<?> apply(WrongNameException t) {
                    return new ResponseEntity<>(t.getMessage(), HttpStatus.BAD_REQUEST);
                }
            }).next();
        }
    }
    
    @RestController
    公共类迎宾控制器{
    @自动连线
    私人迎宾服务迎宾服务;
    @请求映射(“/greeting”)
    
    public Promise这里有一章专门介绍如何处理被动引用中的异常和错误:


    在任何情况下,反应性管道在其中性意义上是“连续的”。您几乎无法阻止它被方法的使用者注意到。

    eventBus.sendandereceive(“/applications/id”,Event.wrap(id),p)
    这不会导致强制转换错误?@AnadiMisra在什么时候?出于好奇尝试了你的代码,我得到了这个
    类型EventBus中的sendAndReceive(Object,Event,Consumer)方法不适用于参数(String,Event,Promise)
    在那一行,我的Promise对象
    Promise response=Promises.prepare(env)发生这种情况是因为
    Promise
    变量中的
    T
    应该扩展
    事件。可能需要更大的上下文,但您可能可以使用原始类型
    Promise p=Promises.prepare(env)
    @RestController
    public class GreetingController {
    
        @Autowired
        private GreetingService greetingService;
    
        @RequestMapping("/greeting")
        public Promise<ResponseEntity<?>> greeting(final @RequestParam(value = "name", defaultValue = "World") String name) {
            return greetingService.provideGreetingFor(name).map(new Function<Greeting, ResponseEntity<?>>() {
                @Override
                public ResponseEntity<?> apply(Greeting t) {
                    return new ResponseEntity<>(t, HttpStatus.OK);
                }
            }).onErrorReturn(WrongNameException.class, new Function<WrongNameException, ResponseEntity<?>>() {
                @Override
                public ResponseEntity<?> apply(WrongNameException t) {
                    return new ResponseEntity<>(t.getMessage(), HttpStatus.BAD_REQUEST);
                }
            }).next();
        }
    }
    
    @Service
    public class GreetingService {
        @Autowired
        private Environment env;
    
        private static final String template = "Hello, %s!";
        private final AtomicLong counter = new AtomicLong();
    
        public Stream<Greeting> provideGreetingFor(String name) {
            return Streams.just(name).dispatchOn(env).map(new Function<String, Greeting>() {
                @Override
                public Greeting apply(String t) {
                    if (t == null || t.matches(".*\\d+.*"))
                        throw new WrongNameException();
                    return new Greeting(counter.incrementAndGet(), String.format(template, t));
                }
            });
        }
    }