2013-05-09 2 views
1

JSON 문자열을 pojo 클래스로 비 정렬 화하려고합니다. 나는 기존의 URL에서 읽기 오전 : 나는 URL을 지금까지POJO에 JSON 비 정렬 및 삽입

@Component 
public class RouteBuilder extends SpringRouteBuilder { 

private Logger logger = LoggerFactory.getLogger(RouteBuilder.class); 
@Override 
public void configure() throws Exception { 

    logger.info("Configuring route"); 

    //Properties die hij niet vindt in de klasse negeren 
    ObjectMapper objectMapper = new ObjectMapper(); 
    objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); 


    DataFormat reportFormat = new JacksonDataFormat(objectMapper, HealthReport.class); 

        from("timer://foo?fixedRate=true&delay=0&period=2000&repeatCount=1") 
          .routeId("accumoloToJsonRoute") 
          .setHeader(Exchange.HTTP_METHOD, constant("GET")) 
          .to("https://builds.apache.org:443/job/Accumulo-1.5/api/json") 
          .convertBodyTo(String.class) 
          .unmarshal(reportFormat) //instance van Build 
          .log(LoggingLevel.DEBUG, "be.kdg.teamf", "Project: ${body}") 
          .to("hibernate:be.kdg.teamf.model.HealthReport"); 


} 

} 

정말 좋은에게 비 정렬 화하기 위해 아파치 낙타를 사용하고 https://builds.apache.org/job/Accumulo-1.5/api/json

. 하이버 네이트 어노테이션을 사용하여 'healthReport'노드 만 삽입하고 싶습니다.

@XmlRootElement(name = "healthReport") 
@JsonRootName(value = "healthReport") 
@Entity(name = "healthreport") 
public class HealthReport implements Serializable { 
@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
private int Id; 

@Column 
@JsonProperty("description") 
private String description; 

@Column 
@JsonProperty("iconUrl") 
private String iconUrl; 

@Column 
@JsonProperty("score") 
private int score; 

public HealthReport() { 
} 

public HealthReport(int score, String iconUrl, String description) { 
    this.score = score; 
    this.iconUrl = iconUrl; 
    this.description = description; 
} 

public String getDescription() { 
    return description; 
} 

public String getIconUrl() { 
    return iconUrl; 
} 

public int getId() { 
    return Id; 
} 

public int getScore() { 
    return score; 
} 

public void setDescription(String description) { 
    this.description = description; 
} 

public void setIconUrl(String iconUrl) { 
    this.iconUrl = iconUrl; 
} 

public void setId(int id) { 
    Id = id; 
} 

public void setScore(int score) { 
    this.score = score; 
} 
} 

여기가 문제입니다. 그것은 주석을 인식하지 못하는 만 null 값은

@XmlRootElement(name = "healthReport") 
@JsonRootName(value = "healthReport") 

아무도이 문제를 해결하는 방법을 알고 있나요

내 데이터베이스에 삽입?

감사

답변

2

내 경로

public class HealthReportProcessor implements Processor { 
@Autowired 
private ConfigurationService configurationService; 

@Override 
public void process(Exchange exchange) throws Exception { 
    ObjectMapper mapper = new ObjectMapper(); 
    JsonNode root = mapper.readTree(exchange.getIn().getBody().toString()); 
    ArrayNode report = (ArrayNode) root.get("healthReport"); 

    int configId = configurationService.findJenkinsConfigurationByName(root.get("displayName").asText()).getId(); 

    for (JsonNode node : report) { 
     JsonObject obj = new JsonObject(); 
     obj.addProperty("description", node.get("description").asText()); 
     obj.addProperty("iconUrl", node.get("iconUrl").asText()); 
     obj.addProperty("score", node.get("score").asInt()); 
     obj.addProperty("jenkinsConfig", configId); 

     exchange.getIn().setBody(obj.toString()); 
    } 
} 
} 

그것은 노력의 프로세서를 사용하여 고정하지만 난 더 나은 솔루션이있다 생각합니다. 당신은 더 나은 솔루션 알려 주시기 바랍니다있는 경우)이 시도 할 수

0

,

from("timer://foo?fixedRate=true&delay=0&period=2000&repeatCount=1") 
.routeId("accumoloToJsonRoute") 
.setHeader(Exchange.HTTP_METHOD,constant("GET")) 
.to("https://builds.apache.org:443/job/Accumulo-1.5/apijson") 
.unmarshal().json(JsonLibrary.Jackson, HealthReport.class) 

을하고 응답을 확인 PARAMS POJO와 필드를 일치합니다.

작동하는지 알려주세요.