Попытка создать спокойный сервис для сохранения "PeriodeEnseignement", как показано ниже:
package DomainModel.Enseignement.Notations;
import java.time.MonthDay;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.format.annotation.DateTimeFormat;
import com.fasterxml.jackson.annotation.JsonFormat;
import DomainModel.Enseignement.UniteeEnseignement;
@Entity
@Table(name = "PERIODES_ENSEIGNEMENTS")
public class PeriodeEnseignement implements UniteeEnseignement {
private Integer id;
private String nom;
private MonthDay debut;
private MonthDay fin;
@Id
@Column(name = "ID")
@GeneratedValue(generator = "UseExistingOrGenerateIdGenerator")
@GenericGenerator(name = "UseExistingOrGenerateIdGenerator", strategy = "UsefulEntities.UseExistingOrGenerateIdGenerator")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Basic
@Column(name = "NOM")
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
@JsonFormat(pattern = "dd/MM")
@DateTimeFormat(pattern = "dd/MM")
public MonthDay getDebut() {
return debut;
}
public void setDebut(MonthDay debut) {
this.debut = debut;
}
@JsonFormat(pattern = "dd/MM")
@DateTimeFormat(pattern = "dd/MM")
public MonthDay getFin() {
return fin;
}
public void setFin(MonthDay fin) {
this.fin = fin;
}
}
Но что-то не так с аннотированными полями:
@JsonFormat(pattern = "dd/MM")
@DateTimeFormat(pattern = "dd/MM")
Я уже сохранил некоторые другие сущности и работает как шарм, но они не содержат атрибутов MonthDay (он отлично работает с сущностями, которые содержат LocalDate).
вот мой restfulcontroller:
@RestController
public class PeriodeEnseignementServices implements GenericUniteeEnseignementService<PeriodeEnseignement> {
@RequestMapping(value = "/periodeEnseignementService/registerPeriodeEnseignementService", method = RequestMethod.POST)
private GenericResponse registerPeriodeEnseignement(PeriodeEnseignement periodeEnseignement) {
return this.insertNewUniteeEnseignement(periodeEnseignement, this.periodeEnseignementRepository);
}
}
На стороне клиента я получил эту ошибку:
jquery-3.3.1.js:9600 POST http://localhost:8080/periodeEnseignementService/registerPeriodeEnseignementService 400
Так как правильно маршаллировать / демаршировать атрибуты MonthDay?
Заранее спасибо
{timestamp: 1552686924075, status: 400, error: "Bad Request", errors: [{,…}, {,…}],…}
error: "Bad Request"
errors: [{,…}, {,…}]
0: {,…}
arguments: [{codes: ["periodeEnseignement.debut", "debut"], arguments: null, defaultMessage: "debut",…}]
0: {codes: ["periodeEnseignement.debut", "debut"], arguments: null, defaultMessage: "debut",…}
arguments: null
code: "debut"
codes: ["periodeEnseignement.debut", "debut"]
0: "periodeEnseignement.debut"
1: "debut"
defaultMessage: "debut"
bindingFailure: true
code: "typeMismatch"
codes: ["typeMismatch.periodeEnseignement.debut", "typeMismatch.debut", "typeMismatch.java.time.MonthDay",…]
0: "typeMismatch.periodeEnseignement.debut"
1: "typeMismatch.debut"
2: "typeMismatch.java.time.MonthDay"
3: "typeMismatch"
defaultMessage: "Failed to convert property value of type 'java.lang.String' to required type 'java.time.MonthDay' for property 'debut'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@com.fasterxml.jackson.annotation.JsonFormat @org.springframework.format.annotation.DateTimeFormat java.time.MonthDay] for value '07/02'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [07/02]"
field: "debut"
objectName: "periodeEnseignement"
rejectedValue: "07/02"
1: {,…}
arguments: [{codes: ["periodeEnseignement.fin", "fin"], arguments: null, defaultMessage: "fin", code: "fin"}]
0: {codes: ["periodeEnseignement.fin", "fin"], arguments: null, defaultMessage: "fin", code: "fin"}
arguments: null
code: "fin"
codes: ["periodeEnseignement.fin", "fin"]
0: "periodeEnseignement.fin"
1: "fin"
defaultMessage: "fin"
bindingFailure: true
code: "typeMismatch"
codes: ["typeMismatch.periodeEnseignement.fin", "typeMismatch.fin", "typeMismatch.java.time.MonthDay",…]
0: "typeMismatch.periodeEnseignement.fin"
1: "typeMismatch.fin"
2: "typeMismatch.java.time.MonthDay"
3: "typeMismatch"
defaultMessage: "Failed to convert property value of type 'java.lang.String' to required type 'java.time.MonthDay' for property 'fin'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@com.fasterxml.jackson.annotation.JsonFormat @org.springframework.format.annotation.DateTimeFormat java.time.MonthDay] for value '01/12'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [01/12]"
field: "fin"
objectName: "periodeEnseignement"
rejectedValue: "01/12"
message: "Validation failed for object='periodeEnseignement'. Error count: 2"
path: "/periodeEnseignementService/registerPeriodeEnseignementService"
status: 400
timestamp: 1552686924075
[РЕДАКТИРОВАТЬ # 2]
После удаления @DateTimeFormat из атрибутов, добавления @RequestBody и обнародования метода register я получил еще одну ошибку в консоли клиента:
POST http://localhost:8080/periodeEnseignementService/registerPeriodeEnseignementService 415
send @ jquery-3.3.1.js:9600
ajax @ jquery-3.3.1.js:9206
jQuery.(anonymous function) @ jquery-3.3.1.js:9355
(anonymous) @ utils_end_file.js:13
dispatch @ jquery-3.3.1.js:5183
elemData.handle @ jquery-3.3.1.js:4991
error: "Unsupported Media Type"
message: "Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported"
path: "/periodeEnseignementService/registerPeriodeEnseignementService"
status: 415
timestamp: 1552811644305
[РЕДАКТИРОВАТЬ # 3]
После перехода с post на ajax на стороне клиента для отправки запроса в формате json я получил еще одну ошибку
error: "Bad Request"
message: "JSON parse error: Cannot construct instance of `DomainModel.Enseignement.Notations.PeriodeEnseignement` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('id=5&nom=sdsdfsfd&debut=13%2F03&fin=17%2F03'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `DomainModel.Enseignement.Notations.PeriodeEnseignement` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('id=5&nom=sdsdfsfd&debut=13%2F03&fin=17%2F03')↵ at [Source: (PushbackInputStream); line: 1, column: 1]"
path: "/periodeEnseignementService/registerPeriodeEnseignementService"
status: 400
timestamp: 1552820002498
Примечание. PeriodeEnseignement имеет конструктор по умолчанию.