我收到以下错误:
The HTTP header line [{: ] does not conform to RFC 7230 and has been ignored.
由于发出以下POST请求:
POST {{host-url}}//api/report/create
Content-Type: application/json
{
"city":"Bronx",
"state":"NY",
"latitude":15,
"longitude":21,
"description":"First POST insert",
"verificationCounter":0,
"events":
[
{
"eventType":"Wind",
"details":
{
"speed":12.0,
"direction":90.0
}
}
]
}
我不知道我的Spring应用程序中的rest控制器是否有问题:
@RestController
@CrossOrigin
@RequestMapping(value="/api/report", headers="Accept=application/json")
public class ReportController {
private final ReportRepository reportRepository;
private final ReportService reportService;
public ReportController(final ReportRepository reportRepository, final ReportService reportService) {
this.reportRepository = reportRepository;
this.reportService = reportService;
}
@PostMapping("/create")
public void create(@RequestBody ReportView reportView, BindingResult bindingResult) {
//assumes these are new reports and events
HashMap<Report, List<Event>> reportAndEvents = ReportTranslator.ViewToEntity(reportView);
for (Report r : reportAndEvents.keySet()) {
reportService.createReport(r, reportAndEvents.get(r));
}
}
}
我不知道“ [{:]”的来源。在Intellij中,也许有些奇怪的格式或读取?如果您需要其他文件,请告诉我。但是我对如何得到这个错误感到困惑。我检查了JSON结构的格式是否正确。也许与我的ReportView对象的设置有关?
任何建议都会有所帮助。谢谢。
我相信问题是由类级别属性headers="Accept=application/json"
和所使用的Tomcat版本引起的。对于最新版本的Tomcat,标头中的:
会引起问题。从控制器类中删除该属性应该可以正常工作。
此外,作为默认的Spring Boot,使用应用程序/ json格式进行请求和响应。但是,如果仍然需要提供媒体类型,请使用@RequestMapping的produces
和consumes
属性。
@RestController
@CrossOrigin
@RequestMapping(value="/api/report", produces="application/json", consumes=produces="application/json")
public class ReportController {
// code goes here
}
希望有帮助!