Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring Ajax - @ResponseBody - Returning null response
    text
    copied!<p>In my spring webapp, I have an ajax servlet that answer json (using jackson):</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;context:component-scan base-package="com.myapp.ajax" /&gt; &lt;util:list id="messageConvertersList"&gt; &lt;bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"&gt; &lt;property name="supportedMediaTypes"&gt; &lt;list&gt; &lt;value&gt;application/json;charset=UTF-8&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;/bean&gt; &lt;/util:list&gt; &lt;bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"&gt; &lt;property name="messageConverters" ref="messageConvertersList" /&gt; &lt;/bean&gt; &lt;bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" /&gt; &lt;mvc:interceptors&gt; &lt;mvc:interceptor&gt; &lt;mvc:mapping path="/**" /&gt; &lt;bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"&gt; &lt;property name="paramName" value="lang" /&gt; &lt;/bean&gt; &lt;/mvc:interceptor&gt; &lt;/mvc:interceptors&gt; &lt;bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" /&gt; &lt;bean id="handlerExceptionResolver" class="com.myapp.ajax.AjaxExceptionHandlerResolver"&gt; &lt;property name="exceptionHandler"&gt; &lt;bean class="com.myapp.ajax.AjaxExceptionHandler" /&gt; &lt;/property&gt; &lt;property name="messageConverters" ref="messageConvertersList" /&gt; &lt;/bean&gt; </code></pre> <p></p> <p>I have the following ajax service:</p> <pre><code>@RequestMapping(value = "getLoggedUser") @ResponseBody public DtoUser getLoggedUser() { return authenticationService.getLoggedUser(); } </code></pre> <p>When the user is logged in it returns something like:</p> <pre><code>{ userName : "jojo", email : "john.doe@email.com", firstName : "John", lastName : "Doe" } </code></pre> <p>When the user is not logged in, the expected behavior is to return</p> <pre><code>null </code></pre> <p>But it returns an empty response which is not a valid JSON response (and additionally with a bad Content-type header)</p> <p>Why is this happening ?<br> Do I have solutions to obtain the expected behaviour ?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload