エンドポイントがファイル名で終わるとき、My Spring REST APIに問題があります(=>406
)。 /file/example.xml
は結果がXMLでなければならないと仮定しているので間違っています(JSONでなければなりません)。Spring REST - URLが.xmlで終わったときに応答タイプをJSONにする方法
Written [{timestamp=Thu Oct 20 11:11:14 CEST 2016, status=406, error=Not Acceptable, exception=org.springframework.web.HttpMediaTypeNotAcceptableException, message=Could not find acceptable representation, path=/file/foobar.xml}] as "application/json" using [org.springfr[email protected]7015ebef]
私は春の4機能は、ファイルの拡張子を検出し、応答タイプをoverwrittingについて何かを読んで、私は応答タイプを強制しようとしたが、私は解決策を見つけることができませんでした。ここで
は私のRESTコントローラです:
@CompileStatic
@RestController
class ImportFileController {
@RequestMapping(
value = "/file/{fileName:.+}",
method = RequestMethod.PUT,
produces = "application/json"
)
@ResponseBody
public ImportFile update(
@PathVariable("fileName") String fileName,
@RequestBody ImportFile importFile) {
// ..
return importFile
}
}
これは、要求/応答である:私が試した何
Request PUT /file/foobar.xml {"reader_name":"something"}
Response 406 {
"timestamp" : "2016-10-20 08:56:35",
"status" : 406,
"error" : "Not Acceptable",
"exception" : "org.springframework.web.HttpMediaTypeNotAcceptableException",
"message" : "Could not find acceptable representation",
"path" : "/file/foobar.xml"
}
:
{filenName:.+}
代わりの{fileName}
- 使い方を使います
produces
私の@RequestMapping
true
またはfalse
へspring.mvc.path-matching.suffix-pattern
を設定するカスタムAccept
ヘッダ(application/json
)- を送信するが、それが働いていた何ちょうど野生の推測
た:
- が
/file/{fileName:.+}
からエンドポイントを変更します〜/file/{fileName:.+}/update
これは解決策ではなく、解決策です。
重要:私はGroovyを使用しており、YML構成を使用しています。
適切「のContent-Type」ヘッダを設定されていることを確認しhttp://stackoverflow.com/a/20743941/1496158を見てください。この記事では、ハンドラマッピングを無効にする方法、または特定の拡張機能に対してのみハンドラマッピングを有効にする方法について説明します。 – dudel
私が間違っている場合は私を訂正してください。それはJava/XMLの設定だけです – MonkeyMonkey
しかし、確かにSpring Beanを設定する方法が必要でしょうか?これは、何を設定するかを指示するものです。 – dudel