How to properly send an HTTP message to the client

后端 未结 7 1137
一生所求
一生所求 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<String, String> systemProperties;
    
        @Override
        public void afterPropertiesSet() throws Exception {
            if (null == systemProperties || systemProperties.isEmpty()) {
                return;
            }
    
            final Set<Entry<String, String>> entrySet = systemProperties.entrySet();
            for (final Entry<String, String> entry : entrySet) {
    
                final String key = entry.getKey();
                final String value = entry.getValue();
    
                System.setProperty(key, value);
            }
    
        }
    
        public void setSystemProperties(final Map<String, String> systemProperties) {
            this.systemProperties = systemProperties;
        }
    
    }
    

    And in applicationContext.xml:

    <bean class="....SystemPropertiesInitializingBean">
        <property name="systemProperties">
            <map>
                <entry key="org.apache.coyote.USE_CUSTOM_STATUS_MSG_IN_HEADER" value="true"/>
            </map>
        </property>
    </bean>
    
    0 讨论(0)
提交回复
热议问题