我在苦苦思索如何使用Spring's RestTemplate和hateoas模块来创建新的相关实体。 我试图抓取一个Foo对象并将其分配给我试图创建的Bar对象。当我发布服务器给我一个Http 400错误请求。当我试图张贴带有链接的资源对象,我得到 此异常:无法使用RestTemplate和Spring Data REST发布具有关系的新实体
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克服弹簧数据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有关。
我可以看到事物l的输出ike http://nonexistantdomain.com.mx.uk.ch:8080/foos/1和http://nonexistantdomain.com.mx.uk.ch:8080/foos/1/bars和http://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);
}
这三个例子都因不同原因而失败。
让我们来看看,如果我理解的问题,你不能创建一个新的,将是有益的通过具有期望的“Foo”设置的帖子的酒吧实例。如果你不能创建一个新的酒吧,为什么你能看到关系和酒吧对象?如果你不能创建一个新的酒吧,不应该有一个酒吧对象吗? – hofan41 2014-12-23 08:31:19
你是如何提出这个帖子的请求? Javascript? – yunandtidus 2014-12-23 08:38:24
请提供完整的堆栈跟踪+客户端Java代码(它试图创建栏) – 2014-12-25 14:00:23