Spring RESTful webservice + Hibernate

时间:2017-08-15 07:59:00

标签: java spring hibernate rest web-services

我在执行我的休息服务时遇到错误,这里是代码:

MyResources.java

import java.math.BigDecimal;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.adp.model.AllCustomerHist;
import com.adp.service.AllCustomerHistService;

/**
 * Root resource (exposed at "myresource" path)
 */
@Path("customer")
public class MyResource {

    @Autowired
    private AllCustomerHistService AllCustomerHistService;
    /**
     * Method handling HTTP GET requests. The returned object will be sent
     * to the client as "text/plain" media type.
     *
     * @return String that will be returned as a text/plain response.
     */
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getIt() {
        return "Test Rest Service!";
    }

    @GET
    @Path("/list")
    @Produces("application/json")
    public List<AllCustomerHist> getAllCust() {
        return AllCustomerHistService.getAllCustomer();
    }

    @GET
    @Path("/list/{adpId}")
    @Produces("application/json")
    public AllCustomerHist getCust(@PathVariable("adpId") BigDecimal adpId) {
        return AllCustomerHistService.getCustomer(adpId);
    }

    @POST
    @Path("/add")
    @Consumes("application/json")
    public ResponseEntity createCust(@RequestBody AllCustomerHist allcustomerhist){
       AllCustomerHistService.addCustomer(allcustomerhist);
       return new ResponseEntity(HttpStatus.OK);
    }
}

这是我的实体 AllCustomerHist.java

import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name="ALL_CUSTOMER_HIST")
public class AllCustomerHist {
    @Column
    private String reffFlag;
    @Column
    private BigDecimal reffId;
    @Column
    private Date lastUpdatedDate;
    @Column
    private Date createdDate;
    @Column
    private String customerId;
    @Column
    private String name;
    @Column
    private String dob;
    @Column
    private String birthPlace;
    @Column
    private String gender;
    @Column
    private String religion;
    @Column
    private String education;
    @Column
    private String employeeType;
    @Column
    private String maritalStatus;
    @Column
    private String address;
    @Column
    private String city;
    @Column
    private String kecamatan;
    @Column
    private String kelurahan;
    @Column
    private String zipCode;
    @Column
    private String bpsCode;
    @Column
    private String idType;
    @Column
    private String idNumber;
    @Column
    private String npwp;
    @Column
    private String homePhone1;
    @Column
    private String homePhone2;
    @Column
    private String homePhone3;
    @Column
    private String homePhone4;
    @Column
    private String homePhone5;
    @Column
    private String officePhone1;
    @Column
    private String officePhone2;
    @Column
    private String officePhone3;
    @Column
    private String officePhone4;
    @Column
    private String officePhone5;
    @Column
    private String mobilePhone1;
    @Column
    private String mobilePhone2;
    @Column
    private String mobilePhone3;
    @Column
    private String mobilePhone4;
    @Column
    private String mobilePhone5;
    @Column
    private String faxNumber1;
    @Column
    private String faxNumber2;
    @Column
    private String email1;
    @Column
    private String email2;
    @Column
    private String email3;
    @Column
    private String srctable;
    @Column
    private BigDecimal adpId;
    @Column
    private BigDecimal systemId;


    public String getReffFlag() {
        return reffFlag;
    }


    public void setReffFlag(String reffFlag) {
        this.reffFlag = reffFlag;
    }


    public BigDecimal getReffId() {
        return reffId;
    }


    public void setReffId(BigDecimal reffId) {
        this.reffId = reffId;
    }


    public Date getLastUpdatedDate() {
        return lastUpdatedDate;
    }


    public void setLastUpdatedDate(Date lastUpdatedDate) {
        this.lastUpdatedDate = lastUpdatedDate;
    }


