Java 错误:org.springframework.web.HttpMediaTypeNotSupportedException:内容类型';文本/纯文本;字符集=UTF-8';不支持

Java 错误:org.springframework.web.HttpMediaTypeNotSupportedException:内容类型';文本/纯文本;字符集=UTF-8';不支持,java,spring,spring-mvc,Java,Spring,Spring Mvc,我是春季数据的新手。 我不断收到错误:org.springframework.web.HttpMediaTypeNotSupportedException:Content-type'text/plain;字符集=UTF-8'不受支持 我试图将@RequestMapping注释中的消费更改为文本/普通,但不幸的是,它没有起到任何作用* 有什么想法吗 谢谢 我的代码如下所示: package com.budget.processing.application; import com.budget

我是春季数据的新手。 我不断收到错误:
org.springframework.web.HttpMediaTypeNotSupportedException:Content-type'text/plain;字符集=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;

}
请求如下所示: 我正在使用简单的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
         }
 ]
}

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

@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
}
@RequestMapping(method=RequestMethod.PUT,consumes=MediaType.APPLICATION\u JSON\u值)
@应答器
公共集合updateConsumerBudget(@RequestBody SomeDto SomeDto)引发GeneralException,ParseException{
//随便
}
给某人上课{
私人清单预算编制人;
//吸气剂二传手
}
该解决方案假定您正在创建的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。i、 e.建造师应该是

public codeVerification(@JsonProperty("code") String code, @JsonProperty("maskedMobileNumber") String maskedMobileNumber) {
    this.code = code;
    this.maskedMobileNumber = maskedMobileNumber;
}

我通过删除
@JsonManagedReference
并保留
@JsonBackReference
来解决问题,以避免无限循环。

请提供更多stacktrace-至少足以知道异常发生在哪个类和方法中…我在stacktrace中添加了一些。如果需要更多,请告诉我。向我们展示您在请求、标题和正文中发送的内容。
@RequestMapping(method=RequestMethod.POST,consumes=MediaType.APPLICATION\u JSON\u VALUE)
@SotiriosDelimanolis我已添加请求。希望很清楚,如果我们不能更改客户端的内容类型,它将无法工作。如何支持“文本/普通”内容类型;弹簧中的字符集=UTF-8'。这方面有什么帮助吗?@jeevs它在没有任何
注释的情况下对我有效。只需将Postman内容类型更改为“JSON”,而不是在“客户端”更改的“Text”@tusar。jeevs询问如何在spring端更改它,假设客户端使用类型“text/plain;charset=UTF-8”@jeevs我在Postman中提交请求时遇到了相同的响应错误。将内容类型更改为“JSON”,而不是“文本”。它起作用了。今天有同样的问题,很难弄清楚。基本上我只是通过调试器找到它。即使将日志级别设置为跟踪,也不会记录异常。在Spring的这个阶段,异常处理非常笨拙。反序列化错误永远不应该以415状态结束,而应该是400甚至500。。。
public codeVerification(@JsonProperty("code") String code, @JsonProperty("maskedMobileNumber") String maskedMobileNumber) {
    this.code = code;
    this.maskedMobileNumber = maskedMobileNumber;
}