A project must handle a wide variety of errors. On the one hand, these errors require the program to react to these errors and allow the programmer to quickly
Navigate to, on the other hand, the user should be given appropriate error prompts when certain errors occur. For example, the method of a request is
publicgetUserInfo(String userId)
The request parameter userid is null at this point and I will do so in the project
publicgetUserInfo(String userId){ "userId不能为空"); // ..}
and use a global exception-catching class to handle all the exceptions in the system, such as the Spring
HandlerExceptionResolver
public Modelandview Resolveexception (HttpServletRequest request, httpservletresponse response, Object handler, Exception ex) { Map map = new HashMap (); map . Put ( "success" , false ); map . Put ( "message" , Ex.getmessage ()); try {Sendjson (response, Utils.parsejson (map )); } catch (IOException e) {e.printstacktrace (); } return new Modelandview (); }
This allows you to return the error message to the client. Of course, in order to better support multi-national users, you can consider the error information to do the internationalization of processing, here is not specifically to expand the speaking. It is important to note, however, that Resoucebundle
cannot be taken for granted because the client user needs to be handled, and ResourceBundle
is only based on the locale of the server.
for the management of error information, we still put it into the configuration file, so that not only can be easily modified, and can provide internationalization at a later stage.
But when the project gets bigger, it's easy to have a maintenance nightmare by storing the error information used by all classes in a large property file. To avoid this, we can draw on the processing method in Tomcat: Assign a property file to each package. For example, the properties file inside the package org.apache.catalina.connector
contains all the error information thrown by all the classes under the package. Each property file is handled by an instance of the Org.apache.catalina.util.StringManager
class. When Tomcat
runs, there will be many instances of Stringmanager
, each of which reads a property file corresponding to the package.
when a class inside a package needs to produce an error message, it first obtains a Stringmanager
instance. However, some other classes inside the same package may also require Stringmanager
, and creating a Stringmanager
instance for each object is a waste of resources. Therefore, the Stringmanager
class is designed as a single-instance (singleton) class. The only way to construct a constructor is private, and you cannot instantiate it with the new
keyword outside of the class. You can only pass a package name to invoke its public static method, GetManager
, to obtain an instance. Each instance is stored in a Hashtable
with the package name key
.
privatestaticnew HashMap<String, StringManager>();publicstaticgetManager(String packageName) { StringManager mgr = (StringManager)managers.get(packageName); ifnull) { new StringManager(packageName); managers.put(packageName, mgr); } return mgr;}
Discuss how to handle error messages in your project