这是我在浏览器中使用REST获得Spring MVC的输出
org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class java.util.ArrayList
org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:226)
org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:224)
org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:82)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:119)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:866)
javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
它正在从数据库中获取列表,并且也在控制台中打印,但是由于某种原因,它无法转换为JSON
这是我的web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Restaurant</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-
class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
这是我的调度程序servlet
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan
base-package="com.mvcrest.controller"></context:component-scan>
</beans>
我进行了搜索,大多数解决方案都指向在pom.xml中添加杰克逊依赖项,但是我已经有了
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>MvcRest</groupId>
<artifactId>MvcRest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
我的控制器也被击中(我知道这是因为列表是在控制台中打印的)
@Controller
public class App {
IRestaurantService res;
public App() {
try {
res = new RestaurantService();
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@RequestMapping(value = "/menu", method = RequestMethod.GET)
public ResponseEntity<List<Menu>> getmenu() {
List<Menu> menus = res.getMenu();
for (Menu menu : menus) {
System.out.println(menu.getName());
}
return new ResponseEntity<List<Menu>>(menus, HttpStatus.OK);
}
}
这是我的控制台日志(我使用Eclipse Photon作记录)。
Nov 12, 2018 5:47:34 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:MvcRest' did not find a matching property.
Nov 12, 2018 5:47:34 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version: Apache Tomcat/9.0.10
Nov 12, 2018 5:47:34 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built: Jun 20 2018 17:32:21 UTC
Nov 12, 2018 5:47:34 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number: 9.0.10.0
Nov 12, 2018 5:47:34 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name: Windows 10
Nov 12, 2018 5:47:34 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version: 10.0
Nov 12, 2018 5:47:34 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture: amd64
Nov 12, 2018 5:47:34 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home: C:\Program Files\Java\jre1.8.0_102
Nov 12, 2018 5:47:34 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version: 1.8.0_102-b14
Nov 12, 2018 5:47:34 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor: Oracle Corporation
Nov 12, 2018 5:47:34 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE: D:\WorkSpace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Nov 12, 2018 5:47:34 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME: D:\Tools\apache-tomcat-9.0.10
Nov 12, 2018 5:47:34 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=D:\WorkSpace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Nov 12, 2018 5:47:34 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=D:\Tools\apache-tomcat-9.0.10
Nov 12, 2018 5:47:34 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=D:\WorkSpace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
Nov 12, 2018 5:47:34 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=D:\Tools\apache-tomcat-9.0.10\endorsed
Nov 12, 2018 5:47:34 PM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
Nov 12, 2018 5:47:34 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jre1.8.0_102\bin;C:\windows\Sun\Java\bin;C:\windows\system32;C:\windows;C:/Program Files/Java/jre1.8.0_102/bin/server;C:/Program Files/Java/jre1.8.0_102/bin;C:/Program Files/Java/jre1.8.0_102/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Microsoft Application Virtualization Client;C:\Program Files\Git\cmd;C:\Program Files\nodejs\;C:\Program Files\Maven\bin\;C:\Program Files (x86)\MySQL\MySQL Utilities 1.6\;C:\Program Files\Tomcat\bin\;C:\Users\M1048950\AppData\Local\Microsoft\WindowsApps;;C:\Program Files\Microsoft VS Code\bin;C:\Users\M1048950\AppData\Roaming\npm;C:\windows\system32;;.]
Nov 12, 2018 5:47:34 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8030"]
Nov 12, 2018 5:47:34 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Nov 12, 2018 5:47:34 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8090"]
Nov 12, 2018 5:47:34 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Nov 12, 2018 5:47:34 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1426 ms
Nov 12, 2018 5:47:34 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Catalina]
Nov 12, 2018 5:47:34 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/9.0.10
Nov 12, 2018 5:47:44 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Nov 12, 2018 5:47:57 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Nov 12, 2018 5:47:57 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Nov 12, 2018 5:47:57 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcher'
Nov 12, 2018 5:47:57 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization started
Nov 12, 2018 5:47:57 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Mon Nov 12 17:47:57 IST 2018]; root of context hierarchy
Nov 12, 2018 5:47:58 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
Nov 12, 2018 5:48:00 PM org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry register
INFO: Mapped "{[/menu],methods=[GET]}" onto public org.springframework.http.ResponseEntity<java.util.List<com.mvcrest.entity.Menu>> com.mvcrest.controller.App.getmenu()
Nov 12, 2018 5:48:01 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
INFO: Looking for @ControllerAdvice: WebApplicationContext for namespace 'dispatcher-servlet': startup date [Mon Nov 12 17:47:57 IST 2018]; root of context hierarchy
Nov 12, 2018 5:48:02 PM org.springframework.web.servlet.FrameworkServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization completed in 4536 ms
Nov 12, 2018 5:48:02 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8030"]
Nov 12, 2018 5:48:02 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8090"]
Nov 12, 2018 5:48:02 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 27251 ms
Nov 12, 2018 5:48:03 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/MvcRest/] in DispatcherServlet with name 'dispatcher'
Burger
Pizza
Pasta
Nutella
Steak
Ice Cream
Nov 12, 2018 5:48:08 PM org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver handleHttpMessageNotWritable
WARNING: Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class java.util.ArrayList
我正在使用浏览器命中端点“ http://localhost:8030/MvcRest/menu”(也尝试过邮递员,但这没有什么区别,在这里是不相关的)。
经历了3天。非常感谢
答案 0 :(得分:0)
在调度程序servlet配置xml文件中,确保具有以下内容:
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
上面的代码添加了必要的转换器。
不要忘记为 mvc 添加名称空间。
答案 1 :(得分:0)
尝试将@RequestMapping
更改为@GetMapping
或将其从ResponseEntity<>
更改为@ResponseBody
示例1
@GetMapping("/menu")
public ResponseEntity<List<Menu>> getmenu() {
List<Menu> menus = res.getMenu();
for (Menu menu : menus) {
System.out.println(menu.getName());
}
return new ResponseEntity<List<Menu>>(menus, HttpStatus.OK);
}
示例2
@RequestMapping(value = "/menu", method = RequestMethod.GET)
public @ResponseBody List<Menu> getmenu() {
List<Menu> menus = res.getMenu();
for (Menu menu : menus) {
System.out.println(menu.getName());
}
return menues;
}
https://www.baeldung.com/spring-response-entity
最后一件事;您的IRestaurantService
在类或实现它的类中应被注释为@Service
,然后应自动连接到您的控制器中,因此您不必实例化它。
@Service
public class RestarauntService implements IRestaurantService {
...
}
然后
@Controller
public class App {
@Autowired
private RestaurantService res;
....
}