如何在springframework中插入mongodb文档

时间:2016-08-22 04:52:39

标签: spring mongodb spring-mvc spring-data-mongodb

我有一些问题 我已经在网页上提出了关于扫描Web漏洞的应用程序 我已将spring mvc pattern应用到我的应用程序中 我的应用程序包括控制器和服务以及业务对象(制作python)和DAO(使用MongoDB)和DTO Business Object是使python语言扫描Web漏洞的主要模块 我的申请流程如下 网页(插入扫描漏洞的目标网址) - > controller(传递的url) - > service(传递url) - >业务对象(扫描Web漏洞并将结果返回给服务) - > DAO(将结果保存到MongoDB)

返回业务对象结果的数据保存了每个DTO(Total 3 DTO)。 每个DTO如下。

//scaninfoDTO
@Document(collection = "scanresult")
public class scanInfoDTO {
@Id
private String _id;

private String url;
private String date;
private String code;

public scanInfoDTO() {
    super();

}

public scanInfoDTO(String url, String date, String _id, String code) {
    super();
    this._id = _id;
    this.url = url;
    this.date = date;
    this.code = code;
}
 below getter/setter method 
 ....

scaninfoDTO保存网址,日期和目标。

//forminfoDTO
@Document(collection = "scanresult")
public class formInfoDTO {
@Id
String _id;
private String form_name;
private String tagid;

public formInfoDTO() {
    super();
}

public formInfoDTO(String form_name, String tagid,String _id) {
    super();
    this._id = _id;
    this.form_name = form_name;
    this.tagid = tagid;
}
//below getter/setter method 

forminfoDTO保存formname,tagid和objectid。

//vulinfoDTO
@Document(collection = "scanresult")
public class vulInfoDTO {

@Id
String _id;

private String vul_name;
private String patterm_id;
private String pattern;
private String pattern_dspt;
private String code;

public vulInfoDTO() {
    super();
}

public vulInfoDTO(String vul_name, String patterm_id, String pattern, String pattern_dspt, String _id,
        String code) {
    super();
    this._id = _id;
    this.vul_name = vul_name;
    this.patterm_id = patterm_id;
    this.pattern = pattern;
    this.pattern_dspt = pattern_dspt;
    this.code = code;
}
// below getter/setter method

vulinfoDTO保存vulname,patternid,pattern,patterndesp和code。

public class scanDTO {

private formInfoDTO forminfo;
private scanInfoDTO scaninfo;
private List<vulInfoDTO> vulinfo;

public scanDTO() {

}

public scanDTO(scanInfoDTO scaninfo, formInfoDTO forminfo, List<vulInfoDTO> vulinfo) {
    super();
    this.forminfo = forminfo;
    this.scaninfo = scaninfo;
    this.vulinfo = vulinfo;
}

然后是3个DTO,其中包括业务对象合并scanDTO和传递服务的结果。

@Service
public class ScannerServiceImpl implements ScannerService {

@Qualifier("dao")
@Autowired
ScannerDAO dao;

@Override
public void process_run(String url) {

    //create business object
    ScanningUrl scanBl = new ScanningUrl();
    //url passed to business object and save the results to scanDTO. 
    scanDTO result = scanBl.scan(url);
    System.out.println("process_run end!!!");

    //extract each dto from scanDTO result and save each dto
    scanInfoDTO scan = result.getScaninfo();
    formInfoDTO form = result.getForminfo();
    List<vulInfoDTO> vulne = result.getVulinfo();


    System.out.println("[service][scaninfo] url: "+scan.getUrl()+"date: "+scan.getDate());
    System.out.println("[service][forminfo] form_name: "+form.getForm_name()+" tagid: "+form.getTagid()+" code: "+form.get_id());
    System.out.println("[service][vulinfo] "+vulne.size());

    //Each dto which saved result passed DAO
    dao.scanInsert(scan);
    //dao.vulListInsert(vulne);
    //dao.formInsert(form);

DAO如下。

@Repository("dao")
public class ScannerDAOImpl implements ScannerDAO {
@Autowired 
MongoTemplate mongoTemplate;

@Override
public void scanInsert(scanInfoDTO scanresult) {
    mongoTemplate.insert(scanresult);

}

@Override
public void formInsert(formInfoDTO formresult) {
    mongoTemplate.insert(formresult);
}

@Override
public void vulListInsert(List<vulInfoDTO> vulresult) {
    mongoTemplate.insertAll(vulresult);
}

结果如下mongodb。

enter image description here

现在结果将mongodb集合中的不同文档分开。

所以我想合并这样的结果..我该怎么办? enter image description here

还有一个问题 如何在弹簧文件中插入多个关于数组的值 例如,像这样的第二张图片中的forminfo数组。 请帮帮我......

0 个答案:

没有答案