मैं इस वर्ग के प्रकार enum REOs की संपत्ति में शामिल है इस enumस्प्रिंग @RequestBody और Enum मूल्य
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();
}
}
और एक वर्ग कहा जाता है की समीक्षा है,।
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;
}
}
अंत में मैं एक नियंत्रक कि @RequestBody के साथ एक वस्तु की समीक्षा प्राप्त करता है।
@RestController
public class ReviewController {
@RequestMapping(method = RequestMethod.POST, value = "/reviews")
@ResponseStatus(HttpStatus.CREATED)
public void saveReview(@RequestBody Review review) {
reviewRepository.save(review);
}
}
मैं साथ
{"reos":"VALUE1"}
वहाँ समस्या नहीं है, लेकिन जब मैं
{"reos":"A"}
साथ आह्वान मैं यह त्रुटि
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"])"
मैं undertand मिल नियंत्रक आह्वान हैं समस्या, लेकिन मैं एक जानना चाहता था वसंत को बताने का तरीका है कि Reos enum के साथ प्रत्येक ऑब्जेक्ट के लिए Reos.valomText() के बजाय Reos.fromText() का उपयोग करें।
क्या यह संभव है?
जेसन क्रिएटर एनोटेशन (चरण 2) ने उस समस्या को हल किया जिस पर मैं काम कर रहा था। अच्छा उत्तर। –