Bu sınıf tipi enum REOs bir özelliğini içeren bu enumBahar @RequestBody ve Enum değeri
public enum Reos {
VALUE1("A"),VALUE2("B");
private String text;
Reos(String text){this.text = text;}
public String getText(){return this.text;}
public static Reos fromText(String text){
for(Reos r : Reos.values()){
if(r.getText().equals(text)){
return r;
}
}
throw new IllegalArgumentException();
}
}
Ve İnceleme denilen bir sınıf var.
public class Review implements Serializable{
private Integer id;
private Reos reos;
public Integer getId() {return id;}
public void setId(Integer id) {this.id = id;}
public Reos getReos() {return reos;}
public void setReos(Reos reos) {
this.reos = reos;
}
}
Sonunda
@RequestBody ile bir nesne inceleme durumuna bir denetleyici var.
@RestController
public class ReviewController {
@RequestMapping(method = RequestMethod.POST, value = "/reviews")
@ResponseStatus(HttpStatus.CREATED)
public void saveReview(@RequestBody Review review) {
reviewRepository.save(review);
}
}
Ben Orada sorun değil ama
{"reos":"A"}
ile çağırmak zaman
Could not read document: Can not construct instance of com.microservices.Reos from String value 'A': value not one of declared Enum instance names: [VALUE1, VALUE2] at [Source: [email protected]; line: 1, column: 40] (through reference chain: com.microservices.Review["reos"]); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of com.microservices.Reos from String value 'A': value not one of declared Enum instance names: [VALUE1, VALUE2] at [Source: [email protected]; line: 1, column: 40] (through reference chain: com.microservices.Review["reos"])"
ben undertand bu hatayı olsun
{"reos":"VALUE1"}
ile kontrolör çağırmak durumunda sorun, ama ben bilmek istedim Reos enum ile her nesne için Reos.valueof() yerine Reos.fromText() kullandığınızı bildirmenin yolu.
Bu mümkün mü?
JsonCreator açıklaması (2. adım) üzerinde çalıştığım sorunu çözdü. Güzel cevap. –