我有一个 Spring RestController,当我GET
从中获取数据时效果很好,但是当我尝试PUT
使用相同的数据时,我得到一个400 Bad Request
.
这是我的控制器的最简单版本,它仍然可以工作(我省略了GET
方法):
@RestController
@RequestMapping("/configuration/ledstrips/{name}/display")
public class DisplayController {
@ResponseBody
@RequestMapping(method = RequestMethod.PUT, produces = { "application/hal+json" })
public DisplayResource setDisplay(@PathVariable String name, @RequestBody DisplayResource display) {
return display;
}
}
这是DisplayResource
:
public class DisplayResource extends ResourceSupport {
private List<Color> pixels;
public List<Color> getPixels() {
return pixels;
}
public void setPixels(List<Color> pixels) {
this.pixels = pixels;
}
}
我几乎从另一个分支复制了这段代码,它在那里工作!
我想不通!
编辑
这是GET
-方法:
@ResponseBody
@RequestMapping(method = RequestMethod.GET, produces = { "application/hal+json" })
public DisplayResource getDisplay(@PathVariable String name) {
LEDStripDTO ledStripDTO;
try {
ledStripDTO = ledStripDTOService.getDTO(name);
} catch (IOException | NullPointerException exception) {
throw new LoadFailedException("Error loading LED strip:", exception);
}
Link self = linkTo(methodOn(DisplayController.class).getDisplay(name)).withSelfRel();
DisplayResource displayResource = new DisplayResource();
displayResource.add(self);
try {
displayResource.setPixels(ledStripService.getPixels(ledStripDTO));
} catch (IOException | TimeoutException | AlreadyConnectedException | NotConnectedException exception) {
throw new LoadFailedException("Error getting Information from LED strip:", exception);
}
return displayResource;
}
它产生的结果(对于长度为 1 的 LED 灯条):
{
"pixels": [
{
"red": 0,
"green": 16,
"blue": 0
}
],
"_links": {
"self": {
"href": "http://localhost:8080/configuration/ledstrips/devled/display"
}
}
}
当我发送这个时,无论是否有_links
段,我都会收到400
错误消息。