Twitter

sâmbătă, 17 noiembrie 2018

Java EE 8 -> JSON-B + JAX-RS + JPA Entities

This is an example of annotating a JPA entity via JSON-B (JSR 367). We use the well-known @Column to shape the database columns names and JSON-B suite to shape the JSON returned to the client. We rely on @JsonbProperty, @JsonbTransient, @JsonbDateFormat, @JsonbNumberFormat, but more are available in javax.json.bind.annotation.
The important part here is the the Product entity listed below:

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDate;
import javax.json.bind.annotation.JsonbDateFormat;
import javax.json.bind.annotation.JsonbNumberFormat;
import javax.json.bind.annotation.JsonbProperty;
import javax.json.bind.annotation.JsonbTransient;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

@Entity
@Table(name = "products")
public class Product implements Serializable {
 
    private static final long serialVersionUID = 1L;

    @Id
    @NotNull
    @Column(name = "id")
    @JsonbProperty(nillable = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @JsonbProperty("product-name")
    @Column(name = "product_name")
    private String name;

    @JsonbProperty(value = "product-brand", nillable = true)
    @Column(name = "brand")
    private String brand;

    @JsonbTransient
    @Column(name = "random_sku")
    private int sku;

    @JsonbProperty("product-arrival-date")
    @JsonbDateFormat("dd-MM-yyyy")
    @Column(name = "arrival_date")
    private LocalDate arrivalDate;

    @JsonbProperty("product-price")
    @Column(name = "price", precision = 11, scale = 2)
    private BigDecimal price;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
     
    @JsonbNumberFormat(locale = "en_US", value = "#0.0")
    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public String getName() {
        return name;
    }

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

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public int getSku() {
        return sku;
    }

    public void setSku(int sku) {
        this.sku = sku;
    }

    public LocalDate getArrivalDate() {
        return arrivalDate;
    }

    public void setArrivalDate(LocalDate arrivalDate) {
        this.arrivalDate = arrivalDate;
    }
}



Example of executing the application from Google Chrome, ARC extension:
  • add a new product via a JSON

  • load an existing product by id
The complete application is available on GitHub. I tested on Payara 5 and Derby DB (jdbc/sample).

vineri, 16 noiembrie 2018

Starting Java EE 8 on NetBeans 8.2 and Payara 5

This is an example of starting a Java EE 8 project on NetBeans 8.2 and Payara 5. I suppose that you already have installed NetBeans 8.2 and Payara 5:

  • In NetBeans, File | New Project | Maven | Project from Archetype. Click Next.


  • In NetBeans, Search filed type search airhacks.com artifact, javaee8-essentials-archetype. Optionally, configure the archetype and click Next.
  • In NetBeans, set up the project name, location, etc. Click Finish.
  • In NetBeans, if everything works fine, you should obtain a project like below:
  • In NetBeans, Build and Run (don't forget to attach Payara server to the project):
  • In the browser, access: http://localhost:8080/quickstart/resources/ping. If everything works as expected you will see the message from the picture below.

This works because the generated quickstart application exposes a JAX-RS endpoint. Just check the source code of PingResource class.

Done! Source code on GitHub.