When using Spring 3.0.5 MVC development, JSON interface data development, using JSONObject.fromobObject (Object) to return JSON data. Problems with “org. Springframework. Web. HttpMediaTypeNotAcceptableException: Could not find acceptable representation” error.
Solution 1:
The @responseBody method’s return type is changed from JSONObject to Object.
Modify SpringMVC-servlet.xml to add 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 package support is required: jackson-core-lgpl-1.2.1.jar and jackson-core-lgpl-1.2.1.jar.
Solution 2:
The @responseBody method returns a String instead of a JSONObject, and returns JSONObject.fromobObject (Object).toString();
At this time, if there is Chinese, there will be garbled code, the solution is as follows:
<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> -->
<!-- default is ISO you should set utf-8 -->
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<bean class="org.springframework.http.MediaType">
<constructor-arg index="0" value="text" />
<constructor-arg index="1" value="plain" />
<constructor-arg index="2" value="UTF-8" />
</bean>
</list>
</property>
</bean>
</list>
</property>
</bean>
Solution 3:
The return type is JSONObject. Write your own 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();
}
}
Replace the Converter in Scenario 1 with a written one.
Read More:
- Spring Error: Transaction synchronization is not active
- [Solved] Could not find resource COM / atguigu / Dao / studentdao.xm, the mapper file for storing SQL statements could not be found and an error occurred
- [Solved] Spring Kafka Send Error in specifies partition: Topic radar not present in metadata after 60000
- [Solved] spring source code compile error: target package does not exist
- Failed to auto-configure a DataSource: ‘spring.datasource.url‘ is not specified and no embedded data
- Java Running Error: Could not find or load main class
- Maven error: Could not find artifact jdk.tools [How to Solve]
- [Solved] Mybatis Error: Could not find resource mybatis-conf.xml
- Spring MVC uses Ajax to submit requests asynchronously to complete login
- [Javac compilation exception] javac compilation prompts that the package in jdk cannot be found error: package jdk.internal.org.objectweb.asm does not exist and error: cannot find symbol
- Split log by date in log4j2 of spring boot
- [Solved] Spring Boot Error: org.springframework.jdbc.datasource.embedded.EmbeddedData
- Spring integrated HBase error [How to Solve]
- [Solved] Spring AOP Error creating bean with name
- Spring interdependence error: beancurrentyincreationexception unsatisfieddependencyexception
- Spring Aop error creating bean with name ‘org.springframework.aop.config.internalAutoProxyCreator
- [Solved] Spring jdbctemplate Error: ‘Java. SQL. Driver’ for property ‘driver’: no
- [Solved] Spring upload file Error: Multipartfile Transferto() reported an error FileNotFoundException
- How to Solve JDBC connection error in spring MVC integration
- Hystrix fuse of spring cloud system