预约了科目四然后密码锁忘记密码怎么办被锁了1天没事吧?

jackson - java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.testing.models.Account - Stack Overflow
Join the Stack Overflow Community
Stack Overflow is a community of 7.1 million programmers, just like you, helping each other.
J it only takes a minute:
I'm getting below error:
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.testing.models.Account
with below code
final int expectedId = 1;
Test newTest = create();
int expectedResponseCode = Response.SC_OK;
ArrayList&Account& account = given().when().expect().statusCode(expectedResponseCode)
.get("accounts/" + newTest.id() + "/users")
.as(ArrayList.class);
assertThat(account.get(0).getId()).isEqualTo(expectedId);
Is there a reason why I cannot do get(0)?
2,7521056104
The issue's coming from Jackson. When it doesn't have enough information on what class to deserialize to, it uses LinkedHashMap.
Since you're not informing Jackson of the element type of your ArrayList, it doesn't know that you want to deserialize into an ArrayList of Accounts.
So it falls back to the default.
Instead, you could probably use as(JsonNode.class), and then deal with the ObjectMapper in a richer manner than rest-assured allows.
Something like this:
ObjectMapper mapper = new ObjectMapper();
JsonNode accounts = given().when().expect().statusCode(expectedResponseCode)
.get("accounts/" + newClub.getOwner().getCustId() + "/clubs")
.as(JsonNode.class);
//Jackson's use of generics here are completely unsafe, but that's another issue
List&Account& accountList = mapper.readValue(
mapper.treeAsTokens(accounts),
new TypeReference&List&Account&&(){}
assertThat(accountList.get(0).getId()).isEqualTo(expectedId);
58.2k9111156
Did you find this question interesting? Try our newsletter
Sign up for our newsletter and get our top new questions delivered to your inbox ().
Subscribed!
Success! Please click the link in the confirmation email to activate your subscription.
Try the following:
POJO pojo = mapper.convertValue(singleObject, POJO.class);
List&POJO& pojos = mapper.convertValue(listOfObjects, new TypeReference&List&POJO&&() { });[conversion of Linkedhashmap][1]
1,24411030
I had a similar exception (but different problem) -
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to org.bson.Document , and fortunately it's solved easier:
Instead of
List&Document& docs = obj.get("documents");
Document doc = docs.get(0)
which gives error on second line, One can use
List&Document& docs = obj.get("documents");
Document doc = new Document(docs.get(0));
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
rev .25906
Stack Overflow works best with JavaScript enabledjava.lang.ClassCastException: $Proxy8 cannot be cast to com.qieast.platform.admincp.dao.impl.UserDaoImplat com.qieast.platform.admincp.dao.test.UserDaoImplTest.setUp(UserDaoImplTest.java:22)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)at java.lang.reflect.Method.invoke(Method.java:597)at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)at org.junit.runners.ParentRunner.run(ParentRunner.java:236)at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
无相关信息
最新教程周点击榜
微信扫一扫java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to asd.Grade - Stack Overflow
Join the Stack Overflow Community
Stack Overflow is a community of 7.1 million programmers, just like you, helping each other.
J it only takes a minute:
i have a large scale project in my hands but i simulated the problem i'm struggling with in this example:
my first class:
import java.io.S
public class Grade implements Serializable{
public String getName() {
public Grade() {}
public Grade(String name, Integer score) {
this.name =
this.score =
public void setName(String name) {
this.name =
public Integer getScore() {
public void setScore(Integer score) {
this.score =
my second class:
import java.io.S
public class Student implements Serializable {
public String getName() {
public Student(String name, Object grade) {
this.name =
public Student() {}
public void setName(String name) {
this.name =
public Object getGrade() {
public void setGrade(Object grade) {
and this is my main class:
import com.google.gson.G
import com.google.gson.GsonB
import java.io.*;
public class Test {
public static void main(String[] args){
Student s1 = new Student();
s1.setName("JeanPierre");
s1.setGrade(new Grade("Math", 8));
Gson gson = new GsonBuilder().create();
String convertedToJson = gson.toJson(s1);
System.out.println("Json string: " + convertedToJson);
Student s2 = gson.fromJson(convertedToJson, Student.class);
System.out.println("Student Name: " + s2.getName());
System.out.println("Grade Name: " + ((Grade)s2.getGrade()).getName());
System.out.println("Grade Score: " + ((Grade)s2.getGrade()).getScore());
Json string is :
{"name":"JeanPierre","Grade":{"name":"Math","score":8}}
Student Name: JeanPierre
Exception in thread "main" java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to asd.Grade
at asd.Test.main(Test.java:24)
My problem is when i call:
System.out.println("Grade Name: " + ((Grade)s2.getGrade()).getName());
System.out.println("Grade Score: " + ((Grade)s2.getGrade()).getScore());
i get this exception:
Exception in thread "main" java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to asd.Grade
at asd.Test.main(Test.java:24)
Declaration and setting of the Grade in Student is syntactically wrong. Not sure how it's even building like that.
public class Student implements Serializable
protected S
protected G
public Student( String name, Grade grade )
this.setName(name).setGrade(grade) ;
public String getName()
{ return this. }
public Student setName( String name )
this.name =
public Grade getGrade()
{ return this. }
public Student setGrade( Grade grade )
this.grade =
You need to parse the grade class as well. It won't convert the entire complex object in one attempt. Try the below code:
import com.google.gson.G
import com.google.gson.GsonB
public class Test {
public static void main(String[] args){
Student s1 = new Student();
s1.setName("JeanPierre");
s1.setGrade(new Grade("Math", 8));
Gson gson = new GsonBuilder().create();
String convertedToJson = gson.toJson(s1);
System.out.println("Json string: " + convertedToJson);
Student s2 = gson.fromJson(convertedToJson, Student.class);
System.out.println("Student Name: " + s2.getName());
Grade g = gson.fromJson(s2.getGrade().toString(), Grade.class);
System.out.println("Grade Name: " + g.getName());
System.out.println("Grade Score: " + g.getScore());
Here s2.getGrade().toString() is still a valid JSON string. You are converting that to Grade class and using it. This is the right way to parse complex objects. Hope you understood.
I changed everything to XML and using XStream now, which works on my tests. Thanks for everyone's answers.
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
The week's top questions and answers
Important community announcements
Questions that need answers
By subscribing, you agree to the
rev .25906
Stack Overflow works best with JavaScript enabled

我要回帖

更多关于 密码锁忘记密码怎么办 的文章

 

随机推荐