Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.tamnd2.basicwebapp.rest.mvc; import com.tamnd2.basicwebapp.core.entities.Blog; import com.tamnd2.basicwebapp.core.entities.BlogEntry; import com.tamnd2.basicwebapp.core.services.BlogService; import com.tamnd2.basicwebapp.core.services.exceptions.BlogNotFoundException; import com.tamnd2.basicwebapp.core.services.util.BlogEntryList; import com.tamnd2.basicwebapp.core.services.util.BlogList; import com.tamnd2.basicwebapp.rest.exceptions.NotFoundException; import com.tamnd2.basicwebapp.rest.resources.BlogEntryListResource; import com.tamnd2.basicwebapp.rest.resources.BlogEntryResource; import com.tamnd2.basicwebapp.rest.resources.BlogListResource; import com.tamnd2.basicwebapp.rest.resources.BlogResource; import com.tamnd2.basicwebapp.rest.resources.asm.BlogEntryListResourceAsm; import com.tamnd2.basicwebapp.rest.resources.asm.BlogEntryResourceAsm; import com.tamnd2.basicwebapp.rest.resources.asm.BlogListResourceAsm; import com.tamnd2.basicwebapp.rest.resources.asm.BlogResourceAsm; import java.net.URI; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; /** * * @author tamnd2 */ @Controller @RequestMapping("/rest/blogs") public class BlogController { private final BlogService blogService; @Autowired public BlogController(BlogService blogService) { this.blogService = blogService; } @RequestMapping(method = RequestMethod.GET) @PreAuthorize("permitAll") public ResponseEntity<BlogListResource> findAllBlogs() { BlogList blogList = blogService.findAllBlogs(); BlogListResource blogListRes = new BlogListResourceAsm().toResource(blogList); return new ResponseEntity<BlogListResource>(blogListRes, HttpStatus.OK); } @RequestMapping(value = "/{blogId}", method = RequestMethod.GET) public ResponseEntity<BlogResource> getBlog(@PathVariable Long blogId) { Blog blog = blogService.findBlog(blogId); BlogResource res = new BlogResourceAsm().toResource(blog); return new ResponseEntity<BlogResource>(res, HttpStatus.OK); } @RequestMapping(value = "/{blogId}/blog-entries", method = RequestMethod.POST) public ResponseEntity<BlogEntryResource> createBlogEntry(@PathVariable Long blogId, @RequestBody BlogEntryResource sentBlogEntry) { BlogEntry createdBlogEntry = null; try { createdBlogEntry = blogService.createBlogEntry(blogId, sentBlogEntry.toBlogEntry()); BlogEntryResource createdResource = new BlogEntryResourceAsm().toResource(createdBlogEntry); HttpHeaders headers = new HttpHeaders(); headers.setLocation(URI.create(createdResource.getLink("self").getHref())); return new ResponseEntity<BlogEntryResource>(createdResource, headers, HttpStatus.CREATED); } catch (BlogNotFoundException e) { throw new NotFoundException(e); } } @RequestMapping(value = "/{blogId}/blog-entries") public ResponseEntity<BlogEntryListResource> findAllBlogEntries(@PathVariable Long blogId) { try { BlogEntryList list = blogService.findAllBlogEntries(blogId); BlogEntryListResource res = new BlogEntryListResourceAsm().toResource(list); return new ResponseEntity<BlogEntryListResource>(res, HttpStatus.OK); } catch (BlogNotFoundException exception) { throw new NotFoundException(exception); } } }