文章目录
@RestController
public class DepetController {
@Resource
private DeptService deptService;
/**
* 根据Id查找
* @param id
* @return
*/
@GetMapping("/dept/{id}")
public Dept getDept(@PathVariable("id") Integer id){
Dept dept = deptService.getDeptById(id);
return dept;
}
/**
* 分页获取列表
* @param pageNum
* @param pageSize
* @return
*/
@GetMapping("/dept")
public List<Dept> getDeptList(Integer pageNum,Integer pageSize){
return deptService.getDeptList(pageNum,pageSize);
}
/**
* 添加dept
* @param dept
* @return
*/
@PostMapping("/dept")
public Dept addDepet(@RequestBody Dept dept){
Dept dept1=deptService.addDept(dept);
return dept1;
}
/**
* 修改dept
* @param dept
* @return
*/
@PutMapping("/dept")
public boolean modifyDept(@RequestBody Dept dept){
return deptService.modifyDept(dept);
}
/**
* 根据Id删除
* @param id
* @return
*/
@DeleteMapping("/dept/{id}")
public boolean deleteDept(@PathVariable("id") Integer id){
return deptService.deleteDept(id);
}
}
@Configuration
public class RestConfig {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
return new RestTemplate(factory);
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setReadTimeout(5000);//单位为ms
factory.setConnectTimeout(5000);//单位为ms
return factory;
}
}
/**
* 通过get请求方式
* @param id
* @param name
* @return
*/
@GetMapping("/dept/{id}/{name}")
public Dept getDept(@PathVariable("id") Integer id,@PathVariable("name")String name){
Dept dept=new Dept();
dept.setDepetId(id);
dept.setDepetName(name);
return dept;
}
public <T> T getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables)
/**
* 使用RestTemplate的getForObject()发出get请求
*/
@GetMapping("/consumer/{id}/{name}")
public Dept getDept(@PathVariable("id")Integer id,@PathVariable("name") String name) throws RestClientException, URISyntaxException{
//使用一个Map封装Get请求的参数,其中key一定要和uri中的占位符一致
Map<String, Object> params=new HashMap<String, Object>();
params.put("id", id);
params.put("name", name);
Dept dept = restTemplate.getForObject(URL_PRFIX+"{id}/{name}", Dept.class,params);
return dept;
}
public <T> T getForObject(URI url, Class<T> responseType)
/**
* 使用RestTemplate的getForObject()发出get请求
*/
@GetMapping("/consumer/{id}/{name}")
public Dept getDept(@PathVariable("id")Integer id,@PathVariable("name") String name) throws RestClientException, URISyntaxException{
//直接在uri中拼接参数
Dept dept = restTemplate.getForObject(URL_PRFIX+id+"/"+name, Dept.class);
return dept;
}
public <T> T getForObject(String url, Class<T> responseType, Object... uriVariables)
/**
* 使用RestTemplate的getForObject()发出get请求
*/
@GetMapping("/consumer/{id}/{name}")
public Dept getDept(@PathVariable("id")Integer id,@PathVariable("name") String name) throws RestClientException, URISyntaxException{
//使用{id}作为一个占位符,第三个参数是用来设置这个占位符的值,当然可以有多个占位符
Dept dept = restTemplate.getForObject(URL_PRFIX+"{id}/{name}", Dept.class, id,name);
return dept;
}
public <T> T postForObject(URI url, @Nullable Object request, Class<T> responseType)
public <T> T postForObject(String url, @Nullable Object request, Class<T> responseType,Map<String, ?> uriVariables)
public <T> T postForObject(String url, @Nullable Object request, Class<T> responseType,Object... uriVariables)
/**
* 新建数据
*/
@PostMapping("/dept")
public Dept addDept(@RequestBody Dept dept) throws RestClientException, URISyntaxException{
Dept dept2 = restTemplate.postForObject(new URI(URL_PRFIX), dept, Dept.class);
return dept2;
}
public void put(URI url, @Nullable Object request)
public void put(String url, @Nullable Object request, Object... uriVariables)
/**
* 更新数据
* @param dept
* @throws RestClientException
* @throws URISyntaxException
*/
@PutMapping("/dept")
public void modifyDept(@RequestBody Dept dept) throws RestClientException, URISyntaxException{
restTemplate.put(new URI(URL_PRFIX), dept);
}
public void delete(URI url)
public void delete(String url, Object... uriVariables)
public void delete(String url, Map<String, ?> uriVariables)
/**
* 删除数据
* @param id
*/
@DeleteMapping("/dept/{id}")
public void delete(@PathVariable("id")Integer id){
restTemplate.delete(URL_PRFIX+"/{id}", id);
}