如何克服没有转换器发现错误

时间:2017-04-17 08:25:36

标签: java angularjs spring hibernate

我正在研究使用Spring 4.3.2.Release和hibernate 5.2.9.Final版本的项目。它是一个基于Spring + hibernate + Restful服务的例子。

在进行演示时我收到错误:找不到转换器:

以下是我的代码的凭据。

servlet.xml中

<?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:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <mvc:annotation-driven/>
    <mvc:default-servlet-handler/>
    <context:annotation-config/>
    <context:component-scan base-package="practice.ngresource.tryone.controller.**"></context:component-scan>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/views/"></property>
        <property name="suffix" value=".html"></property>
    </bean>

</beans>

这是Rest Controller。

import org.springframework.web.bind.annotation.RestController;

import practice.ngresource.tryone.model.Task;


@RestController
@RequestMapping("/trialone")
public class TrialController {

    @ResponseStatus(HttpStatus.OK)
    @RequestMapping(value="/trial",method=RequestMethod.GET)
    public List<Task> taskList(){
        System.out.println("inside the gettasklist");
        List<Task> list = new ArrayList<>();
        list.add(new Task("one","oneDesc","completed"));
        list.add(new Task("Two","twoDesc","completed"));
        list.add(new Task("Three","threeDesc","pending"));
        list.add(new Task("four","fourDesc","pending"));
        list.add(new Task("five","fiveDesc","completed"));
        return list;
    }

}

查看并对应JS页面:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body ng-app="trial1App" ng-controller="trialController as tCont">
        <h1>The Task List is as follow</h1>
        <div>
            <table>
                <tr ng-repeat="data in tCont.tasks">
                    <td>{{data.taskName}}</td>
                </tr>
            </table>

        </div>

        <script type="text/javascript"
        src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
        <script type="text/javascript"
        src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular-resource.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
        <script>



            var trialApp = angular.module('trial1App', ['ngResource']);

            trialApp.factory('Task',['$resource',function($resource){
                    return $resource('http://localhost:8080/NgRes_Prac_001/trialone/trial/:trialId',
                            {trialId:'@tId'},
                            {
                                updateTask : {method : 'PUT'}
                            });
                }]);

            trialApp.controller('trialController', ['$scope','Task',function($scope,Task){
                    var ob = this;
                    ob.tasks = [];
                    ob.fetchAllTasks = function(){
                            console.log("Inside Fetch all list");                           
                            ob.tasks = Task.query();
                            console.log("Fetch List Completed");
                        };

                    ob.fetchAllTasks();
                }]);



        </script>
</body>
</html>

以下是我的错误状态。

Apr 17, 2017 1:43:24 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [ngRs001] in context with path [/NgRes_Prac_001] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.ArrayList] with root cause
java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.ArrayList
    at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:188)
    at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:173)
    at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:81)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:130)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1099)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:670)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

请帮我解决这个问题。 提前谢谢。

2 个答案:

答案 0 :(得分:0)

通常,每当您看到处理转换的错误(如您的)时,都会由不兼容的数据类型引起。

快速查看错误消息使其看起来像

List<Task> list = new ArrayList<>();

是罪魁祸首。 list和ArrayList似乎是不兼容的类型。这可能意味着类型转换是必要的,或者你应该考虑使'list'成为一种不同的类型。

- Edit-- 另一种可能性是缺少依赖性。确保项目内的所有内容都被正确引用。

希望这至少有些用处,
-Joseph

答案 1 :(得分:0)

如果你的控制器上有@RestController足以将你的数据转换成JSON字符串,那么我查了一遍,似乎你在任务类中缺少getter \ setters < /强>

根据:

How to return JSON data from spring Controller using @ResponseBody

java.lang.IllegalArgumentException: No converter found for return value of type