如何让Spring 3.0控制器触发404?

我有一个控制器@RequestMapping(值= "/**",方法= RequestMethod.GET)和一些访问控制器的url,我希望容器提出一个404。


重写你的方法签名,使它接受HttpServletResponse作为参数,这样你就可以对它调用setStatus(int)。

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-arguments


从Spring 3.0开始,你还可以抛出一个用@ResponseStatus注释声明的Exception:

@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException {
    ...
}

@Controller
public class SomeController {
    @RequestMapping.....
    public void handleCall() {
        if (isFound()) {
            // whatever
        }
        else {
            throw new ResourceNotFoundException(); 
        }
    }
}

我想提一下,Spring默认提供了404异常(不仅是)。有关详细信息,请参阅Spring文档。所以如果你不需要自己的异常,你可以简单地这样做:

 @RequestMapping(value = "/**", method = RequestMethod.GET)
 public ModelAndView show() throws NoSuchRequestHandlingMethodException {
    if(something == null)
         throw new NoSuchRequestHandlingMethodException("show", YourClass.class);

    ...

  }

简单地说,您可以使用web.xml添加错误代码和404错误页面。但是要确保404错误页面不能位于WEB-INF下面。

<error-page>
    <error-code>404</error-code>
    <location>/404.html</location>
</error-page>

这是最简单的方法,但也有局限性。假设您希望为该页添加与其他页相同的样式。这样你就不能那样做了。你必须使用@ResponseStatus(value = HttpStatus.NOT_FOUND)


如果你的控制器方法是用于文件处理,那么ResponseEntity是非常方便的:

@Controller
public class SomeController {
    @RequestMapping.....
    public ResponseEntity handleCall() {
        if (isFound()) {
            return new ResponseEntity(...);
        }
        else {
            return new ResponseEntity(404);
        }
    }
}

使用setting配置web.xml

<error-page>
    <error-code>500</error-code>
    <location>/error/500</location>
</error-page>

<error-page>
    <error-code>404</error-code>
    <location>/error/404</location>
</error-page>

创建新控制器

   /**
     * Error Controller. handles the calls for 404, 500 and 401 HTTP Status codes.
     */
    @Controller
    @RequestMapping(value = ErrorController.ERROR_URL, produces = MediaType.APPLICATION_XHTML_XML_VALUE)
    public class ErrorController {


        /**
         * The constant ERROR_URL.
         */
        public static final String ERROR_URL = "/error";


        /**
         * The constant TILE_ERROR.
         */
        public static final String TILE_ERROR = "error.page";


        /**
         * Page Not Found.
         *
         * @return Home Page
         */
        @RequestMapping(value = "/404", produces = MediaType.APPLICATION_XHTML_XML_VALUE)
        public ModelAndView notFound() {

            ModelAndView model = new ModelAndView(TILE_ERROR);
            model.addObject("message", "The page you requested could not be found. This location may not be current.");

            return model;
        }

        /**
         * Error page.
         *
         * @return the model and view
         */
        @RequestMapping(value = "/500", produces = MediaType.APPLICATION_XHTML_XML_VALUE)
        public ModelAndView errorPage() {
            ModelAndView model = new ModelAndView(TILE_ERROR);
            model.addObject("message", "The page you requested could not be found. This location may not be current, due to the recent site redesign.");

            return model;
        }
}

你可以使用@ControllerAdvice来处理异常, 默认行为@ControllerAdvice注释类将帮助所有已知的控制器。

因此,当任何控制器抛出404错误时,它将被调用。

像下面这样:

@ControllerAdvice
class GlobalControllerExceptionHandler {
    @ResponseStatus(HttpStatus.NOT_FOUND)  // 404
    @ExceptionHandler(Exception.class)
    public void handleNoTFound() {
        // Nothing to do
    }
}

并将此404响应错误映射到web.xml中,如下所示:

<error-page>
        <error-code>404</error-code>
        <location>/Error404.html</location>
</error-page>

希望能有所帮助。


从Spring 3.0.2开始,你可以返回ResponseEntity<T>作为控制器方法的结果:

@RequestMapping.....
public ResponseEntity<Object> handleCall() {
    if (isFound()) {
        // do what you want
        return new ResponseEntity<>(HttpStatus.OK);
    }
    else {
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
}

(ResponseEntity<T>是一个比@ResponseBody注释更灵活的注释-参见另一个问题)


我建议抛出HttpClientErrorException,就像这样

@RequestMapping(value = "/sample/")
public void sample() {
    if (somethingIsWrong()) {
        throw new HttpClientErrorException(HttpStatus.NOT_FOUND);
    }
}

您必须记住,这只能在将任何内容写入servlet输出流之前进行。


虽然标记的答案是正确的,但有一种方法可以做到这一点,没有例外。服务返回搜索对象的Optional<T>,这被映射到HttpStatus。如果找到OK,如果为空则返回404。

@Controller
public class SomeController {

    @RequestMapping.....
    public ResponseEntity<Object> handleCall(@PathVariable String param) {
        return  service.find(param)
                .map(result -> new ResponseEntity<>(result, HttpStatus.OK))
                .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }
}

@Service
public class Service{
  
    public Optional<Object> find(String param){
        if(!found()){
            return Optional.empty();
        }
        ...
        return Optional.of(data); 
    }
    
}

如果你想从控制器返回404状态,你只需要这样做

@RequestMapping(value = "/something", method = RequestMethod.POST)
@ResponseBody
public HttpStatus doSomething(@RequestBody String employeeId) {
    try {
        return HttpStatus.OK;
    } 
    catch (Exception ex) { 
         return HttpStatus.NOT_FOUND;
    }
}

通过这样做,当您想从控制器返回404时,您将收到一个404错误。


这有点晚了,但如果你正在使用Spring Data REST,那么已经有org.springframework.data.rest.webmvc.ResourceNotFoundException 它还使用@ResponseStatus注释。不再需要创建自定义运行时异常。


从Spring 5.0开始,你不需要创建额外的异常:

throw new ResponseStatusException(NOT_FOUND, "Unable to find resource");

此外,你可以用一个内置异常覆盖多个场景,你有更多的控制。

看到更多:

ResponseStatusException (javadoc) https://www.baeldung.com/spring-response-status-exception


因为做同一件事至少有十种方法总是好的:

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class Something {
    @RequestMapping("/path")
    public ModelAndView somethingPath() {
        return new ModelAndView("/", HttpStatus.NOT_FOUND);
    }
}