我有一个grails rest接口,扩展了RestfulController。当我不重写任何方法,我的代码看起来像这样:
class UserController extends RestfulController<User> {
static responseFormats = ["json"]
UserController() {
super(User)
}
}
通过上面的代码,我可以通过resttemplate和curl发布没有问题
_restTemplate.exchange(url, httpMethod, httpEntity, clazz, id)
一旦我覆盖了一些其他控制器,就像我的resttemplate.exchange下面的代码不再有效。我没有看到用户对象被传递到控制器(通过调试检查)。使用curl post我看到一个对象被传递给控制器。我可以通过curl和restTemplate.echange
成功使用GETclass UserController extends RestfulController<User> {
static responseFormats = ["json"]
UserController() {
super(User)
}
@Transactional
def save(User user) {
user.save(flush: true)
render(model: user, status: 201) as JSON
}
}
我的标题和模板是这样构建的:
private List<HttpMessageConverter<?>> getMessageConverters() {
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
converters.add(new MappingJackson2HttpMessageConverter());
return converters;
}
private HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.setContentType((MediaType.APPLICATION_JSON));
return headers;
}
private RestTemplate getTemplate() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.setMessageConverters(getMessageConverters());
restTemplate.setErrorHandler(new CustomResponseErrorHandler());
return restTemplate;
}
我从resttemplate.exchange发帖时收到的错误消息是:
An unexpected error occurredorg.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class receiptbucketserver.User] and content type [text/html;charset=UTF-8]
任何想法???
答案 0 :(得分:0)
@Override
Object save() {
def instance = createResource()
User user = instance
user.save(flush: true)
respond user, [status: CREATED]
}