[英]org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/xml;charset=UTF-8' not supported
[英]Error: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'text/plain;charset=UTF-8' not supported
我是 Spring Data 的新手。 我不斷收到錯誤消息: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'text/plain;charset=UTF-8' not supported
我試圖將@RequestMapping
注釋中的消耗更改為text/plain
但不幸的是它沒有幫助。*
有任何想法嗎?
謝謝,
我的代碼如下所示:
package com.budget.processing.application;
import com.budget.business.service.Budget;
import com.budget.business.service.BudgetItem;
import com.budget.business.service.BudgetService;
import com.budget.processing.dto.BudgetDTO;
import com.budget.processing.dto.BudgetPerConsumerDTO;
import com.utils.Constants;
import com.common.utils.config.exception.GeneralException;
import org.apache.log4j.Logger;
import org.joda.time.YearMonth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import javax.ws.rs.core.MediaType;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@Controller("budgetManager")
@RequestMapping(value = "budget", produces = Constants.RESPONSE_APP_JSON)
@Transactional(propagation = Propagation.REQUIRED)
public class BudgetManager {
private static final Logger logger = Logger.getLogger(BudgetManager.class);
@Autowired
private BudgetService budgetService;
@RequestMapping(method = RequestMethod.GET)
public
@ResponseBody
Collection<BudgetDTO> getBudgetMonthlyAllConsumers() throws GeneralException {
List<Budget> budgetList = budgetService.getBudgetForAllConsumers();
List<BudgetDTO> bugetDtos = new ArrayList<>();
for (Budget budget : budgetList) {
BudgetDTO budgetDTO = generateBudgetDto(budget);
bugetDtos.add(budgetDTO);
}
return bugetDtos;
}
@RequestMapping(method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON)
public
@ResponseBody
Collection<BudgetDTO> updateConsumerBudget(@RequestParam(value = "budgetPerDate", required = false)
ArrayList<BudgetPerConsumerDTO> budgetPerDate) throws GeneralException, ParseException {
List<BudgetItem> budgetItemList = new ArrayList<>();
List<Budget> budgets = new ArrayList<>();
if (budgetPerDate != null) {
for (BudgetPerConsumerDTO budgetPerConsumerDTO : budgetPerDate) {
budgetItemList.add(budgetService.createBudgetItemForConsumer(budgetPerConsumerDTO.getId(), new YearMonth(budgetPerConsumerDTO.getDate()), budgetPerConsumerDTO.getBudget()));
}
}
budgets = budgetService.getBudgetForAllConsumers();
List<BudgetDTO> budgetDTOList = new ArrayList<>();
for (Budget budget : budgets) {
BudgetDTO budgetDto = generateBudgetDto(budget);
budgetDTOList.add(budgetDto);
}
return budgetDTOList;
}
}
這是我得到的例外:
ERROR 2014-07-26 18:05:10.737 (GlobalExceptionHandler.eITFMSException: 86) Error executing Web Service org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'text/plain;charset=UTF-8' not supported
at org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:215)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:289)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:229)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:56)
at org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:298)
at org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1091)
該請求如下所示:我正在使用 Simple Rest Template Google Extension。 請求如下所示:
localhost:8080/rest
1 requests ❘ 140 B transferred
HeadersPreviewResponseCookiesTiming
Remote Address:localhost:8080
Request URL: localhost:8080/rest/budget
Request Method:PUT
Status Code:500 Internal Server Error
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,he;q=0.6
Connection:keep-alive
Content-Length:331
Content-Type:text/plain;charset=UTF-8
Cookie:JSESSIONID=AE87EEB7A73B9F9E81956231C1735814
Host:10.23.204.204:8080
Origin:chrome-extension://fhjcajmcbmldlhcimfajhfbgofnpcjmb
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36
Request Payloadview parsed
{
"budgetPerDate":
[
{
"id":942,
"date":[
2014,
1,
1
],
"budget": 100
},
{
"id":942,
"date":[
2014,
2,
1
],
"budget": 150
}
]
}
基於評論中提到的內容,最簡單的解決方案是:
@RequestMapping(method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Collection<BudgetDTO> updateConsumerBudget(@RequestBody SomeDto someDto) throws GeneralException, ParseException {
//whatever
}
class SomeDto {
private List<WhateverBudgerPerDateDTO> budgetPerDate;
//getters setters
}
該解決方案假定您正在創建的 HTTP 請求實際上具有
Content-Type:application/json
而不是text/plain
對我來說,結果證明我在一個實體中有一個@JsonManagedReferece
,而在另一個引用的實體中沒有@JsonBackReference
。 這導致編組器拋出錯誤。
好的 - 對我來說問題的根源在於序列化/反序列化。 正在發送和接收的對象如下,其中提交代碼並返回代碼和 maskedPhoneNumber。
@ApiObject(description = "What the object is for.")
@JsonIgnoreProperties(ignoreUnknown = true)
public class CodeVerification {
@ApiObjectField(description = "The code which is to be verified.")
@NotBlank(message = "mandatory")
private final String code;
@ApiObjectField(description = "The masked mobile phone number to which the code was verfied against.")
private final String maskedMobileNumber;
public codeVerification(@JsonProperty("code") String code, String maskedMobileNumber) {
this.code = code;
this.maskedMobileNumber = maskedMobileNumber;
}
public String getcode() {
return code;
}
public String getMaskedMobileNumber() {
return maskedMobileNumber;
}
}
問題是我沒有在構造函數中為 maskedMobileNumber 定義 JsonProperty。 即構造函數應該是
public codeVerification(@JsonProperty("code") String code, @JsonProperty("maskedMobileNumber") String maskedMobileNumber) {
this.code = code;
this.maskedMobileNumber = maskedMobileNumber;
}
我通過刪除@JsonManagedReference
並保留@JsonBackReference
來解決它以避免無限循環。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.