2013-05-09 40 views
1

我想解組一個json字符串到pojo類。 我從一個現有的URL讀它: https://builds.apache.org/job/Accumulo-1.5/api/jsonJSON解組到POJO並插入

我使用Apache的駱駝解組的URL

@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"); 


} 

} 

到目前爲止好。我想只使用hibernate註釋插入'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; 
} 
} 

這是問題所在。它不承認註釋 只有空值插入我的數據庫

@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領域。

讓我知道它是否有效。