我在我的webservice项目中遇到错误,我正在使用mysql和jersey,这是我的代码和stacktrace:
AccountResource.java:
else if (userInput === movie) {
Account.java:
package com.mycompany.mavenproject1;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/Customers")
public class AccountResource {
//===========================================
//= Attributes
//===========================================
private EntityManagerFactory emf = Persistence.createEntityManagerFactory("test");
private EntityManager em = emf.createEntityManager();
private EntityTransaction tx = em.getTransaction();
//===========================================
//= Constructor
//===========================================
//===========================================
//= REST API methods
// //===========================================
//
@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public List<Customer> getCustomers() {
return allEntries();
}
public List<Customer> allEntries() {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Customer> cq = cb.createQuery(Customer.class);
Root<Customer> rootEntry = cq.from(Customer.class);
CriteriaQuery<Customer> all = cq.select(rootEntry);
TypedQuery<Customer> allQuery = em.createQuery(all);
return allQuery.getResultList();
}
@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("{id}")
public Customer getCustomer(@PathParam("id") int id) {
Customer test = em.find(Customer.class, id);
em.close();
return test;
}
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Path("/Account/{id}")
public Account getAccount(@PathParam("id") int AccountId) {
Account test = em.find(Account.class, AccountId);
em.close();
return test;
}
@POST
@Path("/transactions")
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Account saver(Account p) {
Account test = em.find(Account.class, p.getAccountId());
if (test == null) {
Transaction tran1 = new Transaction();
tran1.setType("Debit");
tran1.setDate("16-09-1990");
tran1.setDescription("this is a test 1");
tran1.setTbalance(400);
tran1.setTransact(80);
tran1.setCard("1111");
em.persist(tran1);
p.getTransactions().add(tran1);
tx.begin();
em.persist(p);
tx.commit();
em.close();
}
return p;
}
@PUT
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Path("/update/{id}/{amount}")
public Customer UpdateAccount(
@PathParam("id") int id,
@PathParam("amount") double amount, Customer b) {
Customer test = em.find(Customer.class, b.getId());
if (test != null) {
Account a1 = new Account();
double balance = a1.getBalance();
double bal = balance + amount;
a1.setBalance(bal);
tx.begin();
em.remove(test);
em.persist(a1);
b.getAccount().add(a1);
tx.commit();
em.close();
}
return b;
}
@POST
@Path("/save")
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Customer save(Customer p) {
Customer test = em.find(Customer.class, p.getId());
if (test == null) {
tx.begin();
em.persist(p);
tx.commit();
em.close();
}
return p;
}
@POST
@Path("/newAccount")
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Customer newAccount(Customer p) {
Customer test = em.find(Customer.class, p.getId());
if (test == null) {
Account a1 = new Account();
a1.setAccountNumber("4234324234");
a1.setBalance(200);
Account a2 = new Account();
a2.setAccountNumber("5654675756");
a2.setBalance(400);
em.persist(a1);
em.persist(a2);
p.getAccount().add(a1);
p.getAccount().add(a2);
tx.begin();
em.persist(p);
tx.commit();
em.close();
}
return p;
}
@DELETE
@Path("/{id}")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public void deleteItem(@PathParam("id") int id) {
Customer test = em.find(Customer.class, id);
if (test !=null) {
tx.begin();
em.remove(test);
tx.commit();
em.close();
}
}
}
Customer.java:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.mycompany.mavenproject1;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity
public class Account implements Serializable {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private int AccountId;
private String AccountNumber;
private double balance;
private String sortcode;
@OneToMany(fetch=FetchType.EAGER)
private Collection<Transaction> transactions = new ArrayList<Transaction>();
public Collection<Transaction> getTransactions() {
return transactions;
}
public void setTransactions(Collection<Transaction> transactions) {
this.transactions = transactions;
}
//
// public Transaction getTransactions() {
// return transactions;
// }
//
// public void setTransactions(Transaction transactions) {
// this.transactions = transactions;
// }
public String getSortcode() {
return sortcode;
}
public void setSortcode(String sortcode) {
this.sortcode = sortcode;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public int getAccountId() {
return AccountId;
}
public void setAccountId(int AccountId) {
this.AccountId = AccountId;
}
public String getAccountNumber() {
return AccountNumber;
}
public void setAccountNumber(String AccountNumber) {
this.AccountNumber = AccountNumber;
}
}
Transaction.java:
package com.mycompany.mavenproject1;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.xml.bind.annotation.XmlRootElement;
@Entity
// @Table
@XmlRootElement
public class Customer implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
@OneToMany(fetch=FetchType.EAGER)
private Collection<Account> account = new ArrayList<Account>();
public Collection<Account> getAccount() {
return account;
}
public void setAccount(Collection<Account> account) {
this.account = account;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Persistance.xml:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.mycompany.mavenproject1;
import java.io.Serializable;
import javax.persistence.Embeddable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import static javax.ws.rs.client.Entity.entity;
//@Embeddable // Tells hibernate that this class is embedded as part of another class
@Entity
public class Transaction implements Serializable {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String type;
private String date;
private String description;
private double tbalance;
private double transact;
private String card;
public double getTransact() {
return transact;
}
public void setTransact(double transact) {
this.transact = transact;
}
public double getTbalance() {
return tbalance;
}
public void setTbalance(double tbalance) {
this.tbalance = tbalance;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getCard() {
return card;
}
public void setCard(String card) {
this.card = card;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
的pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
<class>com.mycompany.mavenproject1.Account</class>
<class>com.mycompany.mavenproject1.Customer</class>
<class>com.mycompany.mavenproject1.Transaction</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/bank"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.validation.mode" value="none"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
堆栈跟踪:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>BankExample</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>BankingSkeleton</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.4.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.4.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.6.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.7</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources/META-INF</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>7.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.4.v20141103</version>
<configuration>
<httpConnector>
<host>127.0.0.1</host>
</httpConnector>
<scanIntervalSeconds>3</scanIntervalSeconds>
<webAppConfig>
<!-- defaultsDescriptor>src/test/resources/jetty-maven-plugin-webdefault.xml</defaultsDescriptor -->
</webAppConfig>
</configuration>
</plugin>
</plugins>
</build>
</project>
文件路径: Image of File Path