GVKun编程网logo

Idea 中:No converter found for return value of type: class java.util.ArrayList:Json 格式转换问题

14

在本文中,我们将详细介绍Idea中:Noconverterfoundforreturnvalueoftype:classjava.util.ArrayList:Json格式转换问题的各个方面,同时,我

在本文中,我们将详细介绍Idea 中:No converter found for return value of type: class java.util.ArrayList:Json 格式转换问题的各个方面,同时,我们也将为您带来关于$.ajax 函数调接口,报异常 No converter found for return value of type: class java.util.ArrayList、(HttpMessageNotWritableException ) No converter found for return value of type xxxx、android – java.lang.NoClassDefFoundError:失败的解析:Lio / realm / internal / SharedRealm $Durability、Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property &#...的有用知识。

本文目录一览:

Idea 中:No converter found for return value of type: class java.util.ArrayList:Json 格式转换问题

Idea 中:No converter found for return value of type: class java.util.ArrayList:Json 格式转换问题

1. 在搞 SSM 框架的时候,前端发送请求后,显示如下错误。

@ResponseBody 注解进行返回 List <对象> 的 json 数据时出现

nested exception is java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.ArrayList 错误,

2. 前后调用场景:

  2.1mvc 配置文件设置:fastJson,没问题

  2.2 Controller 层的注解没问题

  2.3 注解路径扫描检查,确认没问题。

  2.4pom 中的依赖是父工程管理的,貌似没问题(最后证明问题出在了依赖上)

 

3. 网上查了这个问题,大部分解释都是:数据以 list 集合返回后,由 AJax 解析是错误,即数据没有转化成 Json 格式

 也可能会以文件的格式下载,而不能正常解析到在浏览器中。

 检查了依赖,发现果然是父工程中依赖版本没有添加。为了保险,把依赖版本直接放入子工程中,结果就正常解析了。

 

<!--json相关的依赖-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.7.4</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.7.4</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.7.4</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.28</version>
        </dependency>

 

解析正常:

 

可参考一下文档:https://www.cnblogs.com/hafiz/p/5812873.html

4. 总结:依赖问题是开发过程中常遇到的,比较坑的问题。很容易把注意力放在代码逻辑上。

    所以好的编码习惯,规范和统一的依赖管理很重要。

$.ajax 函数调接口,报异常 No converter found for return value of type: class java.util.ArrayList

$.ajax 函数调接口,报异常 No converter found for return value of type: class java.util.ArrayList

接口正常执行,返回给前端后报服务器 500 异常,异常详情:

org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class java.util.ArrayList

 

原因没有深究,大概是 $.ajax 函数在前端接收的数据类型为 JSON,而服务中没有能将 ArrayList 转换成 JSON 的类。

暂时记下解决方案 —— 在项目中添加 JSON 全家桶的依赖如下:

<dependency>
<groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.9</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.9</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.9</version> </dependency>

 

(HttpMessageNotWritableException ) No converter found for return value of type xxxx

(HttpMessageNotWritableException ) No converter found for return value of type xxxx

最近在家没事儿,写写代码玩,用了 Maven 构建 SSM 项目,结果提示如下信息

org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class com.lcy.pojo.User
【原因】找不到返回值类型的转换器,找了好久发现controller返回的是json格式数据

【方案】在mavenpox.xml中只引入Json的依赖时,只引入了“jackson-core”;没有引入 “jackson-databind”,添加了依赖后问题解决了。
【提示】当controller标记@ResponseBody后,会用解析器去解析Controller的返回值,解析器会去寻找SpringMvc中注册的HttpMeesageConverter接口的实现类,结果因为没有添加对应的依赖,所以
就找不到Json类型的转换器了,添加依赖后就正常了。



【正确的依赖】
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.8</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.8</version>
        </dependency>

 






android – java.lang.NoClassDefFoundError:失败的解析:Lio / realm / internal / SharedRealm $Durability

android – java.lang.NoClassDefFoundError:失败的解析:Lio / realm / internal / SharedRealm $Durability

我正在尝试使用Realm创建一个演示应用程序,使用Facebook的stetho浏览default.realm.我每次尝试使用chrome:// inspect>在谷歌浏览器上打开default.realm时出现此错误资源> Web sql> default.realm

这就是我所做的.

的build.gradle