    public Date getCreatedDate() {
        return createdDate;
    }


    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }


    public String getCustomerId() {
        return customerId;
    }


    public void setCustomerId(String customerId) {
        this.customerId = customerId;
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public String getDob() {
        return dob;
    }


    public void setDob(String dob) {
        this.dob = dob;
    }


    public String getBirthPlace() {
        return birthPlace;
    }


    public void setBirthPlace(String birthPlace) {
        this.birthPlace = birthPlace;
    }


    public String getGender() {
        return gender;
    }


    public void setGender(String gender) {
        this.gender = gender;
    }


    public String getReligion() {
        return religion;
    }


    public void setReligion(String religion) {
        this.religion = religion;
    }


    public String getEducation() {
        return education;
    }


    public void setEducation(String education) {
        this.education = education;
    }


    public String getEmployeeType() {
        return employeeType;
    }


    public void setEmployeeType(String employeeType) {
        this.employeeType = employeeType;
    }


    public String getMaritalStatus() {
        return maritalStatus;
    }


    public void setMaritalStatus(String maritalStatus) {
        this.maritalStatus = maritalStatus;
    }


    public String getAddress() {
        return address;
    }


    public void setAddress(String address) {
        this.address = address;
    }


    public String getCity() {
        return city;
    }


    public void setCity(String city) {
        this.city = city;
    }


    public String getKecamatan() {
        return kecamatan;
    }


    public void setKecamatan(String kecamatan) {
        this.kecamatan = kecamatan;
    }


    public String getKelurahan() {
        return kelurahan;
    }


    public void setKelurahan(String kelurahan) {
        this.kelurahan = kelurahan;
    }


    public String getZipCode() {
        return zipCode;
    }


    public void setZipCode(String zipCode) {
        this.zipCode = zipCode;
    }


    public String getBpsCode() {
        return bpsCode;
    }


    public void setBpsCode(String bpsCode) {
        this.bpsCode = bpsCode;
    }


    public String getIdType() {
        return idType;
    }


    public void setIdType(String idType) {
        this.idType = idType;
    }


    public String getIdNumber() {
        return idNumber;
    }


    public void setIdNumber(String idNumber) {
        this.idNumber = idNumber;
    }


    public String getNpwp() {
        return npwp;
    }


    public void setNpwp(String npwp) {
        this.npwp = npwp;
    }


    public String getHomePhone1() {
        return homePhone1;
    }


    public void setHomePhone1(String homePhone1) {
        this.homePhone1 = homePhone1;
    }


    public String getHomePhone2() {
        return homePhone2;
    }


    public void setHomePhone2(String homePhone2) {
        this.homePhone2 = homePhone2;
    }


    public String getHomePhone3() {
        return homePhone3;
    }


    public void setHomePhone3(String homePhone3) {
        this.homePhone3 = homePhone3;
    }


    public String getHomePhone4() {
        return homePhone4;
    }


    public void setHomePhone4(String homePhone4) {
        this.homePhone4 = homePhone4;
    }


    public String getHomePhone5() {
        return homePhone5;
    }


    public void setHomePhone5(String homePhone5) {
        this.homePhone5 = homePhone5;
    }


    public String getOfficePhone1() {
        return officePhone1;
    }


    public void setOfficePhone1(String officePhone1) {
        this.officePhone1 = officePhone1;
    }


    public String getOfficePhone2() {
        return officePhone2;
    }


    public void setOfficePhone2(String officePhone2) {
        this.officePhone2 = officePhone2;
    }


    public String getOfficePhone3() {
        return officePhone3;
    }


    public void setOfficePhone3(String officePhone3) {
        this.officePhone3 = officePhone3;
    }


    public String getOfficePhone4() {
        return officePhone4;
    }


    public void setOfficePhone4(String officePhone4) {
        this.officePhone4 = officePhone4;
    }


    public String getOfficePhone5() {
        return officePhone5;
    }


    public void setOfficePhone5(String officePhone5) {
        this.officePhone5 = officePhone5;
    }


    public String getMobilePhone1() {
        return mobilePhone1;
    }


    public void setMobilePhone1(String mobilePhone1) {
        this.mobilePhone1 = mobilePhone1;
    }


    public String getMobilePhone2() {
        return mobilePhone2;
    }


    public void setMobilePhone2(String mobilePhone2) {
        this.mobilePhone2 = mobilePhone2;
    }


    public String getMobilePhone3() {
        return mobilePhone3;
    }


    public void setMobilePhone3(String mobilePhone3) {
        this.mobilePhone3 = mobilePhone3;
    }


    public String getMobilePhone4() {
        return mobilePhone4;
    }


    public void setMobilePhone4(String mobilePhone4) {
        this.mobilePhone4 = mobilePhone4;
    }


    public String getMobilePhone5() {
        return mobilePhone5;
    }


    public void setMobilePhone5(String mobilePhone5) {
        this.mobilePhone5 = mobilePhone5;
    }


    public String getFaxNumber1() {
        return faxNumber1;
    }


    public void setFaxNumber1(String faxNumber1) {
        this.faxNumber1 = faxNumber1;
    }


    public String getFaxNumber2() {
        return faxNumber2;
    }


    public void setFaxNumber2(String faxNumber2) {
        this.faxNumber2 = faxNumber2;
    }


    public String getEmail1() {
        return email1;
    }


    public void setEmail1(String email1) {
        this.email1 = email1;
    }


    public String getEmail2() {
        return email2;
    }


    public void setEmail2(String email2) {
        this.email2 = email2;
    }


    public String getEmail3() {
        return email3;
    }


    public void setEmail3(String email3) {
        this.email3 = email3;
    }


    public String getSrctable() {
        return srctable;
    }


    public void setSrctable(String srctable) {
        this.srctable = srctable;
    }


    public BigDecimal getAdpId() {
        return adpId;
    }


    public void setAdpId(BigDecimal adpId) {
        this.adpId = adpId;
    }


    public BigDecimal getSystemId() {
        return systemId;
    }


    public void setSystemId(BigDecimal systemId) {
        this.systemId = systemId;
    }

    public AllCustomerHist(){}

    public AllCustomerHist(String reffFlag, BigDecimal reffId, Date lastUpdatedDate, Date createdDate,
            String customerId, String name, String dob, String birthPlace, String gender, String religion,
            String education, String employeeType, String maritalStatus, String address, String city, String kecamatan,
            String kelurahan, String zipCode, String bpsCode, String idType, String idNumber, String npwp,
            String homePhone1, String homePhone2, String homePhone3, String homePhone4, String homePhone5,
            String officePhone1, String officePhone2, String officePhone3, String officePhone4, String officePhone5,
            String mobilePhone1, String mobilePhone2, String mobilePhone3, String mobilePhone4, String mobilePhone5,
            String faxNumber1, String faxNumber2, String email1, String email2, String email3, String srctable,
            BigDecimal adpId, BigDecimal systemId) {
        super();
        this.reffFlag = reffFlag;
        this.reffId = reffId;
        this.lastUpdatedDate = lastUpdatedDate;
        this.createdDate = createdDate;
        this.customerId = customerId;
        this.name = name;
        this.dob = dob;
        this.birthPlace = birthPlace;
        this.gender = gender;
        this.religion = religion;
        this.education = education;
        this.employeeType = employeeType;
        this.maritalStatus = maritalStatus;
        this.address = address;
        this.city = city;
        this.kecamatan = kecamatan;
        this.kelurahan = kelurahan;
        this.zipCode = zipCode;
        this.bpsCode = bpsCode;
        this.idType = idType;
        this.idNumber = idNumber;
        this.npwp = npwp;
        this.homePhone1 = homePhone1;
        this.homePhone2 = homePhone2;
        this.homePhone3 = homePhone3;
        this.homePhone4 = homePhone4;
        this.homePhone5 = homePhone5;
        this.officePhone1 = officePhone1;
        this.officePhone2 = officePhone2;
        this.officePhone3 = officePhone3;
        this.officePhone4 = officePhone4;
        this.officePhone5 = officePhone5;
        this.mobilePhone1 = mobilePhone1;
        this.mobilePhone2 = mobilePhone2;
        this.mobilePhone3 = mobilePhone3;
        this.mobilePhone4 = mobilePhone4;
        this.mobilePhone5 = mobilePhone5;
        this.faxNumber1 = faxNumber1;
        this.faxNumber2 = faxNumber2;
        this.email1 = email1;
        this.email2 = email2;
        this.email3 = email3;
        this.srctable = srctable;
        this.adpId = adpId;
        this.systemId = systemId;
    }


}

