/java/

Wildfly 10 + JAX-RS + H2 database

2016-04-11 18:43:09

https://github.com/szalek/JEE-Wildfly10-H2

.../src/main/resources/META-INF/persistence.xml

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>

<persistence http:="" ns="" persistence="" persistence_2_1.xsd&quot;="" version='"2.1"' xml="" xmlns='"http://xmlns.jcp.org/xml/ns/persistence"' xmlns.jcp.org="" xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance"' xsi:schemalocation='"http://xmlns.jcp.org/xml/ns/persistence'>
<persistence-unit name='"h2DataBase"' transaction-type='"JTA"'>
<properties>
<property name='"javax.persistence.schema-generation.database.action"' value='"drop-and-create"/'>
</property></properties>
</persistence-unit>
</persistence>

pl.btbw.core.MyApp.java

package pl.btbw.core;

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

@ApplicationPath("/rest")
public class MyApp extends Application {
}

pl.btbw.module.post.Post.java

package pl.btbw.module.post;

import javax.persistence.*;

@Entity
@Table(name = "post")
public class Post {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private long id;

    @Column(name = "name")
    private String name;

    public Post() {
    }

    public Post(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

}

pl.btbw.module.post.PostRepository.java

package pl.btbw.module.post;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class PostRepository {

    @PersistenceContext
    private EntityManager em;

    public void save(Post post) {
        em.persist(post);
        em.flush();
    }

    public Post find(long id) {
        return em.find(Post.class, id);
    }
}

pl.btbw.module.post.PostController.java

package pl.btbw.module.post;

import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

@Path("/post")
public class PostController {

    @Inject
    private PostRepository postRepository;

    @GET
    @Path("/create")
    public String create() {
        postRepository.save(new Post("test 1"));
        postRepository.save(new Post("test 2"));
        postRepository.save(new Post("test 3"));
        return "ok";
    }

    @GET
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Post one(@PathParam("id") long id) {
        return postRepository.find(id);
    }

}

pom.xml

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>

<project http:="" maven-4.0.0.xsd&quot;="" maven.apache.org="" xmlns='"http://maven.apache.org/POM/4.0.0"' xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance"' xsd="" xsi:schemalocation='"http://maven.apache.org/POM/4.0.0'>
<modelversion>4.0.0</modelversion>
<groupid>pl.btbw</groupid>
<artifactid>h2example</artifactid>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupid>javax</groupid>
<artifactid>javaee-api</artifactid>
<version>7.0</version>
</dependency>
</dependencies>
<build>
<finalname>app</finalname>
<plugins>
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-compiler-plugin</artifactid>
<configuration>
<source/>1.8
                    <target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactid>maven-war-plugin</artifactid>
<configuration>
<failonmissingwebxml>false</failonmissingwebxml>
</configuration>
</plugin>
</plugins>
</build>
</project>