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).

Niciun comentariu:

Trimiteți un comentariu