码迷,mamicode.com
首页 > 编程语言 > 详细

Spring3.0 Could not find acceptable representation 解决方案

时间:2015-04-28 17:34:39      阅读:927      评论:0      收藏:0      [点我收藏+]

标签:

在使用spring3.0.5 mvc开发时,进行json接口数据开发,使用JSONObject.fromObject(object)返回json数据。遇到“org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation”错误。

 

解决方案1:

@ResponseBody 方法的返回类型由JSONObject改为 Object, return时直接返回Object;

修改springmvc-servlet.xml, 增加 messageConverters 

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<!-- Object->json -->
<property name="messageConverters"> 
<list> 
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /> 
</list> 
</property> 
</bean>

需要jacksonjar包支持: jackson-core-lgpl-1.2.1.jar和 jackson-core-lgpl-1.2.1.jar。  

解决方案2:

@ResponseBody 方法的返回类型由JSONObject改为 String, return时直接返回JSONObject.fromObject(object).toString();

 

解决方案3:

返回类型为JSONObject, 编写一个自己converter.

package javacommon.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.charset.Charset;

import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;

public class JaksonConverter extends AbstractHttpMessageConverter<Object> {
	public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
	
	public JaksonConverter() {
		super(new MediaType("application", "json", DEFAULT_CHARSET));
	}
	
	@Override
	protected boolean supports(Class<?> clazz) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	protected Object readInternal(Class<? extends Object> clazz,
			HttpInputMessage inputMessage) throws IOException,
			HttpMessageNotReadableException {
		logger.info(clazz.getSimpleName());
		InputStream inputStream=inputMessage.getBody();
		StringBuilder stringBuilder = new StringBuilder();
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
		String line = null; 
		while((line = bufferedReader.readLine()) != null){
			stringBuilder.append(line);
		}
		logger.info(stringBuilder.toString());
		return stringBuilder;
	}

	@Override
	protected void writeInternal(Object t, HttpOutputMessage outputMessage)
			throws IOException, HttpMessageNotWritableException {
		logger.info(t.getClass().getSimpleName());
		
		// HashMap
		// JSONObject
		logger.info(t.toString());
		OutputStream os=outputMessage.getBody();
		 
		os.write(t.toString().getBytes("utf-8")); 
		os.flush();
		
	}

}

将方案1中的converter换成编写的即可。  

Spring3.0 Could not find acceptable representation 解决方案

标签:

原文地址:http://www.cnblogs.com/leegtang/p/4463178.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!