请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

只成功插入一条就报解析错误了....

代码:

(1) ProductControllerTest:

package com.imooc.manager.controller;

import com.imooc.entity.Product;
import com.imooc.entity.enums.ProductStatus;
import com.imooc.util.RestUtil;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.Assert;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)//随机端口,每次测试随机指定端口
public class ProductControllerTest {

   private static RestTemplate rest = new RestTemplate();

   @Value("http://localhost:${local.server.port}/manager")//获取上面的随机端口
   private String baseUrl;

   //正常产品数据
   private static List<Product> normals = new ArrayList<>();

   @BeforeClass
   public static void init() {
       Product p1 = new Product("T001", "零活宝1号", ProductStatus.AUDITING.name(), BigDecimal.valueOf(10), BigDecimal.valueOf(1), BigDecimal.valueOf(3.42));
       Product p2 = new Product("T002", "理财通2号", ProductStatus.AUDITING.name(), BigDecimal.valueOf(10), BigDecimal.valueOf(0), BigDecimal.valueOf(3.25));
       Product p3 = new Product("T003", "金阳光3号", ProductStatus.AUDITING.name(), BigDecimal.valueOf(100), BigDecimal.valueOf(10), BigDecimal.valueOf(3.28));
       normals.add(p1);
       normals.add(p2);
       normals.add(p3);
   }

   @Test
   public void create() {
       normals.forEach(product -> {
           Product result = RestUtil.postJSON(rest, baseUrl + "/products", product, Product.class);
           Assert.notNull(result.getCreateAt(), "创建失败");
       });
   }
}


(2) Product:


package com.imooc.entity;
/**
* 产品
*/

import javax.persistence.Entity;
import javax.persistence.Id;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

@Entity//表示和数据库的表对应,实体类名与表名一致,可以省略(name="product")
public class Product implements Serializable {
   @Id
   private String id;

   private String name;
   /**
    * 产品状态
    *
    * @see com.imooc.entity.enums.ProductStatus
    */
   private String status;
   //起投金额
   private BigDecimal thresholdAmount;
   //投资步长
   private BigDecimal stepAmount;
   //锁定期
   private Integer lockTerm;
   //收益率,因为要与其他数相乘,所以使用BigDecimal
   private BigDecimal rewardRate;

   private String memo;

   private Date createAt;

   private Date updateAt;

   private String createUser;

   private String updateUser;

   public Product() {
   }

   public Product(String id, String name, String status, BigDecimal thresholdAmount, BigDecimal stepAmount, BigDecimal rewardRate) {
       this.id = id;
       this.name = name;
       this.status = status;
       this.thresholdAmount = thresholdAmount;
       this.stepAmount = stepAmount;
       this.rewardRate = rewardRate;
   }

   public String getId() {
       return id;
   }

   public void setId(String id) {
       this.id = id;
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public String getStatus() {
       return status;
   }

   public void setStatus(String status) {
       this.status = status;
   }

   public BigDecimal getThresholdAmount() {
       return thresholdAmount;
   }

   public void setThresholdAmount(BigDecimal thresholdAmount) {
       this.thresholdAmount = thresholdAmount;
   }

   public BigDecimal getStepAmount() {
       return stepAmount;
   }

   public void setStepAmount(BigDecimal stepAmount) {
       this.stepAmount = stepAmount;
   }

   public Integer getLockTerm() {
       return lockTerm;
   }

   public void setLockTerm(Integer lockTerm) {
       this.lockTerm = lockTerm;
   }

   public BigDecimal getRewardRate() {
       return rewardRate;
   }

   public void setRewardRate(BigDecimal rewardRate) {
       this.rewardRate = rewardRate;
   }

   public String getMemo() {
       return memo;
   }

   public void setMemo(String memo) {
       this.memo = memo;
   }

   public Date getCreateAt() {
       return createAt;
   }

   public void setCreateAt(Date createAt) {
       this.createAt = createAt;
   }

   public Date getUpdateAt() {
       return updateAt;
   }

   public void setUpdateAt(Date updateAt) {
       this.updateAt = updateAt;
   }

   public String getCreateUser() {
       return createUser;
   }

   public void setCreateUser(String createUser) {
       this.createUser = createUser;
   }

   public String getUpdateUser() {
       return updateUser;
   }

   public void setUpdateUser(String updateUser) {
       this.updateUser = updateUser;
   }

   @Override
   public String toString() {
       return "Product{" +
               "id='" + id + '\'' +
               ", name='" + name + '\'' +
               ", status='" + status + '\'' +
               ", thresholdAmount=" + thresholdAmount +
               ", stepAmount=" + stepAmount +
               ", lockTerm=" + lockTerm +
               ", rewardRate=" + rewardRate +
               ", memo='" + memo + '\'' +
               ", createAt=" + createAt +
               ", updateAt=" + updateAt +
               ", createUser='" + createUser + '\'' +
               ", updateUser='" + updateUser + '\'' +
               '}';
   }
}


(3) console信息:


2018-05-07 21:04:39.073  INFO 5324 --- [o-auto-1-exec-1] c.i.m.controller.ProductController       : 创建产品,参数:Product{id='T001', name='零活宝1号', status='AUDITING', thresholdAmount=10, stepAmount=1, lockTerm=null, rewardRate=3.42, memo='null', createAt=null, updateAt=null, createUser='null', updateUser='null'}

Hibernate: select product0_.id as id1_1_0_, product0_.create_at as create_a2_1_0_, product0_.create_user as create_u3_1_0_, product0_.lock_term as lock_ter4_1_0_, product0_.memo as memo5_1_0_, product0_.name as name6_1_0_, product0_.reward_rate as reward_r7_1_0_, product0_.status as status8_1_0_, product0_.step_amount as step_amo9_1_0_, product0_.threshold_amount as thresho10_1_0_, product0_.update_at as update_11_1_0_, product0_.update_user as update_12_1_0_ from product product0_ where product0_.id=?

Hibernate: insert into product (create_at, create_user, lock_term, memo, name, reward_rate, status, step_amount, threshold_amount, update_at, update_user, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

2018-05-07 21:04:39.211  INFO 5324 --- [o-auto-1-exec-1] c.i.m.controller.ProductController       : 创建产品,结果:Product{id='T001', name='零活宝1号', status='AUDITING', thresholdAmount=10, stepAmount=1, lockTerm=0, rewardRate=3.42, memo='null', createAt=Mon May 07 21:04:39 CST 2018, updateAt=Mon May 07 21:04:39 CST 2018, createUser='null', updateUser='null'}


org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Can not deserialize value of type java.util.Date from String "2018-05-07 21:04:39": not a valid representation (error: Failed to parse Date value '2018-05-07 21:04:39': Can not parse date "2018-05-07 21:04:39Z": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS'Z'', parsing fails (leniency? null)); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize value of type java.util.Date from String "2018-05-07 21:04:39": not a valid representation (error: Failed to parse Date value '2018-05-07 21:04:39': Can not parse date "2018-05-07 21:04:39Z": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS'Z'', parsing fails (leniency? null))

 at [Source: java.io.PushbackInputStream@22b7ef2b; line: 1, column: 147] (through reference chain: com.imooc.entity.Product["createAt"])


at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:244)

at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:229)

