|
@@ -0,0 +1,42 @@
|
|
|
|
+
|
|
|
|
+import java.net.URI;
|
|
|
|
+
|
|
|
|
+import org.example.Article;
|
|
|
|
+import org.springframework.http.HttpEntity;
|
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
|
+import org.springframework.http.HttpMethod;
|
|
|
|
+import org.springframework.http.MediaType;
|
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
|
+public class clientTest {
|
|
|
|
+ public void getAllArticlesDemo() {
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
|
+ String url = "http://localhost:8080/user/articles";
|
|
|
|
+ HttpEntity<String> requestEntity = new HttpEntity<String>(headers);
|
|
|
|
+ ResponseEntity<Article[]> responseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, Article[].class);
|
|
|
|
+ Article[] articles = responseEntity.getBody();
|
|
|
|
+ for(Article article : articles) {
|
|
|
|
+ System.out.println("Id:"+article.getArticleId()+", Title:"+article.getTitle()
|
|
|
|
+ +", Category: "+article.getCategory());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ public void addArticleDemo() {
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
|
+ String url = "http://localhost:8080/user/article";
|
|
|
|
+ Article objArticle = new Article();
|
|
|
|
+ objArticle.setTitle("Spring REST Security using Hibernate");
|
|
|
|
+ objArticle.setCategory("Spring");
|
|
|
|
+ HttpEntity<Article> requestEntity = new HttpEntity<Article>(objArticle, headers);
|
|
|
|
+ URI uri = restTemplate.postForLocation(url, requestEntity);
|
|
|
|
+ System.out.println(uri.getPath());
|
|
|
|
+ }
|
|
|
|
+ public static void main(String args[]) {
|
|
|
|
+ clientTest util = new clientTest();
|
|
|
|
+ util.addArticleDemo();
|
|
|
|
+ util.getAllArticlesDemo();
|
|
|
|
+ }
|
|
|
|
+}
|