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

Pet*_*ith 5 java spring spring-mvc 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]
Run Code Online (Sandbox Code Playgroud)

我想知道如何使用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;
}
Run Code Online (Sandbox Code Playgroud)

酒吧:

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;
}
Run Code Online (Sandbox Code Playgroud)

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

我可以看到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"
}
Run Code Online (Sandbox Code Playgroud)

哪个成功.我如何使用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);
}
Run Code Online (Sandbox Code Playgroud)

所有三个例子都因不同原因而失败.

pau*_*aul 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);
Run Code Online (Sandbox Code Playgroud)

如果您向我们展示到目前为止您在 RestTemplate 上编程了什么,这将会很有用