第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Spring Boot中的ObjectMapper可以讀取字符串,但在解析標(biāo)頭時(shí)不起作用

Spring Boot中的ObjectMapper可以讀取字符串,但在解析標(biāo)頭時(shí)不起作用

神不在的星期二 2023-12-13 17:08:07
我試圖在 spring boot 中將枚舉值作為標(biāo)頭參數(shù)提供給我的其余端點(diǎn)@RestController。為此,我將杰克遜庫(kù)放入我的build.gradle文件中,因?yàn)樽詣?dòng)生成的枚舉使用了杰克遜注釋。我無(wú)法更改枚舉代碼(它是根據(jù) openapi 規(guī)范自動(dòng)生成的)。它看起來(lái)像這樣:public enum DocumentTypes {  APPLICATION_PDF("application/pdf"),  APPLICATION_RTF("application/rtf"),  APPLICATION_VND_OASIS_OPENDOCUMENT_TEXT("application/vnd.oasis.opendocument.text"),  APPLICATION_VND_OPENXMLFORMATS_OFFICEDOCUMENT_WORDPROCESSINGML_DOCUMENT("application/vnd.openxmlformats-officedocument.wordprocessingml.document"),  APPLICATION_VND_MS_WORD("application/vnd.ms-word"),  TEXT_HTML("text/html"),  TEXT_PLAIN("text/plain");  private String value;  DocumentTypes(String value) {    this.value = value;  }  @Override  @JsonValue  public String toString() {    return String.valueOf(value);  }  @JsonCreator  public static DocumentTypes fromValue(String text) {    for (DocumentTypes b : DocumentTypes.values()) {      if (String.valueOf(b.value).equals(text)) {        return b;      }    }    throw new IllegalArgumentException("Unexpected value '" + text + "'");  }}我用來(lái)測(cè)試的其余控制器如下所示:@RestController@RequestMapping("/test")public class TestController {    @Autowired    private ObjectMapper objectMapper;    @RequestMapping(path = "", method = RequestMethod.GET)    public void test(@RequestHeader(value = "Accept", required = false) DocumentTypes targetFormat) throws IOException {        DocumentTypes value = objectMapper.readValue("\"application/pdf\"", DocumentTypes.class);    }}如果我不提供 Accept 標(biāo)頭,而只是在代碼中中斷,我可以看到代碼的第一行工作正常,字符串a(chǎn)pplication/pdf被轉(zhuǎn)換為,value因此ObjectMapper使用該方法完成了它的工作@JsonCreator。
查看完整描述

1 回答

?
青春有我

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超8個(gè)贊

我認(rèn)為這正在按設(shè)計(jì)進(jìn)行。Spring 僅使用 JacksonObjectMapper來(lái)轉(zhuǎn)換消息體(使用注冊(cè)的HttpMessageConverter,特別是MappingJackson2HttpMessageConverter)。

這記錄在https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-typeconversion

如果參數(shù)聲明為 String 之外的其他內(nèi)容,則表示基于 String 的請(qǐng)求輸入(例如@RequestParam、@RequestHeader@PathVariable、@MatrixVariable@CookieValue)的某些帶注釋的控制器方法參數(shù)可能需要類(lèi)型轉(zhuǎn)換。

對(duì)于這種情況,會(huì)根據(jù)配置的轉(zhuǎn)換器自動(dòng)應(yīng)用類(lèi)型轉(zhuǎn)換

https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-requestbody

您可以使用@RequestBody注釋通過(guò)以下方式讀取請(qǐng)求正文并將其反序列化為對(duì)象:HttpMessageConverter


查看完整回答
反對(duì) 回復(fù) 2023-12-13
  • 1 回答
  • 0 關(guān)注
  • 201 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)