从Web服务上的mongodb检索数据时出错

时间:2016-06-08 03:20:36

标签: spring mongodb rest spring-mvc

我在mongodb的json数据

{ "_id" : NumberLong(2), "_class" : "hello.Record", "cameraid" : "001", "timestamp" : ISODate("2015-06-15T14:45:21.982Z"), "filename" : "yhao.png" }
{ "_id" : NumberLong(3), "_class" : "hello.Record", "cameraid" : "002", "timestamp" : ISODate("2015-06-15T14:45:21.982Z"), "filename" : "ydd.png" }
{ "_id" : NumberLong(4), "_class" : "hello.Record", "cameraid" : "003", "timestamp" : ISODate("2015-06-15T14:45:21.982Z"), "filename" : "ddds.png" }

这是我的模特课

  public class Record {
  private long id;
  private String cameraid;
  private DateTime timestamp;
  private String filename;


public Record(long id, String cameraid, String timestamp, String filename) {
    this.id = id;
    this.cameraid = cameraid;
    this.timestamp = ISODateTimeFormat.dateTime().parseDateTime(timestamp);
    this.filename = filename;
}
//getters & setters

这是我的控制器类。

@RestController

@RequestMapping("/camera")
public class RecordController {
@Autowired
RecordRepository rep;

@RequestMapping(value="list")
public List<Record> getList() {
    return rep.findAll();
}

这是我的MongoRepository类。

import org.springframework.data.mongodb.repository.MongoRepository;

public interface RecordRepository extends MongoRepository<Record, String> {

}

我在春天遇到的错误:

java.lang.IllegalArgumentException: argument type mismatch

我在浏览器上运行我的网址时出错

Failed to instantiate hello.Record using constructor public hello.Record(long,java.lang.String,java.lang.String,java.lang.String) with arguments 2,001,2015-06-15T22:45:21.982+08:00,yhao.png

任何人都知道为什么我有这个错误?我认为错误是日期时间格式。

3 个答案:

答案 0 :(得分:0)

将您的Record更改为此内容;)

public class Record {
    private Long id;
    private String cameraid;
    private DateTime timestamp;
    private String filename;


    public Record(Long id, String cameraid, String timestamp, String filename) {
        this.id = id;
        this.cameraid = cameraid;
        this.timestamp = ISODateTimeFormat.dateTime().parseDateTime(timestamp);
        this.filename = filename;
    }

    // getter and setter
}

答案 1 :(得分:0)

使构造函数参数类型与字段类型完全匹配:

public class Record {
  private long id;
  private String cameraid;
  private DateTime timestamp;
  private String filename;

  public Record(long id, String cameraid, DateTime timestamp, String filename) {
    this.id = id;
    this.cameraid = cameraid;
    this.timestamp = timestamp;
    this.filename = filename;
  }

  //getters & setters
}

正如TheCoder在评论中所说的,如果你没有明确地使用构造函数,你可能只是删除它,Spring会使用字段/ setter。

答案 2 :(得分:0)

您不需要明确定义构造函数。我认为这是由于不匹配的参数类型(类似的东西)。因此,删除构造函数,让JPA通过对setter的反射来处理ResultSet -> Bean转换。

public class Record {

    private long id;
    private String cameraid;
    private DateTime timestamp;
    private String filename;

    // getters of all fields
    // setters of id, cameraid, filename

    // Not sure whether this will work, coz Argument datatype is diff from field datatype.
    public void setTimestamp(String timestamp) {
        this.timestamp = ISODateTimeFormat.dateTime().parseDateTime(timestamp);
    }

    // If the above doesn't work, comment above setter and uncomment below setter.
    // Also you don't need to handle String result to Datatime conversion manually, 
    // coz JPA is capable of converting result value to appropriate DataType 
    // (But does it support Datatime datatype..?)
    /*public void setTimestamp(DateTime timestamp) {
        this.timestamp = timestamp;
    }*/

}