Skip to content

Commit d9e2639

Browse files
committed
URI URL REST commit.
1 parent a45541a commit d9e2639

6 files changed

Lines changed: 156 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.springboot.rest;
2+
3+
public class Greeting {
4+
private static final long serialVersionUID = 1L;
5+
6+
private Integer id = null;
7+
private String content = null;
8+
9+
public Greeting(Integer id) {
10+
this.id = id;
11+
this.content = "Hello World";
12+
}
13+
14+
public Integer getId() {
15+
return id;
16+
}
17+
18+
public String getContent() {
19+
return content;
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.springboot.rest;
2+
3+
import java.util.concurrent.atomic.AtomicLong;
4+
5+
import org.springframework.stereotype.Component;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RequestMethod;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
@RestController("/")
11+
@Component
12+
public class GreetingController {
13+
14+
private final AtomicLong counter = new AtomicLong();
15+
16+
@RequestMapping(value = "/greetings", method = RequestMethod.GET)
17+
public Greeting greeting() {
18+
19+
return new Greeting(new Integer((int) counter.incrementAndGet()));
20+
}
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.springboot.rest;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.boot.builder.SpringApplicationBuilder;
7+
import org.springframework.boot.web.support.SpringBootServletInitializer;
8+
9+
@EnableAutoConfiguration
10+
@SpringBootApplication
11+
public class SpringBootWebApplication extends SpringBootServletInitializer {
12+
13+
// method for explicit deployment on Application Server
14+
@Override
15+
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
16+
return application.sources(SpringBootWebApplication.class);
17+
}
18+
19+
// run it as standalone JAVA application
20+
public static void main(String[] args) throws Exception {
21+
SpringApplication.run(SpringBootWebApplication.class, args);
22+
}
23+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.springboot.rest.client;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.web.client.RestTemplate;
6+
7+
public class ApplicationClient {
8+
//private static final Logger log = LoggerFactory.getLogger(ApplicationClient.class);
9+
final static String URI_STRING = "http://localhost:8080/spring-boot/greetings";
10+
11+
12+
public ApplicationClient() {
13+
super();
14+
}
15+
16+
public Greeting init() {
17+
RestTemplate restTemplate = new RestTemplate();
18+
Greeting greeting = restTemplate.getForObject(ApplicationClient.URI_STRING, Greeting.class);
19+
return greeting;
20+
}
21+
public static void main(String args[]) {
22+
Greeting greeting = new ApplicationClient().init();
23+
System.out.println(greeting.toString());
24+
}
25+
26+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.springboot.rest.client;
2+
3+
import java.io.Serializable;
4+
5+
public class Greeting implements Serializable {
6+
private static final long serialVersionUID = 1L;
7+
8+
private Integer id = null;
9+
private String content = null;
10+
11+
/** Default constructor is mandatory for client */
12+
public Greeting() {
13+
super();
14+
}
15+
16+
public Greeting(Integer id) {
17+
this.id = id;
18+
this.content = "Hello World";
19+
}
20+
21+
public Integer getId() {
22+
return id;
23+
}
24+
25+
public String getContent() {
26+
return content;
27+
}
28+
29+
@Override
30+
public String toString() {
31+
return "Id: " + getId().toString() + " Content: " + getContent();
32+
}
33+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.springboot.rest.test;
2+
3+
import static org.junit.Assert.assertNotNull;
4+
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
import org.springframework.web.client.RestTemplate;
8+
9+
import com.baeldung.springboot.rest.client.Greeting;
10+
11+
public class DifferenceURIURLRESTTest {
12+
final static String URI_STRING = "http://localhost:8989/DifferenceURIURLREST/greetings";
13+
static RestTemplate restTemplate;
14+
Greeting greeting;
15+
16+
@BeforeClass
17+
public static void setupTest() {
18+
restTemplate = new RestTemplate();
19+
}
20+
21+
@Test
22+
public void givenRestTenplate_whenIsNotNull_thenSuccess() {
23+
assertNotNull("Rest Template not null", restTemplate);
24+
}
25+
26+
@Test
27+
public void givenWiredConstructorParam_whenIsNotNull_thenSuccess() {
28+
greeting = restTemplate.getForObject(URI_STRING, Greeting.class);
29+
assertNotNull("Greeting class is not null", greeting);
30+
}
31+
32+
}

0 commit comments

Comments
 (0)