使用来自JaxRs Web服务的嵌入式Neo4j数据库

时间:2015-12-28 16:50:51

标签: java tomcat neo4j glassfish jax-rs

所以我试图制作一个使用嵌入式Neo4j数据库的RAX-RS java Web服务。

这是我的代码:

import java.util.HashMap;

import java.util.Map;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import org.neo4j.cypher.ExecutionEngine;
import org.neo4j.cypher.ExecutionResult;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.kernel.impl.util.StringLogger;

@XmlRootElement(name = "company")
public class Company {

    private String pib;
    private String naziv;
    private String adresa;
    private String mesto;
    private String opstina;
    private String delatnost;
    private String mat;
    private String tel;
    private String fax;
    private String email;
    private String website;
    private String korisnik;
    private String lozinka;
    private String prava;
    private String message;
    private String obrisan;

    @XmlElement
    public String getPib() {
        return pib;
    }

    public void setPib(String pib) {
        this.pib = pib;
    }

    @XmlElement
    public String getNaziv() {
        return naziv;
    }

    public void setNaziv(String naziv) {
        this.naziv = naziv;
    }

    @XmlElement
    public String getAdresa() {
        return adresa;
    }

    public void setAdresa(String adresa) {
        this.adresa = adresa;
    }

    @XmlElement
    public String getMesto() {
        return mesto;
    }

    public void setMesto(String mesto) {
        this.mesto = mesto;
    }

    @XmlElement
    public String getOpstina() {
        return opstina;
    }

    public void setOpstina(String opstina) {
        this.opstina = opstina;
    }

    @XmlElement
    public String getDelatnost() {
        return delatnost;
    }

    public void setDelatnost(String delatnost) {
        this.delatnost = delatnost;
    }

    @XmlElement
    public String getMat() {
        return mat;
    }

    public void setMat(String mat) {
        this.mat = mat;
    }

    @XmlElement
    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    @XmlElement
    public String getFax() {
        return fax;
    }

    public void setFax(String fax) {
        this.fax = fax;
    }

    @XmlElement
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @XmlElement
    public String getWebsite() {
        return website;
    }

    public void setWebsite(String website) {
        this.website = website;
    }

    @XmlElement
    public String getKorisnik() {
        return korisnik;
    }

    public void setKorisnik(String korisnik) {
        this.korisnik = korisnik;
    }

    @XmlElement
    public String getLozinka() {
        return lozinka;
    }

    public void setLozinka(String lozinka) {
        this.lozinka = lozinka;
    }

    @XmlElement
    public String getPrava() {
        return prava;
    }

    public void setPrava(String prava) {
        this.prava = prava;
    }

    @XmlElement
    public String getObrisan() {
        return obrisan;
    }

    public void setObrisan(String obrisan) {
        this.obrisan = obrisan;
    }

    @XmlElement
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public static Company getDataUsingPib(String pib) {

        Company c = new Company();
        String DB_PATH = System.getProperty("user.dir") + "\\web\\db\\grapgdb\\companies.graphdb";

        GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH);
        ExecutionEngine engine = new ExecutionEngine(graphDb, StringLogger.SYSTEM);

        ExecutionResult result;

        try (Transaction tx = graphDb.beginTx()) {

            Map<String, Object> params = new HashMap<String, Object>();
            params.put("pib", pib);
            String query = "MATCH (n) where n.pib={pib} RETURN n";
            result = engine.execute(query, params);

            scala.collection.Iterator<Node> it = result.columnAs("n");
            while (it.hasNext()) {

                Node n1 = it.next();

                c.setAdresa(n1.getProperty("adresa").toString());
                c.setNaziv(n1.getProperty("naziv").toString());
                c.setPib(n1.getProperty("pib").toString());
                c.setMesto(n1.getProperty("mesto").toString());
                c.setOpstina(n1.getProperty("opstina").toString());
                c.setDelatnost(n1.getProperty("delatnost").toString());
                c.setMat(n1.getProperty("mat").toString());
                c.setTel(n1.getProperty("tel").toString());
                c.setFax(n1.getProperty("fax").toString());
                c.setEmail(n1.getProperty("email").toString());
                c.setWebsite(n1.getProperty("website").toString());
                c.setKorisnik(n1.getProperty("korisnik").toString());
                c.setLozinka(n1.getProperty("lozinka").toString());
                c.setPrava(n1.getProperty("prava").toString());
                c.setMessage("Kompanija postoji");
                c.setObrisan(n1.getProperty("obrisan").toString());

            }

            result.close();
            tx.success();
        }
        graphDb.shutdown();

        return c;
    }

@Override
public String toString() {
    return "Company{" + "pib=" + pib + ", naziv=" + naziv + ", adresa=" + adresa + ", mesto=" + mesto + ", opstina=" + opstina + ", delatnost=" + delatnost + ", mat=" + mat + ", tel=" + tel + ", fax=" + fax + ", email=" + email + ", website=" + website + ", korisnik=" + korisnik + ", lozinka=" + lozinka + ", prava=" + prava + ", message=" + message + ", obrisan=" + obrisan + '}';
}

}

这是我的服务:

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;


@Path("/company")
public class CompaniesXmlService {

    @GET
    @Path("/{pib}")
    @Produces(MediaType.APPLICATION_XML)
    public Company getCustomerInXML(@PathParam("pib") String pib) {

        Company c =Company.getDataUsingPib(pib);
        return c;

    }

 }

这是我测试类的方法:

public class Test {
    public static void main(String[] args)
    {
    companyPibTest();
    }

    private static void companyPibTest()
    {
   Company c=Company.getDataUsingPib("100000709");
   System.out.println(c.toString());
    }
}

这可能是一个简单的问题,但我想知道为什么我的公共静态测试方法提供了良好的结果,但是当我以相同的方式调用方法时,服务返回null:

Company c=Company.getDataUsingPib("100000709");

PS。

这是我的应用程序类

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/services")
public class CompaniesApplication extends Application {


}

当我填写没有neo4j调用的数据时,我得到了结果。

0 个答案:

没有答案