Spring MongoDB Repository仅在某些情况下有效

时间:2016-02-21 17:58:27

标签: java spring mongodb jhipster

我有一个基于jHipster(Java + MongoDB + AngularJS)的项目。我正在使用mongodb和Spring MongoDB存储库来保持数据持久性。

问题是存储库中的相同方法(称为findOneByEntityId)在某些情况下的工作方式不同。

案例1: Angular资源要求控制器获取TermMap。上述控制器使用方法。结果是成功的。

案例2: DataMapFactory尝试使用相同参数的相同方法并获取NullPointerException。结果是失败。

案例3: 我也尝试从DataMapFactory调用控制器方法,但它也给了我NullPointerException。

存储库代码:

package com.xxx.yyy.repository;


import com.xxx.yyy.domain.maps.TermsMap;
import org.springframework.data.mongodb.repository.MongoRepository;

public interface TermsMapRepository extends MongoRepository<TermsMap,String> {
    TermsMap findOneByEntityId(String entityId);
}

带方法的控制器代码(我已经剥离了非必要的方法)

package com.xxx.yyy.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.xxx.yyy.domain.maps.TermsMap;
import com.xxx.yyy.repository.TermsMapRepository;
import com.xxx.yyy.web.rest.util.HeaderUtil;
import com.xxx.yyy.web.rest.util.PaginationUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api")
public class TermMapResource {
    private final Logger log = LoggerFactory.getLogger(TermMapResource.class);

    @Inject
    private TermsMapRepository tmRepository;

    /**
     * GET  /termmaps/:id -> get the "id" company.
     */
    @RequestMapping(value = "/termmaps/{id}",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public ResponseEntity<TermsMap> getTM(@RequestParam(required = false) String field, @PathVariable String id) {
        switch (field) {
            case "entityId":    log.debug("REST request to get TM by entityId: {}", id);
                                return Optional.ofNullable(tmRepository.findOneByEntityId(id))
                                .map(tm -> new ResponseEntity<>(
                                    tm,
                                    HttpStatus.OK))
                                .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
            default:        log.debug("REST request to get TM : {}", id);
                            return Optional.ofNullable(tmRepository.findOne(id))
                            .map(tm -> new ResponseEntity<>(
                                tm,
                                HttpStatus.OK))
                            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
        }
    }    
}

存储库代码:

package com.xxx.yyy.domain.factories;

import com.xxx.yyy.domain.maps.TermsMap;
import com.xxx.yyy.repository.TermsMapRepository;
import com.xxx.yyy.web.rest.TermMapResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Optional;

public class DataMapsFactory {
    private static DataMapsFactory instance;

    private final Logger log = LoggerFactory.getLogger(DataMapsFactory.class);

    @Inject
    private TermsMapRepository termsMapRepository;

    public DataMapsFactory(){

    }

    public static DataMapsFactory getInstance(){
        if(instance == null) {
            instance = new DataMapsFactory();
        }
        return instance;
    }

    public ArrayList<String> getDataMapForVehicle(String id) {
        log.debug("Getting data map for: "+id);
        try {
            TermsMap map = termsMapRepository.findOneByEntityId(id);
            log.debug("fetched map: "+map);
            return map.getTermsIds();
        } catch (Exception e) {
            e.printStackTrace();
            return null;

        }
    }
}

任何帮助都会非常感激,因为我很擅长这一点。 如果您需要任何其他信息,请写:-) 提前感谢所有答案。

1 个答案:

答案 0 :(得分:0)

As mlk内部无法正常工作,问题在于我的单例实现导致弹出容器出现问题。我已将DataMapsFactory的代码更改为:

package com.njugroup.flotilla.domain.factories;

import com.njugroup.flotilla.domain.maps.TermsMap;
import com.njugroup.flotilla.repository.TermsMapRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class DataMapsFactory{

    private final Logger log = LoggerFactory.getLogger(TermsMapsFactory.class);

    @Autowired
    private TermsMapRepository termsMapRepository;

    public TermsMap getDataMapForVehicle(String id) {
        log.debug("Getting data map for: "+id);
        try {
            Optional<TermsMap> map = Optional.ofNullable(termsMapRepository.findOneByEntityId(id));
            log.debug("fetched map: "+map);
            return map.get();
        } catch (Exception e) {
            e.printStackTrace();
            return null;

        }
    }
}

现在它就像一个魅力。