gpt4 book ai didi

java - 无法使用 RestTemplate 和 Spring Data REST 发布具有关系的新实体

转载 作者:搜寻专家 更新时间:2023-10-31 20:27:52 28 4
gpt4 key购买 nike

我正在努力研究如何将 Spring 的 RestTemplate 与 hateoas 模块一起使用来创建新的相关实体。我试过获取一个 Foo 对象并将其分配给我要创建的 Bar 对象。当我发帖时,服务器给我一个 Http 400 Bad Request。当我尝试发布带有链接的 Resource 对象时,我得到这个异常(exception):

 Exception in thread "main" org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [org.springframework.hateoas.Resource]

我不知道如何使用 RestTemplate 针对 Spring Data REST 服务创建正确的 POST 请求。

背景:我有两个类 Foo 和 Bar。 Foo 与 Bar 具有 OneToMany 关系,因此 Bar 与 Foo 具有 ManyToOne 关系。

每个类的代码如下:

富:

package com.foo;

//Imports omitted for clarity

@Entity
@Getter
@Setter
@Table(name="Foo", schema="dbo")
public class Foo implements Identifiable<Integer> {

@Id
@Column(name="FOO_I")
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

@Column(name="Name")
private String name;

@Column(name="descript")
private String description;

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy="foo")
private Set<Bar> bars;
}

酒吧:

package com.foo;

@Entity
@Getter
@Setter
@Table(name="Bar", schema="dbo")
public class Bar implements Identifiable<Integer> {

@Id
@Column(name="BAR_I")
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

@Column(name="barname")
private String name;

@Column(name="bardescription")
private String description;

@Column(name="qty")
private int qty;

@ManyToOne
@JoinColumn(name="FOO_I", referencedColumnName="FOO_I", nullable=false)
private Foo foo;
}

我正在尝试发布到 http://nonexistantdomain.com.mx.uk.ch:8080/bars创建一个与 FOO_I 为 1 的 foo 相关的新栏。

我可以看到类似 http://nonexistantdomain.com.mx.uk.ch:8080/foos/1 的输出和 http://nonexistantdomain.com.mx.uk.ch:8080/foos/1/barshttp://nonexistantdomain.com.mx.uk.ch:8080/bars/5 .所以我知道这种关系正在发挥作用。

此外,我还可以使用 wget 和以下帖子主体创建一个新栏 http://nonexistantdomain.com.mx.uk.ch:8080/bars/ :

{
"name": "newWgetBar",
"description": "just another bar",
"qty": 2,
"foo" : "http://nonexistantdomain.com.mx.uk.ch:8080/foos/1"
}

哪个成功了。我如何使用 Spring 的 RestTemplate 来执行此操作,以便我可以从我的 Java 代码中完成我需要的东西?

编辑:

这是我试过的例子。

private RestTemplate acquireTemplate(boolean isHalJson) {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.registerModule(new Jackson2HalModule());
mapper.registerModule(new JodaModule());

MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setSupportedMediaTypes(MediaType.parseMediaTypes("application/hal+json"));
converter.setObjectMapper(mapper);

return new RestTemplate(Collections.<HttpMessageConverter<?>> singletonList(converter));
}

public void addABar(String name) {
Bar b = new Bar();
b.setName(name);
b.setDescription("An added bar.");
b.setQty(2);
RestTemplate template = acquireTemplate();
ResponseEntity<Foo> f = template.getForEntity("http://localhost:8080/foos/1", Foo.class);
Link l = new Link("foo","http://localhost:8080/foos/1");
Resource<Bar> r = new Resource<Bar>(b,l);
URI i = template.postForLocation("http://localhost:8080/bars", r);
}

public void addABarAttempt2(String name) {
Bar b = new Bar();
b.setName(name);
b.setDescription("An added bar.");
b.setQty(2);
RestTemplate template = acquireTemplate();
ResponseEntity<Foo> f = template.getForEntity("http://localhost:8080/foos/1", Foo.class);
b.setFoo(f.getBody());
URI i = template.postForLocation("http://localhost:8080/bars", b);
}

public void addABarAttempt3(String name) {
Bar b = new Bar();
b.setName(name);
b.setDescription("An added bar.");
b.setQty(2);
RestTemplate template = acquireTemplate();
template.put("http://localhost:8080/foos/1/bars",b);
}

所有三个示例都因不同的原因而失败。

最佳答案

如何使用这段代码:

        RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());

String uri = new String("url");

Bar b= new Bar();
bar.setName("newWgetBar");

rt.postForObject(uri, b, Bar.class);

如果您向我们展示到目前为止您在 RestTemplate 上编写了哪些程序,那将会很有用

关于java - 无法使用 RestTemplate 和 Spring Data REST 发布具有关系的新实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27414922/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com