Ошибка при получении данных из mongodb на веб-сервисе
Мои данные JSON в Mongodb
{ "_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
Ошибка при запуске моего URL в браузере
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 ответа
Измени свой 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
}
Сделайте так, чтобы типы аргументов конструктора точно соответствовали типам полей:
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 будет использовать поля / сеттеры.
Вам не нужно явно определять конструктор. Я полагаю, это связано с несовпадающим типом аргумента (что-то подобное). Так что удалите конструктор и дайте JPA обработать 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;
}*/
}