java - Spring MVC realise a custom response for multiple controllers based on a variable -


i have couple of controllers, annotated @requestmapping so:

@requestmapping(value = "/group/{groupname}", method = requestmethod.get) public responseentity<list<group>> getgroups(@pathvariable("groupname") string groupname) {...} 

as side note requests , responses (de)serialized jackson.

those requests can handled if there exists connection server. if connection breaks receive notification , want retry establishing connection. while doing want return status code 500.

what cleanest way so?

-- thank in advance.

edit:

i think wasn't clear. when lose jmx connection other server controllers still work , return error codes in 200-range.

but when receive notification connection has been lost want controllers return 500.

the way think of achieve set flag , use if-statement in each controller.

public class connector {     void handlenotification(notification notification, object handback) {             switch (notification.gettype()) {                 case "jmx.remote.connection.failed":                     is_connected = false;                     break;                 // ... other cases             }     } } 

the controller this:

@requestmapping(value = "/group/{groupname}", method = requestmethod.get) public responseentity<list<group>> getgroups(@pathvariable("groupname") string groupname) {     if (connector.is_connected) {         // ...     } else {         // return 500     } } 

this code redundant , unmaintainable hope has better solution this.

you have several way handle error in spring.

for example, can throw error , add @exceptionhandler in controller.

if want have more global exception handling, can use @controlleradvice on class define way bind exception on error code.

it's possible return responseentity make. in class can set http status want return. example, if goes wrong, can return new responseentity<list<group>>(httpstatus.internal_server_error);

the other way define handlerexceptionresolver or use existing one.

you find more information on blog post : https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -