Java tutorial
/** * Copyright (C) 2011 ArtiVisi Intermedia <info@artivisi.com> * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package com.artivisi.belajar.restful.ui.controller; import com.artivisi.belajar.restful.domain.Permission; import com.artivisi.belajar.restful.service.BelajarRestfulService; import java.net.URI; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ExceptionHandler; 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; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.util.UriTemplate; @Controller public class PermissionController { @Autowired private BelajarRestfulService belajarRestfulService; private Logger logger = LoggerFactory.getLogger(this.getClass()); @RequestMapping("/permission/{id}") @ResponseBody public Permission findById(@PathVariable String id) { Permission x = belajarRestfulService.findPermissionById(id); if (x == null) { throw new IllegalStateException(); } return x; } @RequestMapping(value = "/permission", method = RequestMethod.POST) @ResponseStatus(HttpStatus.CREATED) public void create(@RequestBody @Valid Permission x, HttpServletRequest request, HttpServletResponse response) { belajarRestfulService.save(x); String requestUrl = request.getRequestURL().toString(); URI uri = new UriTemplate("{requestUrl}/{id}").expand(requestUrl, x.getId()); response.setHeader("Location", uri.toASCIIString()); } @RequestMapping(method = RequestMethod.PUT, value = "/permission/{id}") @ResponseStatus(HttpStatus.OK) public void update(@PathVariable String id, @RequestBody @Valid Permission x) { Permission a = belajarRestfulService.findPermissionById(id); if (a == null) { logger.warn("Permission dengan id [{}] tidak ditemukan", id); throw new IllegalStateException(); } x.setId(a.getId()); belajarRestfulService.save(x); } @RequestMapping(method = RequestMethod.DELETE, value = "/permission/{id}") @ResponseStatus(HttpStatus.OK) public void delete(@PathVariable String id) { Permission a = belajarRestfulService.findPermissionById(id); if (a == null) { logger.warn("Permission dengan id [{}] tidak ditemukan", id); throw new IllegalStateException(); } belajarRestfulService.delete(a); } @RequestMapping(value = "/permission", method = RequestMethod.GET) @ResponseBody public List<Permission> findAll(Pageable pageable, HttpServletResponse response) { return belajarRestfulService.findAllPermissions(pageable).getContent(); } @ResponseStatus(HttpStatus.NOT_FOUND) @ExceptionHandler({ IllegalStateException.class }) public void handle() { logger.debug("Resource dengan URI tersebut tidak ditemukan"); } }