Size: 2590
Comment:
|
Size: 5369
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 75: | Line 75: |
=== Application.java === {{{#!highlight java package chucknorris.bitarus.allowed.org; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.SpringApplication; import org.springframework.context.annotation.ComponentScan; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @ComponentScan //scans for @Component beans @EnableAutoConfiguration public class Application { private static Logger logger; public static void main(String[] args) { logger = LoggerFactory.getLogger(Application.class); logger.info("Starting application"); SpringApplication.run(Application.class, args); } } }}} === ChuckNorrisController.java === {{{#!highlight java package chucknorris.bitarus.allowed.org; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.ui.Model; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.ResponseBody; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import org.springframework.http.HttpMethod; import com.google.gson.Gson; @Controller public class ChuckNorrisController{ private final Logger logger = LoggerFactory.getLogger(ChuckNorrisController.class); public ChuckNorrisController(){ logger.info("ChuckNorrisController created"); } @RequestMapping("/chucknorris") @ResponseBody // http://localhost:8080/chucknorris public JokeResponse chucknorris() { String ret = ""; Gson gson = new Gson(); try { URL url = new URL("https://api.chucknorris.io/jokes/random"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.connect(); logger.info( Integer.toString(connection.getResponseCode() )); try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"))) { StringBuilder response = new StringBuilder(); String responseLine = null; while ((responseLine = br.readLine()) != null) { response.append(responseLine.trim()); } Joke joke = gson.fromJson(response.toString(), Joke.class); ret = joke.getValue(); } } catch(Exception ex){ logger.error("error",ex); } JokeResponse jr = new JokeResponse(); jr.setResponse(ret); return jr; } } }}} |
ChuckNorris
Test app that invokes a ChuckNorris random joke API
Structure
pom.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <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">
3 <modelVersion>4.0.0</modelVersion>
4 <groupId>bitarus.allowed.org</groupId>
5 <artifactId>chucknorris</artifactId>
6 <version>0.1.0</version>
7 <parent>
8 <groupId>org.springframework.boot</groupId>
9 <artifactId>spring-boot-starter-parent</artifactId>
10 <version>2.1.6.RELEASE</version>
11 </parent>
12 <dependencies>
13 <dependency>
14 <groupId>org.springframework.boot</groupId>
15 <artifactId>spring-boot-starter-thymeleaf</artifactId>
16 </dependency>
17 <dependency>
18 <groupId>org.springframework.boot</groupId>
19 <artifactId>spring-boot-starter-web</artifactId>
20 </dependency>
21 <dependency>
22 <groupId>com.google.code.gson</groupId>
23 <artifactId>gson</artifactId>
24 <version>2.8.6</version>
25 <scope>compile</scope>
26 </dependency>
27 </dependencies>
28 <properties>
29 <start-class>chucknorris.bitarus.allowed.org.Application</start-class>
30 </properties>
31 <build>
32 <plugins>
33 <plugin>
34 <groupId>org.springframework.boot</groupId>
35 <artifactId>spring-boot-maven-plugin</artifactId>
36 </plugin>
37 </plugins>
38 </build>
39 <repositories>
40 <repository>
41 <id>spring-milestone</id>
42 <url>http://repo.spring.io/libs-release</url>
43 </repository>
44 </repositories>
45 <pluginRepositories>
46 <pluginRepository>
47 <id>spring-milestone</id>
48 <url>http://repo.spring.io/libs-release</url>
49 </pluginRepository>
50 </pluginRepositories>
51 </project>
Application.java
1 package chucknorris.bitarus.allowed.org;
2
3 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
4 import org.springframework.boot.SpringApplication;
5 import org.springframework.context.annotation.ComponentScan;
6 import org.slf4j.Logger;
7 import org.slf4j.LoggerFactory;
8
9 @ComponentScan //scans for @Component beans
10 @EnableAutoConfiguration
11 public class Application {
12 private static Logger logger;
13
14 public static void main(String[] args) {
15 logger = LoggerFactory.getLogger(Application.class);
16 logger.info("Starting application");
17 SpringApplication.run(Application.class, args);
18 }
19 }
ChuckNorrisController.java
1 package chucknorris.bitarus.allowed.org;
2
3 import org.springframework.stereotype.Controller;
4 import org.springframework.web.bind.annotation.RequestMapping;
5 import org.springframework.ui.Model;
6 import org.slf4j.Logger;
7 import org.slf4j.LoggerFactory;
8 import org.springframework.web.bind.annotation.ResponseBody;
9
10 import java.io.BufferedReader;
11 import java.io.InputStreamReader;
12 import java.io.OutputStream;
13 import java.net.HttpURLConnection;
14 import java.net.URL;
15 import org.springframework.http.HttpMethod;
16 import com.google.gson.Gson;
17
18 @Controller
19 public class ChuckNorrisController{
20 private final Logger logger = LoggerFactory.getLogger(ChuckNorrisController.class);
21
22 public ChuckNorrisController(){
23 logger.info("ChuckNorrisController created");
24 }
25
26 @RequestMapping("/chucknorris")
27 @ResponseBody
28 // http://localhost:8080/chucknorris
29 public JokeResponse chucknorris() {
30 String ret = "";
31 Gson gson = new Gson();
32
33 try {
34 URL url = new URL("https://api.chucknorris.io/jokes/random");
35 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
36 connection.connect();
37 logger.info( Integer.toString(connection.getResponseCode() ));
38
39 try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"))) {
40 StringBuilder response = new StringBuilder();
41 String responseLine = null;
42 while ((responseLine = br.readLine()) != null) {
43 response.append(responseLine.trim());
44 }
45
46 Joke joke = gson.fromJson(response.toString(), Joke.class);
47 ret = joke.getValue();
48 }
49
50 }
51 catch(Exception ex){
52 logger.error("error",ex);
53 }
54
55 JokeResponse jr = new JokeResponse();
56 jr.setResponse(ret);
57 return jr;
58 }
59 }