1
\$\begingroup\$

Model:

public class UserBean { 
 @JsonProperty
 private String userId; 
 @JsonProperty
 private int limit; 
 @JsonProperty
 private int page;
 public String getUserId() {
 return userId;
 }
 public void setUserId(String userId) {
 this.userId = userId;
 }
 public int getLimit() {
 return limit;
 }
 public void setLimit(int limit) {
 this.limit = limit;
 }
 public int getPage() {
 return page;
 }
 public void setPage(int page) {
 this.page = page;
 }
}
public class ResponseJSON {
 private Map<String, Object> response = new LinkedHashMap<String, Object>(); 
 public void setStatus(String status) {
 response.put("status", status);
 }
 public void setMessage(String message) {
 response.put("message", message);
 } 
 public void add(String key, Object value) {
 response.put(key, value);
 } 
 public Map<String, Object> get() {
 return this.response;
 }
}

Controller:

@ResponseBody
@RequestMapping(value = "/user/products", method = RequestMethod.POST)
public Object getProductListByUser(@RequestBody UserBean userBean) {
 ResponseJSON responseJSON = new ResponseJSON();
 try {
 List<Object> result = userRepository.getProductListByUser(userBean);
 int resultCount = result.size();
 List<Object> productList = new ArrayList<Object>();
 for (int i = 0; i < resultCount; i++) {
 ProductInfo product = (ProductInfo) result.get(i);
 Map<String, Object> productInfo = new LinkedHashMap<String, Object>(); 
 productInfo.put("id", product.getId());
 productInfo.put("title", product.getTitle());
 productInfo.put("description", product.getDescription());
 productInfo.put("iconImage", product.getIconImage()); 
 productInfo.put("updateDatetime", product.getUpdateDatetime());
 productInfo.put("createDatetime", product.getCreateDatetime());
 productList.add(productInfo);
 }
 responseJSON.setStatus(Constants.SUCCESS);
 responseJSON.add("items", productList); 
 } catch (Exception e) {
 responseJSON.setStatus(Constants.FAIL);
 responseJSON.setMessage(e.getMessage());
 logger.error(e.getMessage());
 }
 return responseJSON.get();
}

Repository:

@SuppressWarnings("unchecked")
@Transactional
public List<Object> getProductListByUser(UserBean userBean) {
 Session session = sessionFactory.getCurrentSession();
 Criteria criteria = session.createCriteria(ProductInfo.class, "product")
 .createAlias("product.productOptions", "options", CriteriaSpecification.LEFT_JOIN)
 .add(Restrictions.eq("product.userId", userBean.getUserId()))
 .addOrder(Order.asc("productOptions.size"))
 .setFirstResult((userBean.getPage() - 1) * userBean.getLimit());
 .setMaxResults(userBean.getLimit());
 return criteria.list(); 
}

I'm developing some back-end APIs. This API returns product lists of some user. Purpose of this API is returning result of JSON data. So, I get datas from repository and assign to ResponseJSON object in the controller. Is this approach efficient or not? Which way is better?

palacsint
30.3k9 gold badges81 silver badges157 bronze badges
asked Jan 26, 2012 at 1:15
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

It would be easier to follow the getProductListByUser method if you use two ResponseJSON instance. One for normal execution and one for the exceptional cases:

try {
 ...
 final ResponseJSON responseJSON = new ResponseJSON();
 ...
 responseJSON.setStatus(Constants.SUCCESS);
 responseJSON.add("items", productList); 
 return responseJSON.get();
} catch (final Exception e) {
 final ResponseJSON responseJSON = new ResponseJSON();
 responseJSON.setStatus(Constants.FAIL);
 responseJSON.setMessage(e.getMessage());
 logger.error(e.getMessage());
 return responseJSON.get();
}

It also ensures that nobody modifies accidentally the exception case result in the try block and there isn't any incomplete ProductInfo data in the error response.

answered Jan 26, 2012 at 9:13
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.