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.Menu; 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.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.web.PageableDefaults; 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 MenuController { @Autowired private BelajarRestfulService belajarRestfulService; private Logger logger = LoggerFactory.getLogger(this.getClass()); @RequestMapping("/menu/{id}") @ResponseBody public Menu findById(@PathVariable String id) { Menu x = belajarRestfulService.findMenuById(id); if (x == null) { throw new IllegalStateException(); } return x; } @RequestMapping(value = "/menu", method = RequestMethod.POST) @ResponseStatus(HttpStatus.CREATED) public void create(@RequestBody @Valid Menu 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 = "/menu/{id}") @ResponseStatus(HttpStatus.OK) public void update(@PathVariable String id, @RequestBody @Valid Menu x) { Menu a = belajarRestfulService.findMenuById(id); if (a == null) { logger.warn("Menu dengan id [{}] tidak ditemukan", id); throw new IllegalStateException(); } x.setId(a.getId()); belajarRestfulService.save(x); } @RequestMapping(method = RequestMethod.DELETE, value = "/menu/{id}") @ResponseStatus(HttpStatus.OK) public void delete(@PathVariable String id) { Menu a = belajarRestfulService.findMenuById(id); if (a == null) { logger.warn("Menu dengan id [{}] tidak ditemukan", id); throw new IllegalStateException(); } belajarRestfulService.delete(a); } @RequestMapping(value = "/menu", method = RequestMethod.GET) @ResponseBody public Page<Menu> findAll(Pageable pageable) { return belajarRestfulService.findAllMenu(pageable); } @ResponseStatus(HttpStatus.NOT_FOUND) @ExceptionHandler({ IllegalStateException.class }) public void handle() { logger.debug("Resource dengan URI tersebut tidak ditemukan"); } }