Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: Spring JSON causes problems when the Spring framework is upgraded from 3.0.2 to 3.2.0

  1. #11
    Join Date
    Nov 2012
    Posts
    5

    Default

    Hi Kent,

    Thanks for your response...I had tried previously with produces ="application/json" and it didnt work. Also now i tried with

    Code:
    	@RequestMapping(method = RequestMethod.GET, value = { WebApplicationConstants.SWITCH_USER_URL },produces = "application/json;charset=UTF-8")
    Still am getting 406. Also I would like to tell you one more observation that we have made. The moment when I change the return type from an Object to a String, it works....
    Code:
    Existing:
    
    public @ResponseBody
    CustomResponse switchUser(
    @RequestParam(WebApplicationConstants.SESSION_TOKE N) String gsxSessionToken,
    @RequestParam(WebApplicationConstants.SWITCH_FLAG) String switchFlag,
    HttpServletRequest request,
    HttpServletResponse response,
    
    Return Type:
    return new CustomResponse(jsonObject.toString());
    
    New:
    )
    public @ResponseBody
    String switchUser(
    @RequestParam(WebApplicationConstants.SESSION_TOKE N) String gsxSessionToken,
    @RequestParam(WebApplicationConstants.SWITCH_FLAG) String switchFlag,
    HttpServletRequest request,
    HttpServletResponse response,
    
    
    Return Type:
    return (jsonObject.toString());

    But this involves changes across application and is not a recommended solution that we are looking for. Could you please let us know how we can tackle this error?
    Last edited by dinup; Jan 14th, 2013 at 11:23 PM.

  2. #12
    Join Date
    Sep 2011
    Posts
    25

    Default

    dinup:
    Have you add jackson-core and jackson-databind?
    I just wrote a sample and it works.

  3. #13
    Join Date
    Nov 2012
    Posts
    5

    Default

    Hi Kent,


    Thanks for your timely response..I had org.codehaus.jackson.jackson-mapper and org.codehaus.jackson.jackson-core(1.9.8). Currently downloading the databind jar and trying whether it works..When I further debugged the code, there's more dimension to this. My return class CustomeResponse is invoked by a customised HTTP Message Converter.

    Code:
    public class ConfigurableStringHttpMessageConverter extends AbstractHttpMessageConverter<Object>{
    
    	/** Logger */
    	private final static Logger logger = LoggerFactory
    			.getLogger(ConfigurableStringHttpMessageConverter.class);
    	private static final String CONST_TEXT ="text";
    	private static final String CONST_PLAIN ="plain";
    	private final List<Charset> availableCharsets;
        private static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1");
    
        public ConfigurableStringHttpMessageConverter() {
            this(DEFAULT_CHARSET);
        }
    
        public ConfigurableStringHttpMessageConverter(Charset defaultCharset) {
        	super(new MediaType("application", "json", defaultCharset));
        	MediaType mediaType = new MediaType("application", "json", defaultCharset);
        	List<MediaType> mediaTypes = new ArrayList<MediaType>();
        	mediaTypes.add(mediaType);
        	this.setSupportedMediaTypes(mediaTypes);
        	this.availableCharsets = new ArrayList<Charset>(Charset.availableCharsets().values());    	
    	}
    @Override
    	public boolean supports(Class<? > clazz) {
    		return CustomResponse.class.isAssignableFrom(clazz);
    	}
    
    	@Override
    	protected String readInternal(Class<? extends Object> clazz, HttpInputMessage inputMessage) throws IOException,HttpMessageNotReadableException {
    		MediaType contentType = inputMessage.getHeaders().getContentType();
    		Charset charset = contentType.getCharSet() != null ? contentType.getCharSet() : DEFAULT_CHARSET;
    		logger.debug("charset in READ {}",charset);
    		logger.debug("Content Type in READ {}",contentType);
    
    		return FileCopyUtils.copyToString(new InputStreamReader(inputMessage.getBody(), charset));
    	}
    
    
    	@Override
    	protected void writeInternal(Object s, HttpOutputMessage outputMessage) throws IOException {
    		outputMessage.getHeaders().setAcceptCharset(getAcceptedCharsets());
    		MediaType contentType = outputMessage.getHeaders().getContentType();
    		logger.debug("Content Type in WRITE {}",contentType);
    		Charset charset = contentType.getCharSet() != null ? contentType.getCharSet() : DEFAULT_CHARSET;
    		logger.debug("charset in WRITE {}",charset);
    		FileCopyUtils.copy(s.toString(), new OutputStreamWriter(outputMessage.getBody(), charset));
    	}
    
    Dispatcher Servlet:
    <bean class="com.apple.ist.gsx.util.WebAppBeanPostProcessor">
    	  <property name="prependedMessageConvertors">
    	   <list>
    	    <bean id="configurableMessageConvertor" class="com.apple.ist.gsx.util.ConfigurableStringHttpMessageConverter">
    	         <constructor-arg value="UTF-8"/>
    	    </bean>
    	   </list>
    	  </property>
    	  <property name="order" value="0"/>
    	 </bean>
    The customised HTTP message converter was done to enforce UTF-8 charset even for Japanese language..
    Earlier it was setting content type to "text/plain". Now I tried explicilty setting the content type to application/json in the message converter as shown above..But it still doesn't work.

    Thanks in advence,
    Dinup.
    Last edited by dinup; Jan 14th, 2013 at 11:32 PM.

  4. #14
    Join Date
    Sep 2011
    Posts
    25

    Default

    dinup:
    If you want to use jackson with spring default message converter to convert object to json.
    Just include jackson-core and jackson-databind into classpath, Spring 32 will automatic include default message-converter,
    Otherwise, config message-converters register-defaults with false and put your custom message converter into configuration.
    Code:
     <mvc:annotation-driven>
            <mvc:message-converters register-defaults="true">
                <bean class="your.custom.message.converter"/>
            </mvc:message-converters>
     </mvc:annotation-driven>

  5. #15
    Join Date
    Nov 2012
    Posts
    5

    Default

    Hi Ken,

    I tried with the above jackson jars and it still didnt work. Also tried registering my custom converter with register-defaults false...

    Code:
     <mvc:annotation-driven>
            <mvc:message-converters register-defaults="false">
                <bean class="com.apple.ist.gsx.util.ConfigurableStringHttpMessageConverter"/>
            </mvc:message-converters>
     </mvc:annotation-driven>
    Still the issue remain same..Am not sure whether its anything to do with the way my Custom Message Converter is handling the content type..Only those flows with Custom Converter is failing for now..Rest of the flow remains intact..I have pasted my custom converter code above..

    I could see this in my logs all in these cases..This is happening after Controller sucessfully processses the request:

    Code:
    org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
    2013-01-14 22:51:22,986 DEBUG [org.springframework.web.servlet.DispatcherServlet][processDispatchResult] Null ModelAndView returned to DispatcherServlet with name 'Spring Servlet': assuming HandlerAdapter completed request handling
    Last edited by dinup; Jan 15th, 2013 at 12:59 AM.

  6. #16
    Join Date
    Sep 2011
    Posts
    25

    Default

    dinup:
    I believe your application must be something wrong.
    See my attachment (wrote by maven) to show how it works.
    You can issue
    "mvn test" to test controller;
    "mvn jetty:run" to start up a jetty server and open link to test on your browser.
    or
    "mvn package" to pack a war file and deploy it to your application server.

    Good luck.
    Attached Files Attached Files

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •