Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9284569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:56:15+00:00 2026-06-18T18:56:15+00:00

In my spring webapp, I have an ajax servlet that answer json (using jackson):

  • 0

In my spring webapp, I have an ajax servlet that answer json (using 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>

I have the following ajax service:

@RequestMapping(value = "getLoggedUser")
@ResponseBody
public DtoUser getLoggedUser() {
    return authenticationService.getLoggedUser();
}

When the user is logged in it returns something like:

{ userName : "jojo", email : "john.doe@email.com", firstName : "John", lastName : "Doe" }

When the user is not logged in, the expected behavior is to return

null

But it returns an empty response which is not a valid JSON response (and additionally with a bad Content-type header)

Why is this happening ?
Do I have solutions to obtain the expected behaviour ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T18:56:17+00:00Added an answer on June 18, 2026 at 6:56 pm

    When the user is not logged in, the expected behavior is to return null

    That’s my expected behaviour because in both Java and Javascript/JSON, null is a valid value, which have a different mean than nothing, empty or error/exception.

    I would expect that Spring answer the null response instead of handling it specifically.
    In that case, the expected convertion for null (Java) would be null (JSON)
    My expected conversion table:

    Java Exception => HTTP Error Code
    null => null
    empty map / object => {}
    void => no response
    

    Why is this happening ?

    For Spring, a controller returning null mean “No response” and not “a response which value is null”. This rule applies to all controller methods including ones with @ResponseBody annotation.
    This allow to write manually to the response without having something appended to the response later:

    if (mySpecialCase) {
        Writer writer = response.getWriter();
        writer.write("my custom response");
        return null;
    } else {
         return myObject;
    }
    

    So when returning null Spring write nothing to the response, nor Content-type header nor body.

    Do I have solutions to obtain the expected behaviour ?

    I made the following dirty hack: add a filter on my ajax path that write null to the response when no response have been commited.

    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();
            }
        }
    
    }
    

    This solution handle methods answering null and method answering nothing (void) the same way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a servlet that outputs JSON. The output encoding for the servlet is
I'm trying to return some JSON from my Spring webapp using Jackson and parse
I have built a web app game that uses Ajax and Spring MVC. When
I have a spring based webapp that persists data with Hibernate. The default web
I'm building a webapp using JQuery, Stripes, Spring and JPA (Hibernate). I have a
I have designed a JSON object that is used as the format of AJAX
I have Apache2 SSL which is fronting Spring webapp as follows: ProxyPass / http://localhost:8080/
We have a Tapestry-Spring-Hibernate webapp running on Tomcat 6, handing about a few 1000
I have a spring web app that uses Hibernate and Velocity. It's an MVC
I'm developing web app using Spring Integration to route my messages, but I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.