Для простого приложения для управления клиентами у меня возникли проблемы с добавлением даты рождения!
вот мой HTML:
<label for="dob" class="firstThreeInputs">Date of Birth: </label>
<input type="date" [(ngModel)]="customer.dateOfBirth" class="" id="dateOfBirthInput" name="dateOfBirth" placeholder="Date of Birth">
Когда я добавляю нового клиента или редактирую существующего, я получаю следующую ошибку:
Failed executing PUT /customers/6: org.jboss.resteasy.spi.ReaderException: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.time.LocalDate` (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('2018-12-12')
Похоже, этообщая проблема, и есть решение с Джексоном, но я не могу понять, как реализовать это в моем приложении.
Ниже приведен мой класс клиента:
import java.time.LocalDate;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
//Data and Integration
@Entity
@Table(name ="basic_info")
@NamedQuery(name="Customers.selectAll", query="SELECT n From Customers n")
public class Customers {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Column(length=100, nullable=false)
private String first_name;
@Column(length=100, nullable=false)
private String last_name;
@Column(length=100, nullable=false)
private String country_code;
@Column(length=100, nullable=false)
private String gender;
@Column(nullable=false)
private LocalDate dateOfBirth;
@Column(nullable=false)
private boolean isActive;
public Customers() {}
public Customers(Long id, String first_name, String last_name, LocalDate dateOfBirth) {
this.id = id;
this.first_name = first_name;
this.last_name = last_name;
this.dateOfBirth = dateOfBirth;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return first_name;
}
public void setFirstName(String first_name) {
this.first_name = first_name;
}
public String getLastName() {
return last_name;
}
public void setLastName(String last_name) {
this.last_name = last_name;
}
public LocalDate getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(LocalDate dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public String getCountryCode() {
return country_code;
}
public void setCountryCode(String country_code) {
this.country_code = country_code;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public boolean getActiveStatus() {
return isActive;
}
public void setActiveStatus(boolean isActive) {
this.isActive = isActive;
}
@Override
public String toString() {
return "Cusomters [id=" + id + ", First Name =" + first_name + ", Last Name=" + last_name + " DateOfBirth="+ dateOfBirth +" Gender= " + gender + "CountryCode=" + country_code + "]";
}
}
Как добавить код Джексона, чтобы мой JSON конвертировался в Java на PUT / POST и наоборот во время GET?Как использовать ObjectMapper и JavaTimeModule?
Ниже приведен мой класс ресурсов клиента:
package at.technikumwien.webf;
import java.net.URI;
import java.util.List;
import java.util.logging.Logger;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.core.Response.Status;
@Path("/customers")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class CustomersResource {
private static final Logger LOGGER = Logger.getLogger(CustomersResource.class.getName());
@PersistenceContext
private EntityManager em;
@Inject
private CustomersService customersService;
@Context
private UriInfo uriInfo;
@POST
@Transactional
public Response create(Customers customer) {
LOGGER.info("create >> customer=" + customer);
em.persist(customer);
URI uri = uriInfo.getAbsolutePathBuilder().path(customer.getId().toString()).build();
return Response.created(uri).build();
}
@PUT
@Path("/{id}")
@Transactional
public void update(@PathParam("id") long id, Customers customerUpdate) {
LOGGER.info("update >> id=" + id + ", customer=" + customerUpdate);
Customers customerOld = em.find(Customers.class, id);
if ( customerOld != null) {
customerOld.setFirstName(customerUpdate.getFirstName());
customerOld.setLastName(customerUpdate.getLastName());
customerOld.setDateOfBirth(customerUpdate.getDateOfBirth());
customerOld.setGender(customerUpdate.getGender());
customerOld.setCountryCode(customerUpdate.getCountryCode());
customerOld.setActiveStatus(customerUpdate.getActiveStatus());
}
else {
throw new WebApplicationException(Status.NOT_FOUND);
}
}
@DELETE
@Path("/{id}")
@Transactional
public void delete(@PathParam("id") long id) {
LOGGER.info("delete >> id=" + id);
Customers customer = em.find(Customers.class, id);
if (customer != null) {
em.remove(customer);
}
else {
throw new WebApplicationException(Status.NOT_FOUND);
}
}
@GET
@Path("/{id}")
public Customers retrieve(@PathParam("id") long id) {
LOGGER.info("retrieve id=" + id);
return em.find(Customers.class, id);
}
@GET
@Path("/")
public List<Customers> retrieveAll() {
LOGGER.info("retrieveAll");
return customersService.getAllCustomers();
}
}