Announcement Announcement Module
Collapse
No announcement yet.
Spring MVC REST Service and Client. I am looking for a better way to deal with errors Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring MVC REST Service and Client. I am looking for a better way to deal with errors

    Spring MVC REST Service and Client. I am looking for a better way to deal with errors!

    I would like sometimes to return a errror message to the client or a status code but I dont know hiow. Can someone please tell me how to find a better way of dealing with errors and error messags for Spring REST Service and Client..

    Here is my service code:
    Code:
        @RequestMapping(value = "/{name}", method = RequestMethod.GET)
        @ResponseBody
        public User getName(@PathVariable String name, ModelMap model) throws ResourceNotFoundException
        {
    
            logger.debug("I am in the controller and got user name: " + name);
    
            /*
    
                Simulate a successful lookup for 2 users, this is where your real lookup code would go
    
             */
    
            if ("user2".equals(name))
            {
                return new User("User2 Real Name", name);
            }
    
            if ("user1".equals(name))
            {
                return new User("User1 Real Name", name);
            }
    
            throw new ResourceNotFoundException("User Is Not Found");
        }
    
    
        @ExceptionHandler(ResourceNotFoundException.class)
        @ResponseStatus(value = Httatus.NOT_FOUND)
        public String handleResourceNotFoundException(ResourceNotFoundException ex)
        {
            logger.warn("user requested a resource which didn't exist", ex);
            return "user requested a resource which didn't exist";
        }

    Now here is the code for the client:
    Code:
            Map<String, String> vars = new HashMap<String, String>();
            vars.put("name", "user1");
    
    
            /**
             *
             * Doing the REST call and then displaying the data/user object
             *
             */
            RestTemplate restTemplate = new RestTemplate(commons);
            restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
            restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
    
            try
            {
                User jsonreturn = restTemplate.getForObject("http://" + mRESTServer.getHost() + ":8080/json/{name}", User.class, vars);
                LOGGER.debug("return object:  " + jsonreturn.toString());
            }
            catch(Exception e)
            {
                LOGGER.error("error:  " + e.toString());
            }
    I want to find so way to return a status code and message if the user is not found.

  • #2
    You might want to take a look at this link. That describes some common scenarios, currently I'm working on 2 HandlerExceptionResolvers which implement 2 of the documented strategies (the proposed standard and the one published on Github).

    Comment

    Working...
    X