私はこの列挙春@RequestBodyと列挙値
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();
}
}
とレビューというクラスを持っているが、このクラスは、型列挙Reosのプロパティが含まれています。
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"}
で起動したときに、私は私がundertand
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"])"
このエラーが出ます問題は、私はReos enumを持つすべてのオブジェクトがReos.valueof()の代わりにReos.fromText()を使用することをSpringに伝える方法。
これは可能ですか?
JsonCreatorアノテーション(ステップ2)は、私が取り組んでいた問題を解決しました。いい答え。 –