尝试从rest api上的mongodb检索数据时出错

时间:2016-06-07 03:53:44

标签: java spring mongodb rest spring-mvc

public class Record {


private DateTime timestamp;
private String filename;
private String cameraid;


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

}
//Getters & setters

(Model)
-----------------------------------------------------------------------------

public class RecordController {
@Autowired
private RecordRepository rep;

@RequestMapping(value="store")
public  String  camera (@RequestParam(required=true, value="ts") String timestamp,
@RequestParam(value="fn",required=true) String filename,
@RequestParam(value="cam",required=true) String cameraid)

{
    try {
        Record cam = new Record(timestamp,filename,cameraid);

        rep.save(cam);
        return "OK";
    }catch(Exception ex){

        String errorMessage;
        errorMessage = ex + " <== error";
        return ("error: " + ex.getMessage());}

}
@RequestMapping(value="li", method=RequestMethod.GET)
public @ResponseBody List<Record> getAll() {
    return rep.findAll();
}
} (Controller)

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

public interface RecordRepository extends MongoRepository<Record, String> {
} (Recpository)

我得到的错误是:

Failed to instantiate hello.Record using constructor public hello.Record(java.lang.String,java.lang.String,java.lang.String) with arguments 2016-06-06T00:19:09.223+08:00,9d9fd7f8f4caec99bce9dff8f4644e41,000000006f4280af

1 个答案:

答案 0 :(得分:0)

添加默认构造函数

public class Record {


private DateTime timestamp;
private String filename;
private String cameraid;

public Record(){} //default constructor

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

}

//getter and setter
}