好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

RestTemplate的DELETE及PUT等请求方法使用精讲

本文是精讲RestTemplate第5篇,前篇的blog访问地址如下:

RestTemplate在Spring或非Spring环境下使用精讲

RestTemplate实现多种底层HTTP客户端类库的切换用法

RestTemplate发送HTTP GET请求使用方法详解

RestTemplate发送HTTP POST请求使用方法详解

为了方便后续开发测试,首先介绍一个网站给大家。JSONPlaceholder是一个提供免费的在线REST API的网站,我们在开发时可以使用它提供的url地址测试下网络请求以及请求参数。或者当我们程序需要获取一些模拟数据、模拟图片时也可以使用它。

一、RESTful风格与HTTP method

熟悉RESTful风格的朋友,应该了解RESTful风格API使用HTTP method表达对资源的操作。

常用HTTP方法 RESTful风格语义(操作)
GET 查询、获取数据
POST 新增、提交数据
DELETE 删除数据
PUT 更新、修改数据
HEAD 获取HTTP请求头数据
OPTIONS 判断URL提供的当前API支持哪些HTTP method方法

在前面的章节,我已经为大家详细的介绍了RestTemplate的GET和POST的相关的使用方法,本节来为大家介绍DELETE、PUT、HEAD、OPTIONS。

二、使用 DELETE方法去删除资源

删除一个已经存在的资源,使用RestTemplate的delete(uri)方法。该方法会向URL代表的资源发送一个HTTP DELETE方法请求。

?

1

2

3

4

5

@Test

void testDelete()  {

    String url = "http://jsonplaceholder.typicode.com/posts/1" ;

    restTemplate.delete(url);

}

在前面章节测试类的基础上,写如上代码的测试用例。上面代码含义为删除posts列表里面的第1个帖子。

三、使用PUT方法去修改资源

修改一个已经存在的资源,使用RestTemplate的put()方法。该方法会向URL代表的资源发送一个HTTP PUT方法请求。

?

1

2

3

4

5

6

7

8

9

10

11

12

@Test

void testPut()  {

    // 请求地址

    String url = "http://jsonplaceholder.typicode.com/posts/1" ;

    // 要发送的数据对象(修改数据)

    PostDTO postDTO = new PostDTO();

    postDTO.setUserId( 110 );

    postDTO.setTitle( "zimug 发布文章" );

    postDTO.setBody( "zimug 发布文章 测试内容" );

    // 发送PUT请求

    restTemplate.put(url, postDTO);

}

上面代码RESTful风格语义是:修改posts列表里面的第1个帖子。

三、通用请求方法exchange方法

exchange方法是一个通用的方法,它可以发送GET、POST、DELETE、PUT等等HTTP方法请求。

下面的两种方式发送GET请求效果是一样的

?

1

2

3

4

5

6

//使用getForEntity发送GET请求

ResponseEntity<PostDTO> responseEntity

             = restTemplate.getForEntity(url, PostDTO. class );

//使用exchange发送GET请求

ResponseEntity<PostDTO> responseEntity = restTemplate.exchange(url, HttpMethod.GET,

             null , PostDTO. class );

下面的两种方式发送POST请求效果是一样的

?

1

2

3

4

5

6

// 使用postForEntity发送POST请求

ResponseEntity<String> responseEntity

             = restTemplate.postForEntity(url, postDTO, String. class );

// 使用exchange发送POST请求

ResponseEntity<String> responseEntity

             = restTemplate.exchange(url, HttpMethod.POST, null , String. class );

下面的两种方式发送DELETE请求效果是一样的,只是一个有返回值,一个返回值为void

?

1

2

3

4

// 使用delete发送DELETE请求,返回值为void

restTemplate.delete(url);

// 使用exchange发送DELETE请求

ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.DELETE, null ,String. class );

上面为大家举了几个用exchange()发送请求的例子,exchange()还能针对很多的HTTP method类型发送请求,是通用方法!

四、使用HEAD方法获取HTTP请求头数据

使用*headForHeaders()*API 获取某个资源的URI的请求头信息,并且只专注于获取HTTP请求头信息。

?

1

2

3

4

5

6

7

8

9

@Test

public void testHEAD()  {

    String url = "http://jsonplaceholder.typicode.com/posts/1" ;

    HttpHeaders httpHeaders  = restTemplate.headForHeaders(url);

    //断言该资源接口数据为JSON类型

    assertTrue(httpHeaders.getContentType()

                .includes(MediaType.APPLICATION_JSON));

    System.out.println(httpHeaders);

}

请求头信息输出打印结果如下

五、使用OPTIONS获取HTTP资源支持的method

下文代码使用optionsForAllow测试该URL资源是否支持GET、POST、PUT、DELETE,即增删改查。

?

1

2

3

4

5

6

7

8

9

@Test

public void testOPTIONS()  {

    String url = "http://jsonplaceholder.typicode.com/posts/1" ;

    Set<HttpMethod> optionsForAllow  = restTemplate.optionsForAllow(url);

    HttpMethod[] supportedMethods

                = {HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT, HttpMethod.DELETE};

    //测试该url资源是否支持GET、POST、PUT、DELETE,即增删改查

    assertTrue(optionsForAllow.containsAll(Arrays.asList(supportedMethods)));

}

以上就是RestTemplate的DELETE及PUT等请求方法使用精讲的详细内容,更多关于RestTemplate的DELETE及PUT等请求用法的资料请关注其它相关文章!

原文链接:https://zimug.blog.csdn.net/article/details/107909654

查看更多关于RestTemplate的DELETE及PUT等请求方法使用精讲的详细内容...

  阅读:62次