我有一个我正在处理的Groovy应用程序,当它从spring-boot 1.3.0.RELEASE升级到1.4.0.RELEASE时有一些奇怪的行为。控制器总是在任何错误上返回406,我不确定它希望返回什么类型的内容。代码如下:
SomeController.groovy:
@RestController
@RequestMapping('/some/mapping')
class SomeController extends AbstractController {
@Autowired
private SomeService someService
@RequestMapping(path = '/abc/{some_param}/some_action', method = RequestMethod.PUT, consumes = MediaType.TEXT_PLAIN_VALUE)
@ResponseStatus(HttpStatus.NO_CONTENT)
@PreAuthorize('isAuthenticated() && (principal.username == #username || principal.admin)')
void setValue(@PathVariable String some_param, @RequestBody String body_content) throws ValidationException, NotFoundException {
handleViolations(validate(AnObject, [some_param: some_param, body: body_content]))
try {
someService.setValue(some_param, body_content)
} catch(AlreadyExistsException e) {
throw new ValidationException([body: 'IN_USE'])
}
}
}
SomeControllerSpec.groovy<测试...
class AccountControllerSpec extends AbstractControllerSpec {
static final BASE_URL = 'http://localhost:8080/api/'
def client = new CustomRESTClient(BASE_URL)
// This test fails
def 'testing api'() {
//Expected 400 bad request but receiving a 406 not acceptable
client.put(
path: "/api/abc/fake_param/some_action",
// The body doesn't conform to the expectations of the API
body: 'blah',
contentType: MediaType.TEXT_PLAIN_VALUE
).status == HttpStatus.SC_BAD_REQUEST
// Exception thrown:
// INFO 22125 --- [tp1838490665-22] c.c.w.c.RestEndpointsConfiguration : org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
}
}
日志中的例外:
INFO 22125 --- [tp1838490665-22] c.c.w.c.RestEndpointsConfiguration : org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
我尝试了很多方法,包括设置预期的标题类型:
client.setHeaders(accept: MediaType.TEXT_PLAIN_VALUE)
我一直在尝试各种其他事情,但无济于事。例外情况仍然存在。
注意:端点上的操作按预期完成。