rest服务

ojsjcaue  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(240)

我的第一个web应用程序实验有些困难。我在用
jdk15.0.1版本
netbeans 12.0版
MySQL5.7.19版本
payara服务器5.201
windows 10内部版本19041.685
当我创建一个projectweb应用程序(java with maven>web应用程序)时,我编写实体类,但是当我试图生成持久性单元时,它不允许我使用jta。
书籍.java

@XmlRootElement
@Entity
@NamedQueries({
 @NamedQuery(name = "Book.findAll", query = "SELECT d FROM Book d")    
})
public class Book implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String title;
    private String author;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Book)) {
            return false;
        }
        Book other = (Book) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.mycompany.test201.Book[ id=" + id + " ]";
    }

}

我从模式中添加了新的>restful web服务。。。
图书资源.java

@RequestScoped
@Path("book")
public class BookResource {
    @PersistenceContext(unitName = "test201pu")
    private EntityManager em;

//    @Context
//    private UriInfo context;

    /**
     * Creates a new instance of BookResource
     */
    public BookResource() {
    }

    @POST
    @Transactional
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED) 
    public void addBook(@FormParam("aut")String autr, @FormParam("titl")String til) {
        Book b=new Book();
        b.setAuthor(autr);
        b.setTitle(til);
        em.persist(b);

    }
    /**
     * Retrieves representation of an instance of com.mycompany.test201.BookResource
     * @return an instance of java.lang.String
     */
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public List<Book> getXml() {
        Query q=em.createNamedQuery("Book.findAll", Book.class);
        return q.getResultList();
    }

    /**
     * PUT method for updating or creating an instance of BookResource
     * @param content representation for the resource
     */
    @PUT
    @Consumes(MediaType.APPLICATION_XML)
    public void putXml(String content) {
    }
}

我被迫手动更正从transaction type=“resource\u local”到transaction type=“jta”的xml。
持久性.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="test201pu" transaction-type="JTA">
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/test201?zeroDateTimeBehavior=CONVERT_TO_NULL"/>
      <property name="javax.persistence.jdbc.user" value="piero"/>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
      <property name="javax.persistence.jdbc.password" value="my_password"/>
      <property name="javax.persistence.schema-generation.database.action" value="create"/>
    </properties>
  </persistence-unit>
</persistence>

此外,复选框“包括所有实体类在。。。“模块”始终处于禁用状态(与其他复选框“使用java事务API”相同)

最后,当我尝试一些事务时,显然像jdbc一样使用到数据库mysql的连接,数据库中没有表,所以我不知道数据存储在哪里。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题