1

I wondered if someone could answer a question for me as I dont quite understand why I needed to read a json object from the request input stream when I passed one to a spring controller manually.

Normally I use a json-rpc framework and it handles everything for me so I hadnt actually had to do this manually until now. Everything works ok but what I dont understand is why there was nothing in the request like when you post a form, and instead I had to use this code to map my object to Jackson:

BufferedInputStream bis = new BufferedInputStream(request.getInputStream());
ChartParameters chartParameters = mapper.readValue(bis, ChartParameters.class);

I would just like to understand why I needed to read in the input stream and pass this to jackson instead of being able to get a value as a string which I first thought I would have to do.

Thanks in advance for any helpful answers.

2
  • Can you show the relevant parts of your Spring controller where those two lines are being used? A bit of context might clarify... Commented Jul 11, 2013 at 7:10
  • I wasnt really that interested in the Spring side of things unless the reason is purely spring related? I didnt write a plain servlet to test if I still needed to do the same thing. Here is the controller method anyway in case it is: public void handleChartJsonRPC(HttpServletRequest request, HttpServletResponse response) throws Exception { jsonService.getBarChart(request, response); } Commented Jul 11, 2013 at 7:26

2 Answers 2

1

If you're on a recent Spring version, the following should be enough to get things rolling:

@ResponseBody
public Chart handleChartJsonRPC(@RequestBody ChartParameters chartParameters) throws Exception {
    return jsonService.getBarChart(chartParameters);
}

This (obviously) assumes your jsonService returns a Chart object, which should be serialised to JSON before sending it back to the browser.

Make sure you have a MessageConverter that will (de-)serialise your objects to and from JSON, as described by @user2054820.

Sign up to request clarification or add additional context in comments.

3 Comments

Right ok so is the reason that I had to get the stream because I dont have a message converter in the chain?
Most likely that's the reason, but it's a bit hard to say with only two lines of code context.
Ok thanks, for now I will have to assume thats the correct answer, and thanks for the code snippet above. I was planning to convert my chart object that the service returns manually with Jackson and then return this in the response but using a message converter and the annotations method looks a better option.
1

You can post your JSON just as a string and you can configure :

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.