Difference between url and uri bael 864 hariprasad (#1987)

* Commit URUURLJNDIFS added.

* URI URL REST commit.

* Revert "URI URL REST commit."

This reverts commit d9e26399be.

* Difference URI URL REST BAEL-864.

* Commit Difference URI URL REST #864, small changes.

* Difference URI URL REST project has been moved to spring-rest.

* BAEL-864. Deleted unused project and did one small change.
This commit is contained in:
hariprasad108
2017-06-04 21:39:21 +02:00
committed by maibin
parent 29e3437545
commit c17d19ff21
11 changed files with 291 additions and 4 deletions

View File

@@ -0,0 +1,32 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.baeldung.springboot.rest</groupId>
<artifactId>difference-uri-url-rest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,21 @@
package com.baeldung.springboot.rest;
public class Greeting {
private static final long serialVersionUID = 1L;
private Integer id = null;
private String content = null;
public Greeting(Integer id) {
this.id = id;
this.content = "Hello World";
}
public Integer getId() {
return id;
}
public String getContent() {
return content;
}
}

View File

@@ -0,0 +1,21 @@
package com.baeldung.springboot.rest;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController("/")
@Component
public class GreetingController {
private final AtomicLong counter = new AtomicLong();
@RequestMapping(value = "/greetings", method = RequestMethod.GET)
public Greeting greeting() {
return new Greeting(new Integer((int) counter.incrementAndGet()));
}
}

View File

@@ -0,0 +1,27 @@
package com.baeldung.springboot.rest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
@EnableAutoConfiguration
@SpringBootApplication
public class SpringBootWebApplication extends SpringBootServletInitializer {
// method for explicit deployment on Application Server
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringBootWebApplication.class);
}
// run it as standalone JAVA application
public static void main(String[] args) throws Exception {
SpringApplication.run(SpringBootWebApplication.class, args);
}
//Samples
// http://localhost:8080/greetings
// http://localhost:8989/difference-uri-url-rest/greetings
}

View File

@@ -0,0 +1,27 @@
package com.baeldung.springboot.rest.client;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.client.RestTemplate;
public class ApplicationClient {
//private static final Logger log = LoggerFactory.getLogger(ApplicationClient.class);
final static String URI_STRING = "http://localhost:8080/difference-uri-url-rest/greetings";
public ApplicationClient() {
super();
}
public Greeting init() {
RestTemplate restTemplate = new RestTemplate();
Greeting greeting = restTemplate.getForObject(ApplicationClient.URI_STRING, Greeting.class);
//log.info(greeting.toString());
return greeting;
}
public static void main(String args[]) {
Greeting greeting = new ApplicationClient().init();
System.out.println(greeting.toString());
}
}

View File

@@ -0,0 +1,33 @@
package com.baeldung.springboot.rest.client;
import java.io.Serializable;
public class Greeting implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id = null;
private String content = null;
/** Default constructor is mandatory for client */
public Greeting() {
super();
}
public Greeting(Integer id) {
this.id = id;
this.content = "Hello World";
}
public Integer getId() {
return id;
}
public String getContent() {
return content;
}
@Override
public String toString() {
return "Id: " + getId().toString() + " Content: " + getContent();
}
}

View File

@@ -0,0 +1,32 @@
package com.baeldung.springboot.rest.test;
import static org.junit.Assert.assertNotNull;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.web.client.RestTemplate;
import com.baeldung.springboot.rest.client.Greeting;
public class DifferenceURIURLRESTTest {
final static String URI_STRING = "http://localhost:8080/difference-uri-url-rest/greetings";
static RestTemplate restTemplate;
Greeting greeting;
@BeforeClass
public static void setupTest() {
restTemplate = new RestTemplate();
}
@Test
public void givenRestTenplate_whenIsNotNull_thenSuccess() {
assertNotNull("Rest Template not null", restTemplate);
}
@Test
public void givenWiredConstructorParam_whenIsNotNull_thenSuccess() {
greeting = restTemplate.getForObject(URI_STRING, Greeting.class);
assertNotNull("Greeting class is not null", greeting);
}
}