Dao界面 AllCustomerHistDao.java

import java.math.BigDecimal;
import java.util.List;

import com.adp.model.AllCustomerHist;

public interface AllCustomerHistDao {
    public void addCustomer(AllCustomerHist allcustomerhist);
    public void editCustomer(AllCustomerHist allcustomerhist);
    public AllCustomerHist getCustomer(BigDecimal adpId);
    public List getAllCustomer();

}

DaoImpl课程 AllCustomerHistDaoImpl.java

import java.math.BigDecimal;
import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.adp.dao.AllCustomerHistDao;
import com.adp.model.AllCustomerHist;

@Repository
public class AllCustomerHistDaoImpl implements AllCustomerHistDao {
    @Autowired
    private SessionFactory session;

    @Override
    public void addCustomer(AllCustomerHist allcustomerhist) {
        // TODO Auto-generated method stub
        session.getCurrentSession().save(allcustomerhist);
    }

    @Override
    public void editCustomer(AllCustomerHist allcustomerhist) {
        // TODO Auto-generated method stub
        session.getCurrentSession().update(allcustomerhist);
    }

    @Override
    public AllCustomerHist getCustomer(BigDecimal adpId) {
        // TODO Auto-generated method stub
        return (AllCustomerHist)session.getCurrentSession().get(AllCustomerHist.class, adpId);
    }

