遵循dto class,
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;
}
opinghrs是用于映射JSON地图结构的类,
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;
}
具有邮政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.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"
}
请求不起作用,因为春季不知道它会期望什么样的数据。因此,您需要告诉Spring您期望
POST
APPLICATION_JSON_VALUE
consumes=
postmapping
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
}
这将指示Spring如何格式化该请求的响应主体。因此,前端接收@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
}
格式的身体,而不仅仅是随机文本。
在我的情况下,问题是我没有在响应类中指定公共getters(您的ABCDTO类模拟)。因此,没有什么可以序列化并返回客户的。 在我的案子中在pom
中对此有所帮助
produces=
我只是花了半天的时间来解决这个错误,最后发现春季的contentNogotiationConfigurer默认情况下,如果存在的话,则有利于JSON
伦波克(Lombok)的问题。尝试使用手动写入getter和setter方法替换@data或@getter和@setter,或正确配置了Lombok插件和依赖项。