FF_*_*Dev 7 java ajax spring spring-mvc jackson
在我的春季webapp中,我有一个回答json的ajax servlet(使用jackson):
<?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>
Run Code Online (Sandbox Code Playgroud)
我有以下ajax服务:
@RequestMapping(value = "getLoggedUser")
@ResponseBody
public DtoUser getLoggedUser() {
return authenticationService.getLoggedUser();
}
Run Code Online (Sandbox Code Playgroud)
当用户登录时,它返回如下内容:
{ userName : "jojo", email : "john.doe@email.com", firstName : "John", lastName : "Doe" }
Run Code Online (Sandbox Code Playgroud)
当用户未登录时,将返回预期的行为
null
Run Code Online (Sandbox Code Playgroud)
但它返回一个空响应,它不是有效的JSON响应(另外还有一个错误的Content-type标头)
为什么会这样?
我是否有解决方案来获得预期的行为?
当用户未登录时,预期的行为是返回null
这是我的预期行为,因为在Java和Javascript/JSON中,null是一个有效值,它具有不同于平均值,空或错误/异常的平均值.
我希望Spring回答null响应而不是专门处理它.在这种情况下,null(Java)的预期转换将为null(JSON)
我的预期转换表:
Java Exception => HTTP Error Code
null => null
empty map / object => {}
void => no response
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
对于Spring,返回null的控制器表示"无响应"而不是"响应哪个值为空".此规则适用于所有控制器方法,包括具有@ResponseBody注释的方法.
这允许手动写入响应,而不会在以后添加到响应中:
if (mySpecialCase) {
Writer writer = response.getWriter();
writer.write("my custom response");
return null;
} else {
return myObject;
}
Run Code Online (Sandbox Code Playgroud)
因此,当返回null时,Spring不会向响应写入任何内容,也不会向Content-type标头写入任何内容.
我是否有解决方案来获得预期的行为?
我做了以下脏黑客攻击:在我的ajax路径上添加一个过滤器,当没有响应提交时,该过滤器将null写入响应.
public class AjaxEmptyResponseFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
chain.doFilter(request, response);
if (!response.isCommitted()) {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json");
Writer writer = response.getWriter();
writer.write("null");
writer.close();
response.flushBuffer();
}
}
}
Run Code Online (Sandbox Code Playgroud)
此解决方案处理回答null的方法和以相同方式回答任何内容(void)的方法.