repositories {
    maven {
        url 'https://github.com/uPhyca/stetho-realm/raw/master/maven-repo'
    }
}

dependencies {
    ...
    compile 'com.uphyca:stetho_realm:2.0.0'
    compile 'com.facebook.stetho:stetho:1.4.1'
}

我的应用类

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this).build();
        Realm.setDefaultConfiguration(realmConfiguration);

        Stetho.initialize(
                Stetho.newInitializerBuilder(this)
                        .enableDumpapp(Stetho.defaultDumperPluginsprovider(this))
                        .enableWebKitInspector(Stetho.defaultInspectorModulesProvider(this))
                        .build());
    }
}

应用程序崩溃时出现此错误:

E/AndroidRuntime: FATAL EXCEPTION: StethoWorker-main-304
                  Process: example.com.demoapp,PID: 12830
                  java.lang.NoClassDefFoundError: Failed resolution of: Lio/realm/internal/SharedRealm$Durability;
                      at com.uphyca.stetho_realm.RealmPeerManager.openSharedRealm(RealmPeerManager.java:136)
                      at com.uphyca.stetho_realm.RealmPeerManager.openSharedRealm(RealmPeerManager.java:128)
                      at com.uphyca.stetho_realm.RealmPeerManager.getDatabaseTableNames(RealmPeerManager.java:55)
                      at com.uphyca.stetho_realm.Database.getDatabaseTableNames(Database.java:124)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.facebook.stetho.inspector.Methoddispatcher$MethoddispatchHelper.invoke(Methoddispatcher.java:96)
                      at com.facebook.stetho.inspector.Methoddispatcher.dispatch(Methoddispatcher.java:67)
                      at com.facebook.stetho.inspector.ChromeDevtoolsServer.handleRemoteRequest(ChromeDevtoolsServer.java:129)
                      at com.facebook.stetho.inspector.ChromeDevtoolsServer.handleRemoteMessage(ChromeDevtoolsServer.java:111)
                      at com.facebook.stetho.inspector.ChromeDevtoolsServer.onMessage(ChromeDevtoolsServer.java:87)
                      at com.facebook.stetho.websocket.WebSocketSession$1.handleTextFrame(WebSocketSession.java:176)
                      at com.facebook.stetho.websocket.WebSocketSession$1.onCompleteFrame(WebSocketSession.java:136)
                      at com.facebook.stetho.websocket.ReadHandler.readLoop(ReadHandler.java:44)
                      at com.facebook.stetho.websocket.WebSocketSession.handle(WebSocketSession.java:45)
                      at com.facebook.stetho.websocket.WebSocketHandler.doUpgrade(WebSocketHandler.java:117)
                      at com.facebook.stetho.websocket.WebSocketHandler.handleRequest(WebSocketHandler.java:83)
                      at com.facebook.stetho.server.http.LightHttpServer.dispatchToHandler(LightHttpServer.java:84)
                      at com.facebook.stetho.server.http.LightHttpServer.serve(LightHttpServer.java:61)
                      at com.facebook.stetho.inspector.DevtoolsSocketHandler.onAccepted(DevtoolsSocketHandler.java:52)
                      at com.facebook.stetho.server.ProtocolDetectingSocketHandler.onSecured(ProtocolDetectingSocketHandler.java:63)
                      at com.facebook.stetho.server.SecureSocketHandler.onAccepted(SecureSocketHandler.java:33)
                      at com.facebook.stetho.server.LazySocketHandler.onAccepted(LazySocketHandler.java:36)
                      at com.facebook.stetho.server.LocalSocketServer$WorkerThread.run(LocalSocketServer.java:167)
                   Caused by: java.lang.classNotFoundException: Didn't find class "io.realm.internal.SharedRealm$Durability" on path: DexPathList[[dex file "/data/data/example.com.demoapp/files/instant-run/dex/slice-support-annotations-24.2.1_fe2fc8f98c9647365914a4e0809de4cf3bdd5d0f-classes.dex",dex file "/data/data/example.com.demoapp/files/instant-run/dex/slice-slice_9-classes.dex",dex file "/data/data/example.com.demoapp/files/instant-run/dex/slice-slice_8-classes.dex",dex file "/data/data/example.com.demoapp/files/instant-run/dex/slice-slice_7-classes.dex",dex file "/data/data/example.com.demoapp/files/instant-run/dex/slice-slice_6-classes.dex",dex file "/data/data/example.com.demoapp/files/instant-run/dex/slice-slice_5-classes.dex",dex file "/data/data/example.com.demoapp/files/instant-run/dex/slice-slice_4-classes.dex",dex file "/data/data/example.com.demoapp/files/instant-run/dex/slice-slice_3-classes.dex",dex file "/data/data/example.com.demoapp/files/instant-run/dex/slice-slice_2-classes.dex",dex file "/data/data/example.com.demoapp/files/instant-run/dex/slice-slice_1-classes.dex",dex file "/data/data/example.com.demoapp/files/instant-run/dex/slice-slice_0-classes.dex",dex file "/data/data/example.com.demoapp/files/instant-run/dex/slice-realm-android-0.87.4_a528e85f9603c6e64968c150f61258a1621159d0-classes.dex",dex file "/data/data/example.com.demoapp/files/instant-run/dex/slice-jsr305-2.0.1_54f738210fc8c811d7251e6035f61735e3c90b00-classes.dex",dex file "/data/data/example.com.demoapp/files/instant-run/dex/slice-internal_impl-24.2.1_d63817604dbcd8615733073ed4cf6bd29db871e2-classes.dex",dex file "/data/data/example.com.demoapp/files/instant-run/dex/slice-internal_impl-24.2.1_d432bb06a79cf966d865aa5fa44c857409504f1f-classes.dex",dex file "/data/data/example.com.d

