Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8618623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:09:07+00:00 2026-06-12T06:09:07+00:00

Background I have an error message class: @XmlRootElement public class ErrorMessage { private String

  • 0

Background

I have an error message class:

@XmlRootElement
public class ErrorMessage {
    private String message;

    public ErrorMessage() {
    }

    public ErrorMessage(String message) {
        this.message = message;
    }

    public String getError() {
        return message;
    }

    public void setError(String message) {
        this.message = message;
    }
}

This class has been assigned as a return value to an @ExceptionHandler in my Spring MVC REST controller:

@ExceptionHandler
@ResponseStatus(HttpStatus.NOT_FOUND)
@ResponseBody
ErrorMessage handleException(RuntimeException e) {
    return new ErrorMessage("something went wrong");
}

Whenever the client triggers a RuntimeException after issuing a request with application/json as a Accept header, it receives a response with the correct status code and a matching JSON body:

{"error":"something went wrong"}

Alternatively, an XML body is received if the Accept header is application/xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<errorMessage><error>something went wrong</error></errorMessage>

Problem

Now, I would like to generify this solution by implementing a HandlerExceptionResolver instead so I don’t have to copy / paste the @ExceptionHandler to every controller (or create a common "parent controller" that the other controllers can extend).

However, the AbstractHandlerExceptionResolver.doResolveException() method returns a ModelAndView and not my propriety ErrorMessage, so I tried the following:

public class RuntimeExceptionHandlerExceptionResolver extends AbstractHandlerExceptionResolver {

    @Override
    protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        if (ex instanceof RuntimeException) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            ModelAndView mav = new ModelAndView();
            mav.addObject("error", "something went wrong");
            return mav;
        }
        return null;
    }
}

When debugging, I can see that the mav.addObject() method is called. The response on the client side has the expected status code, but the content type is text/html with html in the body, rather than the JSON or XLM content that was specified by the Accept header in the original request.

(Side note, the actual exception, response code and text message in the example above is not important, they just serve as simple example.)

Spring version: 3.1.1.RELEASE

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:09:08+00:00Added an answer on June 12, 2026 at 6:09 am

    I have spent some time investigating the matter and I have written a blog post in which I present a solution to the problem.

    Update: If you are using Spring 3.2, you can take advantage of the @ControllerAdvice annotation. More details can be found in my second blog post.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

PLEASE HELP ME TO SOLVE THIS ERROR cs file public partial class Control :
I have a class which is used to show correct message. This class works
Background: I have this with rollup query defined in MySQL: SELECT case TRIM(company) when
Consider MyClass.java: public class MyClass { public void firstfunction(double fwd[]) { fwd[0] = 42;
I have 2 classes with a draw function in them, my Background class and
Background: I have an MVC based polaroid object. The model keeps the photo's metadata,
Background I have an array of objects (Users) defined and set as follows: //
Background I have two lists, the first is items which contains around 250 tuples,
Background I have a function that takes a config object as an argument. Within
Background : I have a few different threads which each need to write to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.