我是 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"]
}
考虑以下 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;
}
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;
}
AbcController 具有 Post API:
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.jackson.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
}
}
请查找我收到的以下回复:
{
"timestamp": 1509193409948,
"status": 406,
"error": "Not Acceptable",
"exception": "org.springframework.web.HttpMediaTypeNotAcceptableException",
"message": "Could not find acceptable representation",
"path": "/abc/xyz"
}
最佳答案
POST
请求不起作用,因为 Spring 不知道它需要什么类型的数据。因此,您需要告诉 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
}
使用 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
}
如您所见,我还添加了其他名称,products=
,这将指示 Spring 如何格式化该请求的响应正文。因此前端接收 JSON
格式的正文,而不仅仅是随机文本。
关于json - 找不到可接受的代表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46990169/