나는 할 때 다음과 같은 오류가 점점 더 JSON request 및 프로세스 있다.
>. 오르조지고데하우스.잭슨.마p.j슨마핑렉세페시옹: 아니 적합한 구성자를 늑대굴에서 문자용 [단순 자료형의, 클래스 코맥미웹스애플스도]: json 객체에는 추가 / 활성화하십시오 하는 타입 정보?) 에서 인스턴스화하지 수 없습니다.
다음은 JSON 실로나는 전송하는:
{
"applesDO" : [
{
"apple" : "Green Apple"
},
{
"apple" : "Red Apple"
}
]
}
내가 가지고 있는 컨트롤러이면 다음과 같은 방법을 서명입니다:
@RequestMapping("showApples.do")
public String getApples(@RequestBody final AllApplesDO applesRequest){
// Method Code
}
는 래퍼 애플스도 앨프리스도 있다.
public class AllApplesDO {
private List<ApplesDO> applesDO;
public List<ApplesDO> getApplesDO() {
return applesDO;
}
public void setApplesDO(List<ApplesDO> applesDO) {
this.applesDO = applesDO;
}
}
애플스도:
public class ApplesDO {
private String apple;
public String getApple() {
return apple;
}
public void setApple(String appl) {
this.apple = apple;
}
public ApplesDO(CustomType custom){
//constructor Code
}
}
내 생각엔 JSON 으로 변환할 수 없는 객체에는 잭슨 자바 클래스. 제발 도와주세요 구성을 통해 마이클 잭슨의 아동 성희롱 매개변수입니다 변환할지 JSON 으로 Java Objects. 스프링 프레임워크 사용하고 있습니다.
편집: 함께 이 문제를 일으키는 주요 버그 해결 방법은 위 예제 클래스 - 수락됨 오토메이티드 확인하십시오.
그래서 결국 문제가 뭔지 깨달았다. 잭슨 구성 문제를 as I 의심하는 것은 아닙니다.
사실 이 문제는 애플스도 롬폭 클래스:
public class ApplesDO {
private String apple;
public String getApple() {
return apple;
}
public void setApple(String apple) {
this.apple = apple;
}
public ApplesDO(CustomType custom) {
//constructor Code
}
}
There was a 에 대해 정의된 클래스 정의 구성자를 있어 기본값입니다 구성자를. 더미 구성자를 소개합니까 카으바를 오류가 go away.
public class ApplesDO {
private String apple;
public String getApple() {
return apple;
}
public void setApple(String apple) {
this.apple = apple;
}
public ApplesDO(CustomType custom) {
//constructor Code
}
//Introducing the dummy constructor
public ApplesDO() {
}
}
이러한 이유로 이런.
정적 클래스 pbs. 잭슨 특정 조건을 {/ / }
정적 클래스 찼음을 pbs. { 긴 개인 id;
공용 조건 () { }
세터 및 게터 // }
또 다른 솔루션을 필요로 하지 않는 구성자를 더미 (dummy) 이 추가 싶다. 이 때문에, 이후 과도한 더미 구성자를 약간 혼란스럽다. 우리는 우리가 제공할 수 있는 안전한 구성자를 잭슨 사이의 매핑을 결정할 수 있도록 인수만 구성자를 달기, 방법으로 구성자를 매개변수입니다 약진.
그래서 다음 또 작동합니까. 필드 이름을 참고합니다 구체화하십시오 δ1 주석 일치해야 합니다.
import com.fasterxml.jackson.annotation.JsonProperty;
public class ApplesDO {
private String apple;
public String getApple() {
return apple;
}
public void setApple(String apple) {
this.apple = apple;
}
public ApplesDO(CustomType custom){
//constructor Code
}
public ApplesDO(@JsonProperty("apple")String apple) {
}
}
주석, 주석 달기 시작하면 구성자를 합니다 모든 보다 작은 자연수이다.
표시문, 내 Staff.name 필드는 " 매핑됨 ANOTHER_NAME"; json 으로 구체화하십시오.
String jsonInString="{\"ANOTHER_NAME\":\"John\",\"age\":\"17\"}";
ObjectMapper mapper = new ObjectMapper();
Staff obj = mapper.readValue(jsonInString, Staff.class);
// print to screen
public static class Staff {
public String name;
public Integer age;
public Staff() {
}
//@JsonCreator - don't need this
public Staff(@JsonProperty("ANOTHER_NAME") String n,@JsonProperty("age") Integer a) {
name=n;age=a;
}
}
어떤 선택을 잭슨 역직렬화를 실현할 합니다 사용할 수 있다. 자바 가상 머신 (jvm) 에 인수, 방법을 컴파일됨 코드 이름은 나타나지 않습니다. # 39, s, t # 39 that& 왜 잭슨 can& 잘 만들 수 있는 모든 것을 주로 사용하고 구성자를 객체에는 이미 설정되었습니다.
따라서, 이 경우 빈 구성자를 및 또한 세터 soundmixer. 빈 구성자를 및 세터. 세터 없는 경우, 일부 어두운 매직 (반사) 을 사용하여 do it.
스케쳐내 생성자를 사용할 수 있는 그의 답변은 @PiersyP 언급한 대로 주석입니다 잭슨 사용해야 합니다. 빌더 패턴 사용할 수도 있습니다. 몇 가지 예외가 있을 경우, 좋은 운빨이야. # 39 의 오류 처리 잭슨 그것은됐다 대량량 시간, it& 횡설수설 하는 것은 이해하기 어렵다고 오류 메시지.
내가 여기서 생성된 롬복 1.18.* 사용하여 동일한 문제가 경식도 마지막 발행물에 관한 문제가 있습니다.
나의 해결책은 @NoArgsConstructor (구성자를 매개변수 없이), 이후 추가 @Data dm_ownerdm_owner 기본적으로 @RequiredArgsConstructor (생성자를 매개변수).
롬복 문서용으로 https://projectlombok.org/features/all
이 문제를 해결할 수 있다고 말했다.
package example.counter;
import javax.validation.constraints.NotNull;
import lombok.Data;
@Data
@NoArgsConstructor
public class CounterRequest {
@NotNull
private final Integer int1;
@NotNull
private final Integer int2;
}
그러나 이 문제로 인해 this used to work for me, 업그레이드하기를 라이브러리보다는 나타날 수 있습니다. 문제가 있으면 클래스용 다음과 같습니다.
package example.counter;
import javax.validation.constraints.NotNull;
import lombok.Data;
@Data
public class CounterRequest {
@NotNull
private final Integer int1;
@NotNull
private final Integer int2;
}
롬복 섬 사용하여.
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.0</version>
</dependency>
대신 사용합니다.
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
문제가 해결되었습니다. 왜 확실하지 않지만, 향후 이 문서를 싶었다.