오류

2017-03-05 3 views
0

내가 JSON 스키마에 자바 인터페이스를 변환하기 위해 노력하고 있지만 오류

public interface Contributors { 

public List<Contributor> contributors(); 

public interface Contributor { 

    public String name(); 

    public String contributorUrl(); 

    public List<String> roles(); 

} 

} 

편집 2 NullPointerException이

을주고있다 : 를 나는 다음과 같은 출력을 얻고있다 :

{"type":"object","$schema":"http://json-schema.org/draft-04/schema#"} 

편집 3 :

다음은 SchemaGeneratorTest 코드입니다.

import com.fasterxml.jackson.core.JsonProcessingException; 
import com.fasterxml.jackson.databind.JsonNode; 
import com.fasterxml.jackson.databind.ObjectMapper; 
import com.fasterxml.jackson.databind.SerializationFeature; 
import com.github.reinert.jjschema.exception.TypeException; 
import com.github.reinert.jjschema.v1.JsonSchemaFactory; 
import com.github.reinert.jjschema.v1.JsonSchemaV4Factory; 

public class SchemaGeneratorTest { 
    private static ObjectMapper mapper = new ObjectMapper(); 
    public static final String JSON_$SCHEMA_DRAFT4_VALUE = "http://json-schema.org/draft-04/schema#"; 
    public static final String JSON_$SCHEMA_ELEMENT = "$schema"; 

    static { 
     // required for pretty printing 
     mapper.enable(SerializationFeature.INDENT_OUTPUT); 
    } 

    public static void main(String[] args) throws JsonProcessingException, TypeException { 

     JsonSchemaFactory schemaFactory = new JsonSchemaV4Factory(); 
     schemaFactory.setAutoPutDollarSchema(true); 
     JsonNode productSchema = schemaFactory.createSchema(Contributors.class); 
     System.out.println(productSchema); 
    } 
    } 
+0

SchemaGeneratorTest를 작성 했습니까? 그렇다면 해당 코드를 게시하십시오. – Nulano

+0

디버깅을 시도 했습니까? NullPointerException은 찾기 쉽습니다. –

+0

@ Nulano 업데이트. –

답변

1

사용중인 라이브러리는 스키마에서 보고서 필드와 getter 만 사용합니다. get로 시작하는 당신의 방법의 이름을 바꿉니다

public interface Contributors { 
    public List<Contributor> getContributors(); 
} 

public interface Contributor { 
    public String getName(); 
    public String getContributorUrl(); 
    public List<String> getRoles(); 
} 

편집 : 당신이 인터페이스를 수정할 수없는 경우 손상 "GET"문자열이 코드를 사용하고 어쨌든 모든 방법을 인쇄 할 수 있습니다. 실제 생산 코드에서 사용하지 마십시오. 많은 문제가 발생할 수 있습니다.

public class Test { 

    private static boolean isCorrupted() { 
     return "haha".startsWith("get"); 
    } 

    public static void main(String[] args) throws Exception { 
     String get = "get"; 
     Field value = String.class.getDeclaredField("value"); 
     value.setAccessible(true); 
     value.set(get, new char[]{}); 
     System.out.println(isCorrupted()); // prints true 
    } 

} 
+0

안녕하세요 @ nulano 불행히도 나는 인터페이스 계약을 만질 수 없다. 계약을 변경하지 않고 다른 방법으로? –

+1

@ArchitMaheshwari Unfortunetaly,'JsonSchemaGenerator'를 수정하지 않고는 이것을 할 수 없습니다. github에서 기능 요청 문제를 열어 볼 수 있습니다 ("모든 메소드를 getter로 간주하는 옵션 추가"). 이것은 getter가 항상'get' 또는'is' 접두어를 사용해야하는 좋은 예입니다. _ 실제로, 만약 당신이 정말로 원한다면, 아마도 그것을 리플렉션으로 수정할 수 있습니다,하지만이 SO 질문의 범위 밖에 있다고 생각합니다. _ – Nulano

+0

네, 아직 지원되지 않는 것 같습니다. 다음 라이브러리를 내 용도로 사용할 수 있도록 포인터를 리플렉션으로 변환 할 수 있도록 제공 할 수 있습니까? 도와 주셔서 감사합니다. –