0

I am using Spring-Boot 1.2.7 for developing Spring-Hateoas application with Spring-Data-JPA. I have developed controller class with methods which returns Resource.

I want to create Exception with HttpStatus and use it in controller class for GET, POST, PUT and DELETE. Please assist me, I am new to this.

Controller 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;

    }


}
Mogsdad
  • 44,709
  • 21
  • 151
  • 275

1 Answers1

0

You need to implement a class for the exception (that is extends from RuntimeException) and annotated with @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) or another Status code, and where you want throw that status, you need throw your CustomException.

For Exceptions you didn't write, can use a exception handler method inside your controller like this:

      @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
      @ExceptionHandler({ExternalException.class})
      public void metodoCuandoExcepcionEsLanzada(){
           //logging and processing
     }
Javier Larios
  • 192
  • 2
  • 9