質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

HTTP

HTTP(Hypertext Transfer Protocol)とはweb上でHTML等のコンテンツを交換するために使われるアプリケーション層の通信プロトコルです。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

Q&A

1回答

3908閲覧

SpringBootの415エラーを解消したい

fishStory

総合スコア10

XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

HTTP

HTTP(Hypertext Transfer Protocol)とはweb上でHTML等のコンテンツを交換するために使われるアプリケーション層の通信プロトコルです。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

0グッド

0クリップ

投稿2020/03/10 10:46

編集2020/03/10 12:37

指定したURL先のサーバにxmlを送信したいです。
どこに問題があるか、ご教授いただけるでしょうか。

#該当のソースコード
呼び出し側

java

1status = service.distribute(xml,URL);

HTTP送信ソース

java

1 2@Service 3public class HttpPost { 4 5 private final RestOperations rest; 6 7 public HttpPost(RestTemplateBuilder builder) { 8 this.rest = builder.basicAuthentication("admin","admin").build(); 9 10 } 11 12/* 13xml = <id>100</id> 14url = http://hostname:8085/order/test 15*/ 16 public HttpStatus distribute(String xml,String url) throws Exception { 17 18 ResponseEntity<String> entity; 19 if (url != null) { 20 entity = rest.postForEntity(url, xml, String.class); 21 } 22 23 return status; 24 } 25} 26

上記のプログラムで実施したいのは、以下のcurlコマンドと同じことです。
ちなみに、下記のcurlコマンドでは、サーバへの送信は201が返ってきており、成功します。

curl

1curl -v -X POST -T (xmlファイルの絶対パス) -u admin:admin http://hostname:8085/order/test

#実行結果/エラー内容

2020-03-10 21:35:44.478 [DEBUG] [scheduling-1Accept=[text/plain, application/json, application/*+json, /]

2020-03-10 21:35:44.478 [DEBUG] [scheduling-1Writing [<id>100</id>
] with org.springframework.http.converter.StringHttpMessageConverter
2020-03-10 21:35:45.014 [ERROR] [scheduling-1doSecondaryResponse:684] org.springframework.web.client.HttpClientErrorException$UnsupportedMediaType: 415 Unsupported Media Type: [<errors xmlns="urn:ietf:params:xml:ns:yang:ietf-restconf">
<error>
<error-message>Unsupported media type: text/plain ; Should be one of: application/yang-data+xml, application/yang-data+json.</error-message>
<error-tag>malformed-message</error-tag>
<error-type>application</error-type>
</error>
</errors>
]

#環境
java8
tomcat9
Springbootフレームワーク

#ほかにやってみたこと
xmlがplane textだと判断されているのかと思ったため、xmlで送ってみようとして以下の処理を入れてみましたが、同じようなエラーが出ます。

java

1 public HttpStatus distribute(String xml,String url) throws Exception { 2 //String型をDocument型にパースする 3 InputSource inputSource = new InputSource(new StringReader(xml)); 4 5 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 6 DocumentBuilder builder; 7 8 builder = factory.newDocumentBuilder(); 9 Document document; 10 document = builder.parse(inputSource); 11 12 entity = rest.postForEntity(url, document, String.class); 13 }

2020-03-10 19:29:10.154 [DEBUG] [scheduling-1Accept=[text/plain, application/json, application/*+json, /]

2020-03-10 19:29:10.154 [DEBUG] [scheduling-1Writing [[#document: null]] with org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
2020-03-10 19:29:10.492 [ERROR] [scheduling-1doSecondaryResponse:658] org.springframework.web.client.HttpClientErrorException$UnsupportedMediaType: 415 Unsupported Media Type: [<errors xmlns="urn:ietf:params:xml:ns:yang:ietf-restconf">
<error>
<error-message>Unsupported media type: application/json ; Should be one of: application/yang-data+xml, application/yang-data+json.</error-message>
<error-tag>malformed-message</error-tag>
<error-type>application</error-type>
</error>
</errors>
]

他にも、明示的にxmlになるようにしてみましたが、xmlではなくyang-data+xmlで指定するようにとのエラーが出ますが
MediaTypeではyang-data+xmlが指定できません。

java

1 public HttpStatus distribute(String xml,String url) throws Exception { 2 3 xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+xml; 4 HttpHeaders headers = new HttpHeaders(); 5 headers.setContentType(MediaType.APPLICATION_XML); 6 HttpEntity<String> request = new HttpEntity<String>(xml, headers); 7 8 final ResponseEntity<String> response = rest.postForEntity(url, request, String.class); 9}

2020-03-10 21:23:11.488 [DEBUG] [scheduling-1Accept=[text/plain, application/json, application/*+json, /]

2020-03-10 21:23:11.488 [DEBUG] [scheduling-1Writing [<?xml version="1.0" encoding="utf-8"?><id>100</id>
] as "application/xml"
2020-03-10 21:23:11.945 [ERROR] [scheduling-1doSecondaryResponse:684] org.springframework.web.client.HttpClientErrorException$UnsupportedMediaType: 415 Unsupported Media Type: [<errors xmlns="urn:ietf:params:xml:ns:yang:ietf-restconf">
<error>
<error-message>Unsupported media type: application/xml ; Should be one of: application/yang-data+xml, application/yang-data+json.</error-message>
<error-tag>malformed-message</error-tag>
<error-type>application</error-type>
</error>
</errors>
]

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

MediaType

public MediaType(String type)

なので

new MediaType("application/yang-data+xml")

のように定義外の MediaType が作成できます、ドキュメントくらいは読みましょう

投稿2020/03/10 12:43

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

fishStory

2020/03/10 14:06 編集

回答ありがとうございます。 ご指摘の方法でMediaTypeを設定したところ、それぞれ以下のエラーが出ます。 application/yang-data+xmlの指定方法をGoogleで検索しても、左記のままのものしか出てきません。 正しい指定方法をご存知でしょうか。 1.new MediaType("application/yang-data+xml”); →java.lang.IllegalArgumentException: Invalid token character '/' in token "application/yang-data+xml" 2.new MediaType("yang-data+xml”); →java.lang.IllegalArgumentException: Content-Type cannot contain wildcard subtype '*' 3.new MediaType("application_yang-data+xml”); →java.lang.IllegalArgumentException: Content-Type cannot contain wildcard subtype '*' 4./をエンコード →2と同様の結果
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問