at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:96)

at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:655)

at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)

at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:380)

at com.imooc.util.RestUtil.postJSON(RestUtil.java:31)

at com.imooc.manager.controller.ProductControllerTest.lambda$create$0(ProductControllerTest.java:44)

at java.util.ArrayList.forEach(ArrayList.java:1257)

at com.imooc.manager.controller.ProductControllerTest.create(ProductControllerTest.java:43)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)

at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)

at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)

at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)

at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)

at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)

at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)

at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)

at org.junit.runners.ParentRunner.run(ParentRunner.java:363)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)

at org.junit.runner.JUnitCore.run(JUnitCore.java:137)

at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)

at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)

at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)

at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Caused by: com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not deserialize value of type java.util.Date from String "2018-05-07 21:04:39": not a valid representation (error: Failed to parse Date value '2018-05-07 21:04:39': Can not parse date "2018-05-07 21:04:39Z": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSS'Z'', parsing fails (leniency? null))

 at [Source: java.io.PushbackInputStream@22b7ef2b; line: 1, column: 147] (through reference chain: com.imooc.entity.Product["createAt"])

at com.fasterxml.jackson.databind.exc.InvalidFormatException.from(InvalidFormatException.java:74)

at com.fasterxml.jackson.databind.DeserializationContext.weirdStringException(DeserializationContext.java:1410)

at com.fasterxml.jackson.databind.DeserializationContext.handleWeirdStringValue(DeserializationContext.java:926)

at com.fasterxml.jackson.databind.deser.std.StdDeserializer._parseDate(StdDeserializer.java:819)

at com.fasterxml.jackson.databind.deser.std.StdDeserializer._parseDate(StdDeserializer.java:788)

at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateBasedDeserializer._parseDate(DateDeserializers.java:172)

at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateDeserializer.deserialize(DateDeserializers.java:259)

at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateDeserializer.deserialize(DateDeserializers.java:242)

at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:504)

at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:104)

at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:357)

at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:148)

at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3798)

at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2922)

at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:241)

... 38 more



正在回答

2回答

时间格式不对"2018-05-07 20:46:31Z",解析成json的时候出错了,看看你创建时间的代码,把Z去掉

0 回复 有任何疑惑可以回复我~
  • 提问者 奥观海同志 #1
    没有Z啊,检查了好几遍,关键是他每次都是第一条插入成功之后,开始报错,我重新贴上了完整信息,请老师帮忙看一下
    回复 有任何疑惑可以回复我~ 2018-05-07 21:06:59
  • skyding 回复 提问者 奥观海同志 #2
    代码贴出来
    回复 有任何疑惑可以回复我~ 2018-05-07 21:10:37
  • 提问者 奥观海同志 回复 skyding #3
    好的,已上
    回复 有任何疑惑可以回复我~ 2018-05-07 21:14:52
car 2018-07-03 12:24:50

我遇到过,问题是这里

date-format: yyyy-MM-dd HH:mm:ss.SSS
time-zone: GMT+8

先注释time-zone: GMT+8

date-format: yyyy-MM-dd HH:mm:ss

0 回复 有任何疑惑可以回复我~
  • 提问者 奥观海同志 #1
    我后来把这两个都剪切掉重新paste一下就好了,有时候莫名其妙不讲道理啊
    回复 有任何疑惑可以回复我~ 2018-08-03 14:23:51
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信