私はSpring Cloudなどで少し混乱させるためにクラウドアプリケーションを開発してきました。今私は、RestTemplate APIを使用して、SpringのデータレストバックエンドにPOSTまたはPUTリクエストを送信しようとしているが、エラーで終了する:HttpMessageNotReadableException:START_OBJECTトークン、HttpMessageNotReadableExceptionのjava.lang.Stringのインスタンスを逆シリアル化できない:ドキュメントを読むことができませんでした:application/xml; contentsetのコンテンツタイプがrequestからのSTART_ARRAYトークンからjava.lang.Stringのインスタンスを逆シリアル化できません; charset = UTF-8!、エラー400 null ...あなたの名前。リサーチした後、私はHAL JSONをRestTemplate(レベル3のJSONハイパーメディア、正しくリコールすれば)で消費するのはかなり難しいことを発見しましたが、それが可能かどうかを知りたいのです。POSTとRestTemplateを介してPUTをSpringデータRestに送信Api
SpringデータレストバックエンドにPOSTとPUTを送信するRestTemplateの動作可能な(可能であれば詳細な)例をお見せしたいと思います。
編集:私はpostForEntity、postForLocation、交換を試してみましたが、それはちょうど異なる種類のエラーで終了しました。それらは私が試したいくつかの抜粋です(それ以上はありますが、私はそれらを処分するだけです)。
マイエンティティ:
@Entity
public class Account implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@NotNull
@NotEmpty
private String username;
@NotNull
@NotEmpty
private String authorities;
@NotNull
@NotEmpty
private String password;
//Constructor, getter and setter
一部restTemplateがattemps:
public Account create(Account account) {
//Doesnt work :S
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add("name", account.getName());
map.add("username", account.getUsername());
map.add("password", account.getPassword());
map.add("authorities", account.getAuthorities());
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
final HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<MultiValueMap<String, String>>(map,
headers);
return restTemplate.exchange(serviceUrl + "/accounts", HttpMethod.POST, entity, Account.class).getBody();
}
//Also tried with a AccountResource which extends from ResourceSupport and doesn't work either. This one gives me a error saying it cannot deserialize Account["name"].
はまた、このようにしようとしたヘッダは、アプリケーション/ xmlのことについてエラーを得た:RestTemplate POSTing entity with associations to Spring Data REST server
他のものはただ繰り返しそれらのエラーの1つ。
あなたのコードを共有してください。私たちは間違いなく助けてくれるでしょう – Coder
投稿すると、サーバーはボディを返すのですか? – zeroflagL
RestTemplateが壊れているため、サーバーは何も返しません。それは、コントローラ上での不正なリクエストエラーと、Spring Data Restバックエンドで答えたコメントに誤りがあります。 –