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

  • Home
  • SEARCH
  • 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 7062931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:39:12+00:00 2026-05-28T04:39:12+00:00

I was creating file upload using ExtJS 4 frontend and Spring 3 as backend.

  • 0

I was creating file upload using ExtJS 4 frontend and Spring 3 as backend. File upload works, but the response from server has wrong content type. When I send {success:true} using Map<String, Object> serialized by Jackson, ExtJS returns error

Uncaught Ext.Error: You're trying to decode an invalid JSON String: <pre style="word-wrap: break-word; white-space: pre-wrap;">{"success":true}</pre>

Why is my response wrapped with <pre> tag? I’ve searched and found out that I should change response type to text/html for example. But changing content type in servlet response didn’t help

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody Map<String, Object> upload(
    FileUpload uploadItem, BindingResult result, HttpServletResponse response) {

    response.setContentType("text/html");

    // File processing   

    Map<String, Object> jsonResult = new HashMap<String, Object>();
    jsonResult.put("success", Boolean.TRUE);
    return jsonResult;
}

When I change return value of upload method to String, everything works correctly, but I want to return Map and have it serialized by Jackson

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody String upload(
    FileUpload uploadItem, BindingResult result, HttpServletResponse response) {

    // File processing   

    return "{success:true}";
}

My Spring configuration

<bean 
    id="stringHttpMessageConverter" 
    class="org.springframework.http.converter.StringHttpMessageConverter">
</bean>
<bean 
    id="jacksonMessageConverter" 
    class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>  
<bean    
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jacksonMessageConverter"/>
            <ref bean="stringHttpMessageConverter" />
        </list>
    </property>
</bean>

How to tell Spring to return correct content type? Why is response of this method incorrect when response of other methods is interpreted correctly?

  • 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-05-28T04:39:13+00:00Added an answer on May 28, 2026 at 4:39 am

    Well, not really the best solution, but it solves the problem. I’ve created class, which has Map inside and method for adding parameters into Map. Also there is implemented method toString().

    public class ExtJSJsonResponse {
    
        /** Parameters to serialize to JSON */
        private Map<String, Object> params = new HashMap<String, Object>();
    
        /**
         * Add arbitrary parameter for JSON serialization. 
         * Parameter will be serialized as {"key":"value"};
         * @param key name of parameter
         * @param value content of parameter
         */
        @JsonIgnore
        public void addParam(String key, Object value) {
            params.put(key, value);
        }
    
        /**
         * Gets all parameters. Also is annotated with <code>@JsonValue</code>.
         * @return all params with keys as map
         */
        @JsonValue
        public Map<String, Object> getParams() {
            return params;
        }
    
        /**
         * Returns specified parameter by <code>key</code> as string "key":"value"
         * @param key parameter key 
         * @return  "key":"value" string or empty string when there is no parameter 
         *          with specified key
         */
        private String paramToString(String key) {
            return params.containsKey(key) 
                ? "\"" + key + "\":\"" + params.get(key) + "\""
                : "";
        }
    
        /**
         * Manually transforms map parameters to JSON string. Used when ExtJS fails 
         * to decode Jackson response. i.e. when uploading file.
         * @return 
         */
        @Override
        @JsonIgnore
        public String toString() {
            StringBuilder sb = new StringBuilder("{");
            String delimiter = "";
    
            for (String key : params.keySet()) {
                sb.append(delimiter);
                sb.append(paramToString(key));
                delimiter = ",";
            }
    
            sb.append("}");
            return sb.toString();
        }
    }
    

    So when Uncaught Ext.Error: You're trying to decode an invalid JSON String occurs, you simply do this

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public @ResponseBody String upload(
        FileUpload uploadItem, BindingResult result, HttpServletResponse response) {
        ExtJSJsonResponse response = new ExtJSJsonResponse();
    
        // File processing
        response.addParam("success", true);
        response.addParam("message", "All OK");   
    
        return response.toString();
    }
    

    In other methods which doesn’t have problem with serialization you can simply call return response; and it will be automatically serialized.

    Method toString() will work only for simple classes such as String. For more complicated classes you’ll have to change it.

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

Sidebar

Related Questions

Actually I'm creating an application for uploading file using JSF. But whenever I upload
How do I start with creating a file upload mechanism that works via Ajax?
im trying to upload a file using org.apache.commons.fileupload. but i dont no, what mistake
I'm using Uploadify and Kohana and I'm creating file uploader. User can upload only
I am trying to upload files using WCF. Everything under 16K works fine, but
I'm creating something that includes a file upload service of sorts, and I need
I would like to create .inf file (for creating .cab file) using msbuild command
I am creating a upload control in javascript and then using element.click() to bring
I'm now creating a file transaction system (through FTP) using wxWidgets for the GUI
I im creating a javascript file using addElemend and childAppend to add a new

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.