0
这是我的休息控制器(服务器):REST客户端与Spring RestTemplate和自定义对象的输入参数
@RestController
public class RemoteController {
@RequestMapping(value="/test", method=RequestMethod.GET)
public Return serverTest(HttpServletRequest req, SearchFilter search) throws Exception{
//...
return new OutputTest();
}
}
我想写相应的客户端与SearchFilter对象作为输入这个GET控制器。
public void clientTest(){
SearchFilter input=new SearchFilter();
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = input;// how to store SearchFilter input ??????
ResponseEntity<OutputTest> response=restTemplate.exchange("http://localhost:8080/test", HttpMethod.GET, entity, OutputTest.class);
OutputTest out=response.getBody();
}
如何发送单个对象到restTemplate?
使用此代码我从RestController得到了“org.springframework.http.converter.HttpMessageNotReadableException:所需的请求正文缺失” – Tobia
似乎GET必须是无身体的,它可能吗?将其转换为POST方法,它的工作原理!无论如何,我必须使用过滤器对象来处理这个读取请求,我应该怎么做才能遵循休息规定? – Tobia
您应该告诉spring将请求参数绑定到'SearchFilter'中。最简单的解决方案是使用'ModelAttribute'注释。有一刻,我以为你正在发送POST请求 –