[英]Unable to send POST parameters with mix of MultiPartFile to Rest Service using spring RestTemplate
[英]Unable to post new entity with relationship using RestTemplate and Spring Data REST
我正在努力研究如何使用Spring的RestTemplate和hateoas模塊來創建新的相關實體。 我已經嘗試獲取Foo對象並將其分配給我正在嘗試創建的Bar對象。 當我發布服務器給我一個Http 400 Bad Request。 當我嘗試使用鏈接發布Resource對象時,我得到以下異常:
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/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);
}
所有三個例子都因不同原因而失敗。
那么使用這段代碼:
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上編程的內容會很有用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.