Advertisement
Guest User

Untitled

a guest
May 21st, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.90 KB | None | 0 0
  1. package pl.test.soa.rest;
  2.  
  3. import pl.test.soa.entity.Movie;
  4.  
  5. import javax.persistence.EntityManager;
  6. import javax.persistence.Persistence;
  7. import javax.ws.rs.*;
  8. import javax.ws.rs.core.MediaType;
  9. import java.util.List;
  10.  
  11. @Path("/movies")
  12. public class MoviesController {
  13.  
  14.     private EntityManager em = Persistence.createEntityManagerFactory("Movies").createEntityManager();
  15.  
  16.     @GET
  17.     @Path("/hello2")
  18.     @Produces(MediaType.TEXT_PLAIN)
  19.     public String sayPlainTextHello() {
  20.         return "Hello there!";
  21.     }
  22.  
  23.     @GET
  24.     @Produces(MediaType.APPLICATION_JSON)
  25.     public List<Movie> getMovies() {
  26.         List<Movie> list = em.createQuery("select m from Movie m", Movie.class)
  27.                 .getResultList();
  28.         return list;
  29.     }
  30.  
  31.     @GET
  32.     @Path("/{id}")
  33.     @Produces(MediaType.APPLICATION_JSON)
  34.     public Movie getMovie(@PathParam("id") int id) {
  35.         return em.createQuery("select m from Movie m where m.id = :id", Movie.class)
  36.                 .setParameter("id", id)
  37.                 .getSingleResult();
  38.     }
  39.  
  40.     @POST
  41.     @Consumes(MediaType.APPLICATION_JSON)
  42.     public void postMovie(Movie movie) {
  43.         em.getTransaction().begin();
  44.         em.persist(movie);
  45.         em.getTransaction().commit();
  46.     }
  47. }
  48.  
  49.  
  50. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  51.  
  52. package pl.test.soa.entity;
  53.  
  54. import javax.persistence.*;
  55. import javax.xml.bind.annotation.XmlRootElement;
  56.  
  57. @Entity
  58. @Table(name = "MOVIES")
  59. @XmlRootElement(name = "Movie")
  60. public class Movie {
  61.  
  62.     @Id
  63.     @GeneratedValue
  64.     private int id;
  65.     private String title;
  66.     private String uri;
  67.  
  68.     public Movie(String title, String uri) {
  69.         this.title = title;
  70.         this.uri = uri;
  71.     }
  72.  
  73.     public Movie() {
  74.     }
  75.  
  76.     @Column(name = "id")
  77.     public int getId() {
  78.         return id;
  79.     }
  80.  
  81.     public void setId(int id) {
  82.         this.id = id;
  83.     }
  84.  
  85.     @Column(name = "title")
  86.     public String getTitle() {
  87.         return title;
  88.     }
  89.  
  90.     public void setTitle(String title) {
  91.         this.title = title;
  92.     }
  93.  
  94.     @Column(name = "uri")
  95.     public String getUri() {
  96.         return uri;
  97.     }
  98.  
  99.     public void setUri(String uri) {
  100.         this.uri = uri;
  101.     }
  102. }
  103.  
  104.  
  105. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  106. web.xml
  107.  
  108. <?xml version="1.0" encoding="UTF-8"?>
  109. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  110.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  111.          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  112.          version="4.0">
  113.  
  114.     <servlet>
  115.         <servlet-name>Jersey REST Service</servlet-name>
  116.         <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
  117.         <init-param>
  118.             <param-name>com.sun.jersey.config.property.packages</param-name>
  119.             <param-value>pl.test.soa.rest</param-value>
  120.         </init-param>
  121.         <init-param>
  122.             <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
  123.             <param-value>true</param-value>
  124.         </init-param>
  125.         <load-on-startup>1</load-on-startup>
  126.     </servlet>
  127.     <servlet-mapping>
  128.         <servlet-name>Jersey REST Service</servlet-name>
  129.         <url-pattern>/resources/*</url-pattern>
  130.     </servlet-mapping>
  131. </web-app>
  132.  
  133.  
  134. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  135. persistence.xml
  136.  
  137. <?xml version="1.0" encoding="UTF-8"?>
  138. <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">
  139.  
  140.     <persistence-unit name="Movies">
  141.         <class>pl.test.soa.entity.Movie</class>
  142.         <properties>
  143.             <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/pieskot"/>
  144.             <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
  145.             <property name="javax.persistence.jdbc.user" value="root" />
  146.             <property name="javax.persistence.jdbc.password" value="" />
  147.  
  148.             <property name="hibernate.show_sql" value="true" />
  149.             <property name="hibernate.format_sql" value="false" />
  150.             <property name="hibernate.use_sql_comments" value="false" />
  151.             <property name="hibernate.hbm2ddl.auto" value="update"/>
  152.             <!-- albo: MySQLInnoDBDialect -->
  153.             <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
  154.         </properties>
  155.  
  156.  
  157.     </persistence-unit>
  158. </persistence>
  159.  
  160.  
  161.  
  162.  
  163. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  164.  
  165.     <dependencies>
  166.  
  167.         <dependency>
  168.             <groupId>asm</groupId>
  169.             <artifactId>asm</artifactId>
  170.             <version>3.3.1</version>
  171.         </dependency>
  172.         <dependency>
  173.             <groupId>com.sun.jersey</groupId>
  174.             <artifactId>jersey-bundle</artifactId>
  175.             <version>1.19.4</version>
  176.         </dependency>
  177.         <dependency>
  178.             <groupId>org.codehaus.jackson</groupId>
  179.             <artifactId>jackson-mapper-asl</artifactId>
  180.             <version>1.9.9</version>
  181.         </dependency>
  182.         <dependency>
  183.             <groupId>com.sun.jersey</groupId>
  184.             <artifactId>jersey-json</artifactId>
  185.             <version>1.14</version>
  186.         </dependency>
  187.  
  188.     </dependencies>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement