2014-12-11 34 views
5

我在苦苦思索如何使用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/1http://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); 
} 

這三個例子都因不同原因而失敗。

+0

讓我們來看看,如果我理解的問題,你不能創建一個新的,將是有益的通過具有期望的「Foo」設置的帖子的酒吧實例。如果你不能創建一個新的酒吧,爲什麼你能看到關係和酒吧對象?如果你不能創建一個新的酒吧,不應該有一個酒吧對象嗎? – hofan41 2014-12-23 08:31:19

+0

你是如何提出這個帖子的請求? Javascript? – yunandtidus 2014-12-23 08:38:24

+0

請提供完整的堆棧跟蹤+客戶端Java代碼(它試圖創建欄) – 2014-12-25 14:00:23

回答

2

怎麼樣使用這個代碼:

 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

相關問題