Java Spring RestTemplate POST问题,而Postman工作

CapitanFindus

我对Spring还是很陌生,我正试图用它来保存Mandrill webhooks事件。创建所有必需的验证器后,我发现它正在使用application/x-www-form-urlencoded标头发送事件,这阻止了所有验证器正常工作。

之后,我决定将Mandrill webhooks的请求重新映射为带有其header的有效JSON对象Content-Type: application/json

在SO上进行搜索,我发现了从Spring向自身发送POST请求的方法很多,经过几次尝试,我开始使用RestTemplate,就像这样

Webhook控制器

@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public void remap(@RequestParam String mandrill_events) throws IOException {
    JsonNodeFactory factory = JsonNodeFactory.instance;
    ArrayNode eventsNode = factory.arrayNode();
    ObjectNode eventNode = factory.objectNode();
    ObjectMapper mapper = new ObjectMapper();
    JsonNode node = mapper.readTree(mandrill_events);
    if (node.isArray()) {
        for (int i = 0; i < node.size(); i++) {
            eventsNode.add(node.get(i));
        }
    }
    eventNode.set("mandrill_events", eventsNode);
    String webhookStoreURL = this.globalConfig.APIUrl + "/webhook/store";
    RestTemplate restTemplate = new RestTemplate();
    try {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        HttpEntity<String> httpEntity = new HttpEntity<>(eventNode.toString(), httpHeaders);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(webhookStoreURL, httpEntity, String.class);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@RequestMapping(path = "/store", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public void store(@RequestBody @Valid MandrillEventList eventList) throws StorageException, NotFoundException, SQLException {
    System.out.println("Saving " + eventList.getMandrillEvents().size() + " events");
    //this.eventService.storeEvents(eventList);
}

问题是,当我发送RestTemplate请求时,我得到的唯一东西是400 nullcatch块中的一个错误

因此,这是我到目前为止尝试过的:

  • @Valid从控制器方法中删除注释。没工作
  • 从全局验证器中删除所有验证器。没工作

看起来好像是在尝试验证请求正文,即使没有注释,所以我也尝试过这两个

  • 从调试中复制请求正文并在Postman上进行测试。有效(正确显示验证错误)

  • 使用Mandrill测试Webhook。作品(春季和邮递员)

我不知道去哪里看

CapitanFindus

我想出了一个解决方案。我没有将API请求重新发送给自己,而是这样做了

@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public void store(@RequestParam String mandrill_events) throws IOException {
    JsonNodeFactory factory = JsonNodeFactory.instance;
    ArrayNode eventsNode = factory.arrayNode();
    ObjectNode eventNode = factory.objectNode();
    ObjectMapper mapper = new ObjectMapper();
    JsonNode node = mapper.readTree(mandrill_events);
    if (node.isArray()) {
        for (int i = 0; i < node.size(); i++) {
            eventsNode.add(node.get(i));
        }
    }
    eventNode.set("mandrill_events", eventsNode);
    try {
        MandrillEventList eventList = mapper.readValue(eventNode.toString(), MandrillEventList.class);
        logger.log(Level.FINE, "Webhook has " + eventList.getMandrillEvents().size() + " events to save");
        this.eventService.storeEvents(eventList);
    } catch (Exception e) {
        e.printStackTrace();
        logger.log(Level.SEVERE, "Unable to setup mandrill event list " + e.getMessage());
        throw new IOException("Unable to setup mandrill event list");
    }
}

我按照Controller的预期重建JSON,然后MandrillEventList使用Jackson将其映射为原始对象ObjectMapper它给了我一些问题,例如当他不认识那些不在类描述中的属性时,所以我添加了如下@JsonIgnoreProperty注释:

@JsonIgnoreProperties(ignoreUnknown = true)
public class MandrillEvent {
 [...]
}

现在一切正常

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章