解决方法

您正在使用一个极其过时的Realm版本,Stetho-Realm 2.0.0不支持该版本.

您应该使用the 1.x branch of Stetho-Realm打开您的领域.

repositories {
    maven {
        url 'https://github.com/uPhyca/stetho-realm/raw/master/maven-repo'
    }
}

dependencies {
    compile 'com.facebook.stetho:stetho:1.3.1'
    compile 'com.uphyca:stetho_realm:0.9.0'
}

Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property &#...

Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property &#...

 

异常信息:

04-Aug-2014 15:49:27.894 SEVERE [http-apr-8080-exec-5] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [cms] in context with path [/cms] threw exception [Request processing failed; nested exception is org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 6 errors Field error in object ''carContractSearchBox'' on field ''createEndDate'': rejected value []; codes [typeMismatch.carContractSearchBox.createEndDate,typeMismatch.createEndDate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [carContractSearchBox.createEndDate,createEndDate]; arguments []; default message [createEndDate]]; default message [Failed to convert property value of type ''java.lang.String'' to required type ''java.util.Date'' for property ''createEndDate''; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property ''createEndDate'': no matching editors or conversion strategy found]

先说明一下,我们的项目使用的是 Spring MVC。相应的功能是一个简单的 form 表单查询功能,里面有一些日期字段的查询。

相应的解决办法为:

在对应的 controller 中增加属性编辑器:

@InitBinder
protected void initBinder(WebDataBinder binder) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); }

注意这块的 new CustomDateEditor(dateFormat, true) 中的 true,查看 CustomDateEditor 源码可以看到:

/**
 * Create a new CustomDateEditor instance, using the given DateFormat
 * for parsing and rendering.
 * <p>The "allowEmpty" parameter states if an empty String should
 * be allowed for parsing, i.e. get interpreted as null value.
 * Otherwise, an IllegalArgumentException gets thrown in that case.
 * @param dateFormat DateFormat to use for parsing and rendering
 * @param allowEmpty if empty strings should be allowed
 */
public CustomDateEditor(DateFormat dateFormat, boolean allowEmpty) { this.dateFormat = dateFormat; this.allowEmpty = allowEmpty; this.exactDateLength = -1; }

allowEmpty 字段为 true 的时候 form 表单传递的值可以为空。否则会出现 "" 字符串解析为 date 报错。

 

关于Idea 中:No converter found for return value of type: class java.util.ArrayList:Json 格式转换问题的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于$.ajax 函数调接口,报异常 No converter found for return value of type: class java.util.ArrayList、(HttpMessageNotWritableException ) No converter found for return value of type xxxx、android – java.lang.NoClassDefFoundError:失败的解析:Lio / realm / internal / SharedRealm $Durability、Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property &#...的相关信息,请在本站寻找。

本文标签: