关于ObjectMapper json转对象的问题,在线等。。。

yiyuzhou00 2015-11-02 09:24:54
UserCar

package com.cyberway.goparking.user.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.cyberway.platform2.support.orm.model.BusinessEntityWithOrganization;
import com.cyberway.platform2.user.model.MemberUser;

/**
* 用户车牌表
* @author yyzhou
* @version v1.0
*/
@Entity
@Table(name = "GP_UserCar")
@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
public class UserCar extends BusinessEntityWithOrganization{
private static final long serialVersionUID = -1465239840030123090L;

/**
* 用户Id
*/
@ManyToOne(targetEntity = MemberUser.class, fetch = FetchType.LAZY)
@JoinColumn(name = "userId", referencedColumnName = "id")
private MemberUser memberUser;

/**
* 车牌号
*/
@Column
private String cardNo;

/**
* 是否首选(默认为 false)
*/
@Column
private Boolean isDefault = false;

/**
* 是否关闭(1:关闭,0:开启)
*/
@Column
private Boolean hasDeleted = true;

public MemberUser getMemberUser() {
return memberUser;
}


public void setMemberUser(MemberUser memberUser) {
this.memberUser = memberUser;
}


public Boolean getIsDefault() {
return isDefault;
}


public void setIsDefault(Boolean isDefault) {
this.isDefault = isDefault;
}


public Boolean getHasDeleted() {
return hasDeleted;
}


public void setHasDeleted(Boolean hasDeleted) {
this.hasDeleted = hasDeleted;
}


public String getCardNo() {
return cardNo;
}


public void setCardNo(String cardNo) {
this.cardNo = cardNo;
}


}


UserCarList:

package com.cyberway.goparking.utils;

import java.util.List;

import com.cyberway.goparking.user.model.UserCar;

public class UserCarList {
private List<UserCar> userCar;

public List<UserCar> getUserCar() {
return userCar;
}

public void setUserCar(List<UserCar> userCar) {
this.userCar = userCar;
}

}


测试代码:


/**
* List测试
*/
@RequestMapping(value = "/listJson")
public @ResponseBody Object listJson(HttpServletRequest request,String userId) throws Exception
{
String str = URLDecoder.decode(request.getParameter("parm"),"UTF-8");
System.out.println(str);
List<UserCar> list = null;
UserCar userCar = null;
UserCarList userCarList = objectMapper.readValue(str, UserCarList.class);
List<UserCar> uc = userCarList.getUserCar();

return new DataResult("xx");
}


异常:
{"userCar":[{"cardNo":"1234","isDefault":"1"}]}
WARN :[11/02 21:11:07,463] com.cyberway.goparking.user.controller.UserController(qtp4089278-22); :发生异常
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
at [Source: {"userCar":[{"cardNo":"1234","isDefault":"1"}]}; line: 1, column: 13] (through reference chain: com.cyberway.goparking.utils.UserCarList["userCar"]->java.util.ArrayList[0])
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:854)
at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:62)
at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:11)
at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:3703)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2006)
at com.fasterxml.jackson.core.JsonParser.readValueAs(JsonParser.java:1506)
at com.cyberway.platform2.support.hibernate.type.json.jackson.HibernateEntityDeserializer.deserialize(HibernateEntityDeserializer.java:22)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:245)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:217)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:25)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:520)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:95)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:256)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:125)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3731)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2724)
at com.cyberway.goparking.user.controller.UserController.listJson(UserController.java:417)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:111)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:806)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:729)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:594)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1366)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:316)
at com.cyberway.platform2.security.core.web.DataInitFilter.doFilterInternal(DataInitFilter.java:76)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at com.cyberway.platform2.security.core.web.RequestInterceptor.doFilterInternal(RequestInterceptor.java:51)
at com.cyberway.platform2.support.web.filter.AbstractFilter.doFilter(AbstractFilter.java:28)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at com.cyberway.platform2.user.services.OnlineUserServiceImpl.doFilterInternal(OnlineUserServiceImpl.java:127)
at com.cyberway.platform2.support.web.filter.AbstractFilter.doFilter(AbstractFilter.java:28)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:149)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:120)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:205)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:91)

按照网上方法做的 一直行不通。但用JsonObject就行 谁能告诉我哪里错了。谢谢了 弄了一天了 每次走到这一步就报错: UserCarList userCarList = objectMapper.readValue(str, UserCarList.class);

...全文
413 点赞 收藏 7
写回复
7 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
objectMapper 这是个什么东东
回复
sinat_31535993 2015-11-03
引用 2 楼 yiyuzhou00 的回复:
{"carNo":[{"cardNo":"1234","isDefault":"1"}]} 这是str内容
这样看carNo这不是成一个对象了?
回复
科學信仰 2015-11-03
引用 2 楼 yiyuzhou00 的回复:
{"carNo":[{"cardNo":"1234","isDefault":"1"}]} 这是str内容
看上去这格式是符合规范的?如果是这样,那肯定是其它什么地方有问题,可能哦,还缺少一些类型的映射信息
回复
longbaopyl 2015-11-03
String str="{\"studentList\":[{\"id\":\"leilei\",\"name\":\"23\"},{\"id\":\"leilei02\",\"name\":\"23\"}]}"; List<Student> list = new ArrayList<Student>(); Student stu=new Student(); Student stu2=new Student(); stu.setId("1"); stu.setName("jack"); stu2.setId("2"); stu2.setName("rose"); list.add(stu); list.add(stu2); StudentList students=new StudentList(); students.setStudents(list); ObjectMapper objectMapper=new ObjectMapper(); String json2 = objectMapper.writeValueAsString(students); System.out.println(json2); System.out.println(str); StudentList studentList=objectMapper.readValue(json2, StudentList.class); List<Student> lists=studentList.getStudents(); for(Student s:lists){ System.out.println(s.getName()+" "+s.getId()); }
回复
longbaopyl 2015-11-03
估计是你的str有问题,但是我没看出来,贴上上面代码的执行结果 {"students":[{"id":"1","name":"jack"},{"id":"2","name":"rose"}]} {"studentList":[{"id":"leilei","name":"23"},{"id":"leilei02","name":"23"}]} jack 1 rose 2
回复
yiyuzhou00 2015-11-02
{"carNo":[{"cardNo":"1234","isDefault":"1"}]} 这是str内容
回复
科學信仰 2015-11-02
打印出来检查下参数str的内容是不是符合转化为UserCarList 的标准呢?
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2015-11-02 09:24
社区公告
暂无公告