Protobuffer
Controller
@RequestMapping(value={"/get/test"}, method = {RequestMethod.GET})
public void userSearch(HttpServletResponse response,
@RequestParam(value = "name", required = false) String name,
@RequestParam(value = "page",required = false) int page) throws IOException{
if(logger.isDebugEnabled()){
logger.debug("name is:" + name);
}
User user = User.newBuilder().setName(name).setPage(page).build();
response.setContentType("application/x-protobuf");
OutputStream outputStream = response.getOutputStream();
user.writeTo(outputStream);
outputStream.flush();
outputStream.close();
}
@RequestMapping(value={"/post/test"}, method = {RequestMethod.POST})
public HttpEntity<?> userUpdate(HttpServletRequest request, HttpServletResponse response) throws IOException{
InputStream inputStream = request.getInputStream();
User user = User.parseFrom(inputStream);
inputStream.close();
System.out.println(user);
return new ResponseEntity<String>("OK",HttpStatus.OK);
}
@RequestMapping(value={"/proto/get/test"}, method = {RequestMethod.GET})
@ResponseBody
public User userSearchConverter(HttpServletResponse response,
@RequestParam(value = "name", required = false) String name,
@RequestParam(value = "page",required = false) int page) {
if(logger.isDebugEnabled()){
logger.debug("name is:" + name);
}
User user = User.newBuilder().setName(name).setPage(page).build();
return user;
}
@RequestMapping(value={"/proto/post/test"}, method = {RequestMethod.POST})
public HttpEntity<?> protoPostTest(@RequestBody Book book ) {
if(logger.isDebugEnabled()){
logger.debug("id is:" + book.getId());
logger.debug("name is:" + book.getName());
logger.debug("author is:" + book.getAuthor());
}
return new ResponseEntity<String>(HttpStatus.OK);
}
test:
@Test
public void testProtoPostTestSearch() {
final String searchURL = baseURL + "proto/post/test";
Book book = Book.newBuilder().setId(33).setName("Secrets").setAuthor("wesley").build();
ResponseEntity<String> response = RestTemplateHelper.getRestTemplate().postForEntity(searchURL, book, String.class);
System.out.println(response);
}