    @Override
    public List getAllCustomer() {
        // TODO Auto-generated method stub
        System.out.println("begin");
        Query q = session.getCurrentSession().createQuery("from AllCustomerHist");
        System.out.println("session = " + session.getCurrentSession());
        System.out.println("query = " + q);
        return q.list();


//      return session.getCurrentSession().createCriteria(AllCustomerHist.class).list();

    }

}

这是我的数据服务 AllCustomerHistService.java

import java.math.BigDecimal;
import java.util.List;

import com.adp.model.AllCustomerHist;

public interface AllCustomerHistService {
    public void addCustomer(AllCustomerHist allcustomerhist);
    public void editCustomer(AllCustomerHist allcustomerhist);
    public AllCustomerHist getCustomer(BigDecimal adpId);
    public List getAllCustomer();
}

服务类impl AllCustomerHistServiceImpl.java

import java.math.BigDecimal;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.adp.dao.AllCustomerHistDao;
import com.adp.model.AllCustomerHist;
import com.adp.service.AllCustomerHistService;
@Service
public class AllCustomerHistServiceImpl implements AllCustomerHistService {
    @Autowired
    private AllCustomerHistDao allcustomerhistdao;

    @Transactional
    public void addCustomer(AllCustomerHist allcustomerhist) {
        // TODO Auto-generated method stub
        allcustomerhistdao.addCustomer(allcustomerhist);
    }

    @Transactional
    public void editCustomer(AllCustomerHist allcustomerhist) {
        // TODO Auto-generated method stub
        allcustomerhistdao.editCustomer(allcustomerhist);
    }

    @Transactional
    public AllCustomerHist getCustomer(BigDecimal adpId) {
        // TODO Auto-generated method stub
        return allcustomerhistdao.getCustomer(adpId);
    }

    @Transactional
    public List getAllCustomer() {
        // TODO Auto-generated method stub
        return allcustomerhistdao.getAllCustomer();
    }

}

问题是,当我部署并点击http://localhost:7001/adpwebapi/customer/list时,它会抛出:

  

休息开始   &lt; [ServletContext @ 17211 44​​524 [app:adp.service.layer   module:adp.service.layer.war path:null spec-version:   3.1],request:weblogic.servlet.internal.ServletRequestImpl@43d1999b [GET /adp.service.layer/adpwebapi/customer/list HTTP / 1.1 User-Agent:   Mozilla / 5.0(Windows NT 6.2; WOW64; rv:54.0)Gecko / 20100101 Firefox /   54.0接受:text / html,application / xhtml + xml,application / xml; q = 0.9, / ; q = 0.8   Accept-Language:id,en-US; q = 0.7,en; q = 0.3 Accept-Encoding:gzip,   deflate Connection:keep-alive Upgrade-Insecure-Requests:1

     

]] ServletException的根本原因。显示java.lang.NullPointerException           在com.id.astra.MyResource.getAllCustomer(MyResource.java:56)           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)           at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl。   Java的:62)           at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces   sorImpl.java:43)           at java.lang.reflect.Method.invoke(Method.java:498)           截断。查看日志文件以获取完整的堆栈跟踪   &GT;

我不知道line发生了什么:return AllCustomerHistService.getAllCustomer();抛出空指针异常

请给我建议,因为我是hibernate和spring framework的新手

此致

0 个答案:

没有答案
相关问题