如何使用 HttpStatus 创建异常并使用 Spring-Hateoas 的资源发送它?

How to create Exception with HttpStatus and send it using Resource of Spring-Hateoas?

我正在使用 Spring-Boot 1.2.7 开发带有 Spring-Data-JPA 的 Spring-Hateoas 应用程序。 我已经使用 returns Resource.

方法开发了控制器 class

我想用 HttpStatus 创建异常,并在控制器 class 中使用它进行 GET、POST、PUT 和 DELETE。请帮助我,我是新手。

控制器Class - ArticleController

@RestController
@RequestMapping(value = "/api/articles")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private ArticleRepository articleRepository;


    @Autowired
    private ArticleResourceAssembler articleResourceAssembler;

   /*@RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public Collection<Resource<Article>> getArticles() {
        Collection<Article> articles = articleService.findAll();
        List<Resource<Article>> resources = new ArrayList<Resource<Article>>();

        for (Article article : articles) {
            resources.add(getArticleResource(article));
        }
        return resources;
    }*/


   @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public PagedResources<Article> getArticles(Pageable pageable, PagedResourcesAssembler assembler) {
        Page<Article> articles = articleService.findAll(pageable);

        return assembler.toResource(articles, articleResourceAssembler);
    }

    @RequestMapping(value = "/{article_id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public Resource<Article> getArticle(@PathVariable(value = "article_id") long article_id) {

        Article article = articleService.findOne(article_id);
        if (article == null) {
            ResponseEntity.status(HttpStatus.NOT_FOUND);
        }
        return getArticleResource(article);
    }


    // Insert Article
    @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE,
                    produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Article> createArtilce(@RequestBody Article article) {
        article.setCreated(new Date());
        Article savedArticle = articleService.create(article);
        article.add(linkTo(methodOn(ArticleController.class).getArticle(savedArticle.getArticle_id()))
                .withSelfRel());
        // I want to return here HttpStatus.NOT_FOUND
    }

    private Resource<Article> getArticleResource(Article article) {

        Resource<Article> resource = new Resource<Article>(article);
        // Link to Article
        resource.add(linkTo(methodOn(ArticleController.class).getArticle(article.getArticle_id())).withSelfRel());
        return resource;

    }


}

您需要为异常(从 RuntimeException 扩展)实现一个 class 并用 @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) 或其他状态代码进行注释,以及您想要抛出该状态的位置,你需要抛出 CustomException。

对于您没有编写的异常,可以在您的控制器中使用异常处理程序方法,如下所示:

      @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
      @ExceptionHandler({ExternalException.class})
      public void metodoCuandoExcepcionEsLanzada(){
           //logging and processing
     }