Announcement Announcement Module
Collapse
No announcement yet.
@PathVariable with custom formatters Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @PathVariable with custom formatters

    I have a domain object called CustomerShard which is a Hibernate entity that I can look up in the database by ID via DAO. I defined and registered a custom formatter for this class so that I can translate them from String id values in forms.

    Code:
    @Component
    public class CustomerShardFormatter implements Formatter<CustomerShard> {
    
    	@Override
    	public String print(CustomerShard shard, Locale locale) {
    		return shard.getDescription();
    	}
    
    	@Override
    	public CustomerShard parse(String id, Locale locale) throws ParseException {
    		try {
    			Long shardId = Long.parseLong(id);
    			return this.customerShardDao.findById(shardId);
    		}
    		catch (NumberFormatException ex) {
    			throw new ParseException(ex.getMessage(), 0);
    		}
    	}
    
    	@Autowired
    	private CustomerShardDao customerShardDao;
    }
    Now I have a CRUD controller for this class and an update form that I'm trying to map using path variables like this:

    Code:
    @RequestMapping(value = "/{shard}/Edit.html", method = RequestMethod.GET)
    public String initUpdateForm(@PathVariable("shard") CustomerShard shard, Model model, RedirectAttributes redirectAttributes) {
    	model.addAttribute(shard);
    	return "Shards/Admin/Shards/Edit";
    }
    The {shard} @PathVariable gets converted to a CustomerShard instance via my formatter which is great. However, I would like to more gracefully handle the case when the id value in the path can not be mapped to a real CustomerShard instance. In such a case, my formatter's parse() method returns null (which is legal as far as I can tell) and the request results in an HTTP-400 status error. I would instead like the opportunity to handle this exception and redirect the user somewhere else, along with an error message. However, I cannot seem to figure out the recommended way to handle that.

    I understand that I could register a custom handler/view for HTTP-400 errors but that would cover every HTTP-400 in the application, when I'm just interested in handling them for THIS particular request.

    I could also get around this, of course, by changing my design to not use the entity @PathVariable, like this:

    Code:
    @RequestMapping(value = "/{id}/Edit.html", method = RequestMethod.GET)
    public String initUpdateForm(@PathVariable("id") long shardId, Model model, RedirectAttributes redirectAttributes) {
    	CustomerShard shard = this.customerShardDao.findById(shardId);
    	if (shard == null) {
    		WebUtils.setFlashError(redirectAttributes, "No such shard");
    		return "redirect:/Shards/Admin/Shards/List.html";
    	}
    	model.addAttribute(shard);
    	return "Shards/Admin/Shards/Edit";
    }
    However, that just seems like I'm dodging the issue rather than really understanding how to use the framework. So what would you all recommend?
Working...
X