Springmvc RestTemplate 使用 && json xml rest 配置

来源:互联网 发布:网站赌博可以改数据吗 编辑:程序博客网 时间:2024/06/18 17:52

JSON:http://howtodoinjava.com/spring/spring-restful/spring-rest-hello-world-json-example/

XML:http://howtodoinjava.com/spring/spring-restful/spring-rest-hello-world-xml-example/

HTTP GET Method Example

1) Get XML representation of employees collection in String format

REST API Code

@RequestMapping(value = "/employees", produces = MediaType.APPLICATION_XML_VALUE, method = RequestMethod.GET)
publicString getAllEmployeesXML(Model model)
{
    model.addAttribute("employees", getEmployeesCollection());
    return"xmlTemplate";
}

REST Client Code

privatestaticvoidgetEmployees()
{
    finalString uri = "http://localhost:8080/springrestexample/employees.xml";
     
    RestTemplate restTemplate = newRestTemplate();
    String result = restTemplate.getForObject(uri, String.class);
     
    System.out.println(result);
}

2) Get JSON representation of employees collection in String format

REST API Code

@RequestMapping(value = "/employees", produces = MediaType.APPLICATION_JSON_VALUE,  method = RequestMethod.GET)
publicString getAllEmployeesJSON(Model model)
{
    model.addAttribute("employees", getEmployeesCollection());
    return"jsonTemplate";
}

REST Client Code

privatestaticvoidgetEmployees()
{
    finalString uri = "http://localhost:8080/springrestexample/employees.json";
     
    RestTemplate restTemplate = newRestTemplate();
    String result = restTemplate.getForObject(uri, String.class);
     
    System.out.println(result);
}

3) Using custom HTTP Headers with RestTemplate

REST API Code

@RequestMapping(value = "/employees", produces = MediaType.APPLICATION_JSON_VALUE,  method = RequestMethod.GET)
publicString getAllEmployeesJSON(Model model)
{
    model.addAttribute("employees", getEmployeesCollection());
    return"jsonTemplate";
}

REST Client Code

privatestaticvoidgetEmployees()
{
    finalString uri = "http://localhost:8080/springrestexample/employees";
     
    RestTemplate restTemplate = newRestTemplate();
     
    HttpHeaders headers = newHttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    HttpEntity<String> entity = newHttpEntity<String>("parameters", headers);
     
    ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
     
    System.out.println(result);
}

4) Get data as mapped object

REST API Code

@RequestMapping(value = "/employees", produces = MediaType.APPLICATION_XML_VALUE, method = RequestMethod.GET)
publicString getAllEmployeesXML(Model model)
{
    model.addAttribute("employees", getEmployeesCollection());
    return"xmlTemplate";
}

REST Client Code

privatestaticvoidgetEmployees()
{
    finalString uri = "http://localhost:8080/springrestexample/employees";
    RestTemplate restTemplate = newRestTemplate();
     
    EmployeeListVO result = restTemplate.getForObject(uri, EmployeeListVO.class);
     
    System.out.println(result);
}

5) Passing parameters in URL

REST API Code

@RequestMapping(value = "/employees/{id}")
publicResponseEntity<EmployeeVO> getEmployeeById (@PathVariable("id")intid)
{
    if(id <= 3) {
        EmployeeVO employee = newEmployeeVO(1,"Lokesh","Gupta","howtodoinjava@gmail.com");
        returnnewResponseEntity<EmployeeVO>(employee, HttpStatus.OK);
    }
    returnnewResponseEntity(HttpStatus.NOT_FOUND);
}

REST Client Code

privatestaticvoidgetEmployeeById()
{
    finalString uri = "http://localhost:8080/springrestexample/employees/{id}";
     
    Map<String, String> params = newHashMap<String, String>();
    params.put("id","1");
     
    RestTemplate restTemplate = newRestTemplate();
    EmployeeVO result = restTemplate.getForObject(uri, EmployeeVO.class, params);
     
    System.out.println(result);
}

HTTP POST Method Example

REST API Code

@RequestMapping(value = "/employees", method = RequestMethod.POST)
publicResponseEntity<String> createEmployee(@RequestBodyEmployeeVO employee)
{
    System.out.println(employee);
    returnnewResponseEntity(HttpStatus.CREATED);
}

REST Client Code

privatestaticvoidcreateEmployee()
{
    finalString uri = "http://localhost:8080/springrestexample/employees";
 
    EmployeeVO newEmployee = newEmployeeVO(-1,"Adam","Gilly","test@email.com");
 
    RestTemplate restTemplate = newRestTemplate();
    EmployeeVO result = restTemplate.postForObject( uri, newEmployee, EmployeeVO.class);
 
    System.out.println(result);
}

HTTP PUT Method Example

REST API Code

@RequestMapping(value = "/employees/{id}", method = RequestMethod.PUT)
publicResponseEntity<EmployeeVO> updateEmployee(@PathVariable("id")intid,@RequestBodyEmployeeVO employee)
{
    System.out.println(id);
    System.out.println(employee);
    returnnewResponseEntity<EmployeeVO>(employee, HttpStatus.OK);
}

REST Client Code

privatestaticvoiddeleteEmployee()
{
    finalString uri = "http://localhost:8080/springrestexample/employees/{id}";
     
    Map<String, String> params = newHashMap<String, String>();
    params.put("id","2");
     
    EmployeeVO updatedEmployee = newEmployeeVO(2,"New Name","Gilly","test@email.com");
     
    RestTemplate restTemplate = newRestTemplate();
    restTemplate.put ( uri, updatedEmployee, params);
}

HTTP DELETE Method Example

REST API Code

@RequestMapping(value = "/employees/{id}", method = RequestMethod.DELETE)
publicResponseEntity<String> updateEmployee(@PathVariable("id")intid)
{
    System.out.println(id);
    returnnewResponseEntity(HttpStatus.OK);
}

REST Client Code

privatestaticvoiddeleteEmployee()
{
    finalString uri = "http://localhost:8080/springrestexample/employees/{id}";
     
    Map<String, String> params = newHashMap<String, String>();
    params.put("id","2");
     
    RestTemplate restTemplate = newRestTemplate();
    restTemplate.delete ( uri,  params );
}

Let me know if something needs more explanation.

阅读全文
0 0
原创粉丝点击