Custom json response for internal exception in spring -


while implementing global exception handler in spring, noticed in case of not recognized accept header, spring throw it's own internal error. need return custom json error structure instead. works fine application specific exceptions , totally fails spring httpmediatypenotacceptableexception.

this code tells me "failed invoke @exceptionhandler method: public java.util.map restexceptionhandler.springmalformedacceptheaderexception()" when try request page incorrect accept header. other way return custom json spring internal exceptions?

@controlleradvice public class restexceptionhandler {      @exceptionhandler(value = httpmediatypenotacceptableexception.class)     @responsebody     public map<string, string> springmalformedacceptheaderexception() {           map<string, string> test = new hashmap<string, string>();          test.put("test", "test");          return test;      } }  

eventually figured way json mapping manually.

@exceptionhandler(value = httpmediatypenotacceptableexception.class) @responsebody public string springmalformedacceptheaderexception(httpservletresponse response) {     // populate errorobj, set response headers, etc     objectwriter jsonwriter = new objectmapper().writer();     try {         return  jsonwriter.writevalueasstring(errorobj);     } catch(exception e){}     return "whatever"; } 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -