首页 / JAVA / java – 错误:org.springframework.web.HttpMediaTypeNotSupportedException:不支持内容类型’text / plain; charset
java – 错误:org.springframework.web.HttpMediaTypeNotSupportedException:不支持内容类型’text / plain; charset
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 错误:org.springframework.web.HttpMediaTypeNotSupportedException:不支持内容类型’text / plain; charset,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5019字,纯文字阅读大概需要8分钟。
内容图文
![java – 错误:org.springframework.web.HttpMediaTypeNotSupportedException:不支持内容类型’text / plain; charset](/upload/InfoBanner/zyjiaocheng/705/01e8061dbf5b421cbe9181e9cd9589ee.jpg)
我是Spring Data的新手.
我一直收到错误:org.springframework.web.HttpMediaTypeNotSupportedException:不支持内容类型’text / plain; charset = UTF-8′
我试图将@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
内容总结
以上是互联网集市为您收集整理的java – 错误:org.springframework.web.HttpMediaTypeNotSupportedException:不支持内容类型’text / plain; charset全部内容,希望文章能够帮你解决java – 错误:org.springframework.web.HttpMediaTypeNotSupportedException:不支持内容类型’text / plain; charset所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。