json 找不到可接受的表示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46990169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Could not find acceptable representation
提问by Mayur
I'm new to Spring Boot and I might be doing some silly mistake so Appologies in advance for such question. I'm trying to write POST API which accept following JSON :
我是 Spring Boot 的新手,我可能会犯一些愚蠢的错误,因此请提前回答此类问题。我正在尝试编写接受以下 JSON 的 POST API:
{
"id" : null,
"a": 1.3,
"b": "somestring",
"mapJson" :
{
"monday" : "10:00-12:00/n14:00-18:00",
"tuesday" : "10:00-12:00/n14:00-18:00",
"wednesday" : "10:00-12:00/n14:00-18:00",
"thursday" : "10:00-12:00/n14:00-18:00",
"friday" : "10:00-12:00/n14:00-18:00",
"saturday" : "10:00-12:00/n14:00-18:00",
"sunday" : "10:00-12:00/n14:00-18:00"
},
"list" : ["cc","paytm","debit"]
}
Consider following DTO class , AbcDTO:
考虑以下 DTO 类,AbcDTO:
package com.abb.dto;
import java.util.List;
import com.abb.entities.OpeningHrs;
import lombok.Data;
@SuppressWarnings("unused")
@Data
public class AbcDTO {
private Long id;
private Double a;
private String b;
private MapJson mapJson;
private List<String> list;
}
OpeningHrsis Class for mapping Json Map structure,
OpeningHrs是映射 Json Map 结构的类,
package com.abb.entities;
import lombok.Data;
@SuppressWarnings("unused")
@Data
public class MapJson {
private String monday;
private String tuesday;
private String wednesday;
private String thursday;
private String friday;
private String saturday;
private String sunday;
}
AbcControllerwhich have Post API :
具有 Post API 的AbcController:
package com.abb.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.Hymanson.databind.ObjectMapper;
import com.abb.dto.AbcDTO;
@RestController
@EnableAutoConfiguration
@RequestMapping("/abc")
@GetMapping(value="/{id}",produces={MediaType.APPLICATION_JSON_VALUE})
public class HotelController {
@RequestMapping(value = "/xyz", method = RequestMethod.POST)
public @ResponseBody AbcDTO registerHotel(@RequestBody AbcDTO aaa) {
System.out.println(aaa.toString());
return aaa;
// I'm not able to map JSON into this Object
}
}
Please find following Responce I'm getting is :
请找到以下我得到的回应是:
{
"timestamp": 1509193409948,
"status": 406,
"error": "Not Acceptable",
"exception": "org.springframework.web.HttpMediaTypeNotAcceptableException",
"message": "Could not find acceptable representation",
"path": "/abc/xyz"
}
采纳答案by Praveen Premaratne
The POSTrequest doesn't work because Spring doesn't know what kind of data it's expecting. So you will need to tell spring that you're expecting APPLICATION_JSON_VALUEso it knows how to process. consumes=will, as you probably guessed, tell Spring what the incoming POSTbody context type.
该POST请求不起作用,因为春天不知道什么样的数据,它的预期。因此,您需要告诉 spring 您正在等待,APPLICATION_JSON_VALUE以便它知道如何处理。consumes=正如您可能猜到的那样,将告诉 Spring 传入的POST正文上下文类型。
@RequestMapping(value = "xyz", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody AbcDTO registerHotel(@RequestBody AbcDTO aaa) {
System.out.println(aaa.toString());
return aaa;
// I'm not able to map JSON into this Object
}
With PostMapping
使用后映射
@PostMapping(value = "xyz", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody AbcDTO registerHotel(@RequestBody AbcDTO aaa) {
System.out.println(aaa.toString());
return aaa;
// I'm not able to map JSON into this Object
}
As you can see I have also added something else called, produces=this will instruct Spring how to format the response body of that request. So frontend receives JSONformatted body, not just random text.
正如您所看到的,我还添加了一些其他名称,produces=这将指示 Spring 如何格式化该请求的响应主体。所以前端接收JSON格式化的正文,而不仅仅是随机文本。
回答by 200OK
In my case the problem was that I didn't specify public getters in response class (analog of your AbcDTO class). So there were nothing to get to serialize and return to client.
就我而言,问题是我没有在响应类(类似于您的 AbcDTO 类)中指定公共 getter。所以没有什么可以序列化并返回给客户端。

