Java tutorial
/******************************************************************************* * Copyright (c) 2005, 2014 * * Licensed under the Apache License, Version 2.0 (the "License"); *******************************************************************************/ package com.xyxy.platform.examples.showcase.webservice.rest; import javax.annotation.PostConstruct; import com.xyxy.platform.examples.showcase.entity.User; import com.xyxy.platform.examples.showcase.service.AccountEffectiveService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.xyxy.platform.modules.core.mapper.BeanMapper; import com.xyxy.platform.modules.metrics.Timer; import com.xyxy.platform.modules.metrics.Timer.TimerContext; import com.xyxy.platform.modules.metrics.MetricRegistry; /** * Shiro?/api/secure/**?authBasic?. * * */ @RestController @RequestMapping(value = { "/api/v1/user", "/api/secure/v1/user" }) public class UserRestController { private static Logger logger = LoggerFactory.getLogger(UserRestController.class); @Autowired private AccountEffectiveService accountService; private Timer executionMetrics; @PostConstruct public void register() { executionMetrics = MetricRegistry.INSTANCE.timer("REST.GetUser"); } /** * ContentNegotiationManager,?URL???? * eg. /api/v1/user/1.xml xml * /api/v1/user/1.json json * /api/v1/user/1 xml(why?) */ @RequestMapping(value = "/{id}", method = RequestMethod.GET) public UserDTO getUser(@PathVariable("id") Long id) { final TimerContext exectuionTimer = executionMetrics.start(); try { User user = accountService.getUser(id); if (user == null) { String message = "?(id:" + id + ")"; logger.warn(message); throw new RestException(HttpStatus.NOT_FOUND, message); } // Dozer?DTODozer? UserDTO dto = BeanMapper.map(user, UserDTO.class); dto.setTeamId(user.getTeam().getId()); return dto; } finally { exectuionTimer.stop(); } } }