package org.springside.examples.showcase.webservice.jaxrs;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springside.examples.showcase.entity.User;
import org.springside.examples.showcase.service.AccountEffectiveService;
import org.springside.examples.showcase.webservice.rest.UserDTO;
import org.springside.modules.mapper.BeanMapper;

@Path("/user")
public class AccountJaxRsService {

	@Autowired
	private AccountEffectiveService accountService;

	@GET
	@Path("/{id}.xml")
	@Produces(MediaType.APPLICATION_XML)
	public UserDTO getAsXml(@PathParam("id") Long id) {
		User user = accountService.getUser(id);
		return bindDTO(user);
	}

	@GET
	@Path("/{id}.json")
	@Produces(MediaType.APPLICATION_JSON)
	public UserDTO getAsJson(@PathParam("id") Long id) {
		User user = accountService.getUser(id);
		return bindDTO(user);
	}

	private UserDTO bindDTO(User user) {
		UserDTO dto = BeanMapper.map(user, UserDTO.class);
		//补充Dozer不能自动绑定的属性
		dto.setTeamId(user.getTeam().getId());
		return dto;
	}
}

@@ -184,24 +184,200 @@
 .rs.
-core.MediaType;%0A
+WebApplicationException;%0Aimport javax.ws.rs.core.MediaType;%0Aimport javax.ws.rs.core.Response;%0Aimport javax.ws.rs.core.Response.Status;%0A%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0Aimp
@@ -728,16 +728,101 @@
 vice %7B%0A%0A
+%09private static Logger logger = LoggerFactory.getLogger(AccountJaxRsService.class);%0A%0A
 %09@Autowi
@@ -1026,32 +1026,154 @@
 ce.getUser(id);%0A
+%09%09if (user == null) %7B%0A%09%09%09String message = %22%E7%94%A8%E6%88%B7%E4%B8%8D%E5%AD%98%E5%9C%A8(id:%22 + id + %22)%22;%0A%09%09%09throw buildException(Status.NOT_FOUND, message);%0A%09%09%7D%0A
 %09%09return bindDTO
@@ -1346,16 +1346,138 @@
 er(id);%0A
+%09%09if (user == null) %7B%0A%09%09%09String message = %22%E7%94%A8%E6%88%B7%E4%B8%8D%E5%AD%98%E5%9C%A8(id:%22 + id + %22)%22;%0A%09%09%09throw buildException(Status.NOT_FOUND, message);%0A%09%09%7D%0A
 %09%09return
@@ -1591,16 +1591,17 @@
 s);%0A%09%09//
+ 
 %E8%A1%A5%E5%85%85Dozer%E4%B8%8D
@@ -1666,11 +1666,210 @@
  dto;%0A%09%7D
+%0A%0A%09private WebApplicationException buildException(Status status, String message) %7B%0A%09%09return new WebApplicationException(Response.status(status).entity(message).type(MediaType.TEXT_PLAIN).build());%0A%09%7D
 %0A%7D%0A

update jax-rs demo