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)
public
String getAllEmployeesXML(Model model)
{
model.addAttribute(
"employees"
, getEmployeesCollection());
return
"xmlTemplate"
;
}
REST Client Code
private
static
void
getEmployees()
{
final
String uri =
"http://localhost:8080/springrestexample/employees.xml"
;
RestTemplate restTemplate =
new
RestTemplate();
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)
public
String getAllEmployeesJSON(Model model)
{
model.addAttribute(
"employees"
, getEmployeesCollection());
return
"jsonTemplate"
;
}
REST Client Code
private
static
void
getEmployees()
{
final
String uri =
"http://localhost:8080/springrestexample/employees.json"
;
RestTemplate restTemplate =
new
RestTemplate();
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)
public
String getAllEmployeesJSON(Model model)
{
model.addAttribute(
"employees"
, getEmployeesCollection());
return
"jsonTemplate"
;
}
REST Client Code
private
static
void
getEmployees()
{
final
String uri =
"http://localhost:8080/springrestexample/employees"
;
RestTemplate restTemplate =
new
RestTemplate();
HttpHeaders headers =
new
HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity =
new
HttpEntity<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)
public
String getAllEmployeesXML(Model model)
{
model.addAttribute(
"employees"
, getEmployeesCollection());
return
"xmlTemplate"
;
}
REST Client Code
private
static
void
getEmployees()
{
final
String uri =
"http://localhost:8080/springrestexample/employees"
;
RestTemplate restTemplate =
new
RestTemplate();
EmployeeListVO result = restTemplate.getForObject(uri, EmployeeListVO.
class
);
System.out.println(result);
}
5) Passing parameters in URL
REST API Code
@RequestMapping
(value =
"/employees/{id}"
)
public
ResponseEntity<EmployeeVO> getEmployeeById (
@PathVariable
(
"id"
)
int
id)
{
if
(id <=
3
) {
EmployeeVO employee =
new
EmployeeVO(
1
,
"Lokesh"
,
"Gupta"
,
"howtodoinjava@gmail.com"
);
return
new
ResponseEntity<EmployeeVO>(employee, HttpStatus.OK);
}
return
new
ResponseEntity(HttpStatus.NOT_FOUND);
}
REST Client Code
private
static
void
getEmployeeById()
{
final
String uri =
"http://localhost:8080/springrestexample/employees/{id}"
;
Map<String, String> params =
new
HashMap<String, String>();
params.put(
"id"
,
"1"
);
RestTemplate restTemplate =
new
RestTemplate();
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)
public
ResponseEntity<String> createEmployee(
@RequestBody
EmployeeVO employee)
{
System.out.println(employee);
return
new
ResponseEntity(HttpStatus.CREATED);
}
REST Client Code
private
static
void
createEmployee()
{
final
String uri =
"http://localhost:8080/springrestexample/employees"
;
EmployeeVO newEmployee =
new
EmployeeVO(-
1
,
"Adam"
,
"Gilly"
,
"test@email.com"
);
RestTemplate restTemplate =
new
RestTemplate();
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)
public
ResponseEntity<EmployeeVO> updateEmployee(
@PathVariable
(
"id"
)
int
id,
@RequestBody
EmployeeVO employee)
{
System.out.println(id);
System.out.println(employee);
return
new
ResponseEntity<EmployeeVO>(employee, HttpStatus.OK);
}
REST Client Code
private
static
void
deleteEmployee()
{
final
String uri =
"http://localhost:8080/springrestexample/employees/{id}"
;
Map<String, String> params =
new
HashMap<String, String>();
params.put(
"id"
,
"2"
);
EmployeeVO updatedEmployee =
new
EmployeeVO(
2
,
"New Name"
,
"Gilly"
,
"test@email.com"
);
RestTemplate restTemplate =
new
RestTemplate();
restTemplate.put ( uri, updatedEmployee, params);
}
HTTP DELETE Method Example
REST API Code
@RequestMapping
(value =
"/employees/{id}"
, method = RequestMethod.DELETE)
public
ResponseEntity<String> updateEmployee(
@PathVariable
(
"id"
)
int
id)
{
System.out.println(id);
return
new
ResponseEntity(HttpStatus.OK);
}
REST Client Code
private
static
void
deleteEmployee()
{
final
String uri =
"http://localhost:8080/springrestexample/employees/{id}"
;
Map<String, String> params =
new
HashMap<String, String>();
params.put(
"id"
,
"2"
);
RestTemplate restTemplate =
new
RestTemplate();
restTemplate.delete ( uri, params );
}
Let me know if something needs more explanation.
阅读全文
0 0
- Springmvc RestTemplate 使用 && json xml rest 配置
- springmvc+velocity+ Rest Services(xml,json)实例
- springmvc+velocity+ Rest Services(xml,json)实例
- Spring REST Client with RestTemplate: Consume RESTful Web Service Example for XML and JSON
- Spring REST Client with RestTemplate: Consume RESTful Web Service Example for XML and JSON
- spring中 使用restTemplate访问rest接口
- 使用spring RestTemplate进行rest接口测试
- spring rest mvc使用RestTemplate调用
- spring rest mvc使用RestTemplate调用
- 使用spring RestTemplate进行rest接口测试
- 零xml配置SpringMVC返回json数据
- Spring MVC Rest Json,XML多视图解析 配置
- Spring MVC Rest Json,XML多视图解析 配置
- 使用spring框架中的 RestTemplate访问rest接口
- REST In Spring : RestTemplate
- REST访问(RestTemplate)
- JSON+REST vs. XML+SOAP
- springMVC中使用Json数据相关配置
- 在命令行中通过adb shell am broadcast发送广播通知
- java.lang.reflect.InvocationTargetException
- mysql 连接闪断自动重连的方法(用在后台运行中的PHP代码)
- Maven项目无法读取src/main/resources下properties资源文件
- 友元函数常见问题
- Springmvc RestTemplate 使用 && json xml rest 配置
- JUnit单元测试框架的使用
- HDU1402 A * B Problem Plus[FFT]
- QTabWidget添加自定义样式
- 【ironic】ironic 命令
- python+sklearn利用特征文件来训练和测试模型并使用joblib方法持久化存储模型
- 首页学习--一些比较杂的小点
- java调用接口
- Kettle案例Demo