How to properly send an HTTP message to the client

后端 未结 7 1139
一生所求
一生所求 2021-02-05 00:14

I\'m working on a RESTful web service in Java. I need a good way to send error messages to the client if something\'s wrong.

According to the Javadoc, HttpServlet

7条回答
  •  一向
    一向 (楼主)
    2021-02-05 00:53

    In Spring powered web application, running on Tomcat I use following bean:

    import java.util.Map;
    import java.util.Set;
    import java.util.Map.Entry;
    
    import org.springframework.beans.factory.InitializingBean;
    
    public class SystemPropertiesInitializingBean implements InitializingBean {
    
        private Map systemProperties;
    
        @Override
        public void afterPropertiesSet() throws Exception {
            if (null == systemProperties || systemProperties.isEmpty()) {
                return;
            }
    
            final Set> entrySet = systemProperties.entrySet();
            for (final Entry entry : entrySet) {
    
                final String key = entry.getKey();
                final String value = entry.getValue();
    
                System.setProperty(key, value);
            }
    
        }
    
        public void setSystemProperties(final Map systemProperties) {
            this.systemProperties = systemProperties;
        }
    
    }
    

    And in applicationContext.xml:

    
        
            
                
            
        
    
    

提交回复
热议问题