当前位置: 首页 > 知识库问答 >
问题:

错误:org.springframework.web.HttpMediaTypeNotSupport异常:不支持内容类型“文本/纯;charset=UTF-8”

商宝
2023-03-14

我是Spring数据的新手。我一直收到错误:org。springframework。网状物HttpMediaTypeNotSupportedException:内容类型为“text/plain”;charset=UTF-8'不受支持我试图将@RequestMapping注释中的消费更改为文本/普通,但不幸的是,它没有帮助*

有什么想法吗?

谢谢,

我的代码如下所示:

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)

请求看起来是这样的:我正在使用简单的Rest模板Google扩展。请求如下所示:

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
         }
 ]
}

共有3个答案

班景龙
2023-03-14

如果您通过Postman发出帖子请求,请确保您选择了这两个选项

2. Content-Type-应用程序/json

班泽语
2023-03-14

对我来说,我在一个实体中有一个@JsonManagedReferece,而在另一个被引用的实体中没有@JsonBackReference。这导致封送员抛出一个错误。

叶富
2023-03-14

根据评论中提到的内容,最简单的解决方案是:

@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请求实际上具有

内容类型:application/json而不是text/plain

 类似资料: