मेरी वसंत webapp में, मैं एक ajax सर्वलेट कि json (जैक्सन का प्रयोग करके) का जवाब है:स्प्रिंग अजाक्स - - @ResponseBody रिटर्निंग अशक्त प्रतिक्रिया
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<context:component-scan base-package="com.myapp.ajax" />
<util:list id="messageConvertersList">
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</util:list>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters" ref="messageConvertersList" />
</bean>
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
</mvc:interceptor>
</mvc:interceptors>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />
<bean id="handlerExceptionResolver" class="com.myapp.ajax.AjaxExceptionHandlerResolver">
<property name="exceptionHandler">
<bean class="com.myapp.ajax.AjaxExceptionHandler" />
</property>
<property name="messageConverters" ref="messageConvertersList" />
</bean>
मैं निम्नलिखित ajax सेवा है:
{ userName : "jojo", email : "[email protected]", firstName : "John", lastName : "Doe" }
:
@RequestMapping(value = "getLoggedUser")
@ResponseBody
public DtoUser getLoggedUser() {
return authenticationService.getLoggedUser();
}
जब उपयोगकर्ता यह प्रवेश होने की तरह कुछ देता है
जब उपयोगकर्ता के लॉग इन नहीं है, अपेक्षित व्यवहार
null
वापस जाने के लिए है लेकिन यह एक खाली प्रतिक्रिया जो एक मान्य JSON प्रतिक्रिया नहीं है देता है (और इसके अलावा एक बुरा Content-Type हैडर के साथ)
ऐसा क्यों हो रहा है?
क्या मेरे पास अपेक्षित व्यवहार प्राप्त करने के लिए समाधान हैं?
मुझे लगता है कि खाली जेसन उचित व्यवहार होगा यदि आप शून्य वस्तु को वापस कर रहे हैं, स्ट्रिंग "शून्य" वापस नहीं लौट रहे हैं। सामग्री प्रकार के रूप में क्या लौटाया जा रहा है? यदि आप एक अधिक विश्वसनीय पैटर्न का पालन करने की कोशिश कर रहे हैं, तो सबसे अच्छा समाधान एक उचित HTTP स्थिति कोड, 404 (नहीं मिला) या 401 (अधिकृत नहीं) की तरह वापस करना होगा। या, बेहतर अभी तक, उन्हें लॉगिन पृष्ठ पर रीडायरेक्ट/अग्रेषित करें। – CodeChimp
एक खाली जेसन का मतलब यह होगा कि मुझे यह जानने के लिए एक संपत्ति (उदा: प्रतिक्रिया.मेल) का परीक्षण करना है कि उपयोगकर्ता शून्य है या नहीं। ऐसा क्यों लगता है कि वापस लौटने से अधिक उचित और सुसंगत –