Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.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、Spring中)_Java_Spring - Fatal编程技术网

干涸控制器-返回实体或重定向的方法(在Java、Spring中)

干涸控制器-返回实体或重定向的方法(在Java、Spring中),java,spring,Java,Spring,我有一个控制器,它有一些方法可以从服务中获取可选的实体,检查是否存在,并继续执行一些其他操作,或者重定向消息“entity not found” 看起来是这样的: @GetMapping("action") public String method(@PathVariable Long id, final RedirectAttributes redirectAttributes){ Optional<Entity> eOpt = e

我有一个控制器,它有一些方法可以从服务中获取可选的实体,检查是否存在,并继续执行一些其他操作,或者重定向消息“entity not found”

看起来是这样的:

@GetMapping("action")
public String method(@PathVariable Long id,
                     final RedirectAttributes redirectAttributes){
    Optional<Entity> eOpt = entityService.findById(id);
    if(eOpt.isEmpty()){
        alertHandler.set(redirectAttributes, Status.ENTITY_NOT_FOUND);
        return "redirect:/entity/list"
    }
    Entity e = eOpt.get();

    // other actions that are using e

    return "view-name";
}

或者你有不同的方法来处理这个问题。或者可能就是这样,你必须重复一遍……

你可以把空的
可选的
视为例外情况

在这种情况下,您可以提供自己的
RuntimeException
包含重定向路径

public class EntityNotFoundException extends RuntimeException {

    private final String fallbackView;

    public EntityNotFoundException(final String fallbackView) {
        this.fallbackView = fallbackView;
    }

    public String getFallbackView() {
        return fallbackView;
    }

然后向控制器类提供一个用
@ExceptionHandler
注释的方法(或者如果这种情况对于多个控制器很常见,则向用
@ControllerAdvice
注释的类提供这种方法)。您的异常处理程序应该捕获刚刚定义的异常并执行重定向

@ExceptionHandler(EntityNotFoundException.class)
public String redirectOnEntityNotFoundException(final EntityNotFoundException exception, 
                                                final RedirectAttributes redirectAttributes) {
    alertHandler.set(redirectAttributes, Status.ENTITY_NOT_FOUND);
    return exception.getFallbackView();
}
最后,您实现了某种类型的
getEntityOrRedirect
。现在,您可以按如下方式使用上述设置:

@GetMapping("action")
public String method(@PathVariable Long id){

    Entity e = entityService.findById(id)
                            .orElseThrow(() -> new EntityNotFoundException("redirect:/entity/list"));

    // other actions that are using e

    return "view-name";
}
代码未经测试,请提前为输入错误道歉


注意我相信它对Spring>=4.3.5有效,否则
重定向属性
将无法为
@ExceptionHandler
(如上所述)

解决。我会很快尝试一下,如果有效,我会告诉你。干杯工作起来很有魅力!非常感谢你。我唯一改变的是,我用一个空构造函数重载了异常的构造函数,该构造函数将回退分配给默认构造函数。对于在这里绊倒的人,它看起来是这样的:public EntityNotFound(){this.fallbackView=“redirect:/default fallback page”},所以我仍然可以选择使用您的方法执行自定义重定向,但由于它通常重定向到一个位置,现在我可以抛出异常,引用如下:Entity e=entityService.findById(id).OrelsThrow(EntityNotFoundException::new);再次感谢
@ExceptionHandler(EntityNotFoundException.class)
public String redirectOnEntityNotFoundException(final EntityNotFoundException exception, 
                                                final RedirectAttributes redirectAttributes) {
    alertHandler.set(redirectAttributes, Status.ENTITY_NOT_FOUND);
    return exception.getFallbackView();
}
@GetMapping("action")
public String method(@PathVariable Long id){

    Entity e = entityService.findById(id)
                            .orElseThrow(() -> new EntityNotFoundException("redirect:/entity/list"));

    // other actions that are using e

    return "view-name";
}