使用 JSON 的 Spring MVC RestTemplate POST

发布于 2024-12-04 02:35:03 字数 2060 浏览 1 评论 0原文

我有一个 Spring MVC Rest 控制器,它只是添加一个客户记录。我的 Rest 服务传递 JSON,并且当我从 Web 客户端或像 Soap-UI 这样的测试台调用它时工作正常。但是,当我尝试使用 RestTemplate 进行 POST 时,出现 JacksonMappingException:无法构造 CustomerObject 实例,问题:找不到合适的创建者方法。

这显然与我创建 JSON 请求的方式有关,因此我正在做的就是添加一些内容:

我的测试帖子使用 RestTemplate

 @Test
 public void postCustomerIntegrationTest() throws Exception{

    String JSONInput = ("{" +
          "  \"firstName\": \"Anouska\"," +
          "  \"lastName\": \"Williams\"," +
          "  \"email\": \"[email protected]\"," +
          "  \"cardBin1\": 123456768," +
          "  \"cardBin2\": 123456789," +
          "  \"language\": \"Spanish\"," +
          "  \"country\": \"Mexico\"," +
          "  \"product\": \"shit\"," +
          "  \"telephoneNumber\": 447869995262," +
          "  \"termsAndConditions\": \"true\"" +
          "}");

  HttpHeaders headers = new HttpHeaders();
  headers.setContentType(MediaType.APPLICATION_JSON);

  HttpEntity request= new HttpEntity(JSONInput, headers);

Registration output = restTemplate.postForObject(BASE_URL, request, CustomerObject.class);

assertNotNull("no person",output);
assertNotNull(output.getId());
assertEquals("Bob", output.getFirstName());

}

我的 RestTemplate 通过 Spring 进行连接,如下所示:

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
    <list>
        <bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
            <property name="supportedMediaTypes" value="application/json" />
        </bean>
    </list>
</property>
</bean>

我的测试 JSON这里使用的与我在 Soap-UI 中使用的测试 JSON 相同。调试完代码后,它看起来像是将我的 JSON 视为字符串,而 Jackson 正在尝试转换整个内容,因为它找不到关联的字段(当然它找不到)。

我想我需要知道如何将 JSON 传递到 RestTemplate,以便以正确的 JSON 格式获取请求。我花了一上午的时间试图找到一个例子,但在任何地方都找不到。

非常感谢您的帮助。

I have a Spring MVC Rest Controller that simply adds a customer record. My Rest Service passes around JSON and works fine when I call it from a web client or a test bed like Soap-UI. However, when I try to POST using RestTemplate I get a JacksonMappingException: Can not construct instance of CustomerObject, problem: no suitable creator method found.

It's clearly something to do with the way I am creating the JSON request so to add some meat to the bones here is what I'm doing:

My Test posts using RestTemplate

 @Test
 public void postCustomerIntegrationTest() throws Exception{

    String JSONInput = ("{" +
          "  \"firstName\": \"Anouska\"," +
          "  \"lastName\": \"Williams\"," +
          "  \"email\": \"[email protected]\"," +
          "  \"cardBin1\": 123456768," +
          "  \"cardBin2\": 123456789," +
          "  \"language\": \"Spanish\"," +
          "  \"country\": \"Mexico\"," +
          "  \"product\": \"shit\"," +
          "  \"telephoneNumber\": 447869995262," +
          "  \"termsAndConditions\": \"true\"" +
          "}");

  HttpHeaders headers = new HttpHeaders();
  headers.setContentType(MediaType.APPLICATION_JSON);

  HttpEntity request= new HttpEntity(JSONInput, headers);

Registration output = restTemplate.postForObject(BASE_URL, request, CustomerObject.class);

assertNotNull("no person",output);
assertNotNull(output.getId());
assertEquals("Bob", output.getFirstName());

}

My RestTemplate is wired through Spring as follows:

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
    <list>
        <bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
            <property name="supportedMediaTypes" value="application/json" />
        </bean>
    </list>
</property>
</bean>

The test JSON I'm using here is the same as the test JSON I use in Soap-UI. Having debuged the code it looks like it is treating my JSON as a String and Jackson is trying to convert the entire thing because it can't find an assiciated field (which of course it can't).

I guess I need to know how to pass JSON to my RestTemplate in such a way that the request will be picked up in the correct JSON format. I've spent all morning trying to find an example and can't find one anywhere.

Many Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

﹏雨一样淡蓝的深情 2024-12-11 02:35:03

至少有一个问题,最后一个参数应该是响应的类别

Registration output = restTemplate.postForObject(BASE_URL, request, Registration.class);

There is at least one problem, last parameter should be class of response

Registration output = restTemplate.postForObject(BASE_URL, request, Registration.class);
做个少女永远怀春 2024-12-11 02:35:03

这是我发布 josn 的方式:

CustomerObject object = template.postForObject(
            BASE_URL, request, CustomerObject.class);

CustomerObject 类必须有字段:firstName、lastName...

This is my way to post josn:

CustomerObject object = template.postForObject(
            BASE_URL, request, CustomerObject.class);

And CustomerObject class must have fields: firstName, lastName...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文