Commit d8aeef23 authored by doc. Ing. Jaroslav Porubän PhD.'s avatar doc. Ing. Jaroslav Porubän PhD.
Browse files

8. prednaska

parent fcbcf3a9
......@@ -29,6 +29,11 @@
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
......
......@@ -2,20 +2,28 @@ package sk.tuke.gamestudio;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import sk.tuke.gamestudio.game.mines.consoleui.ConsoleUI;
import sk.tuke.gamestudio.game.mines.core.Field;
import sk.tuke.gamestudio.service.ScoreService;
import sk.tuke.gamestudio.service.ScoreServiceJDBC;
import sk.tuke.gamestudio.service.ScoreServiceJPA;
import sk.tuke.gamestudio.service.ScoreServiceRestClient;
@SpringBootApplication
@Configuration
@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX,
pattern = "sk.tuke.gamestudio.server.*"))
public class SpringClient {
public static void main(String[] args) {
SpringApplication.run(SpringClient.class, args);
new SpringApplicationBuilder(SpringClient.class).web(WebApplicationType.NONE).run(args);
//SpringApplication.run(SpringClient.class, args);
}
@Bean
......@@ -36,6 +44,7 @@ public class SpringClient {
@Bean
public ScoreService scoreService() {
//return new ScoreServiceJDBC();
return new ScoreServiceJPA();
//return new ScoreServiceJPA();
return new ScoreServiceRestClient();
}
}
\ No newline at end of file
package sk.tuke.gamestudio.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import sk.tuke.gamestudio.service.ScoreService;
import sk.tuke.gamestudio.service.ScoreServiceJPA;
@SpringBootApplication
@Configuration
@EntityScan({"sk.tuke.gamestudio.entity"})
public class GameStudioServer {
public static void main(String[] args) {
SpringApplication.run(GameStudioServer.class, args);
}
@Bean
public ScoreService scoreService() {
//return new ScoreServiceJDBC();
return new ScoreServiceJPA();
}
}
package sk.tuke.gamestudio.server.webservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sk.tuke.gamestudio.entity.Score;
import sk.tuke.gamestudio.service.ScoreService;
import java.util.List;
@RestController
@RequestMapping("/api/score")
//http://localhost:8080/api/score/mines
public class ScoreRestService {
@Autowired
private ScoreService scoreService;
@GetMapping("/{game}")
public List<Score> getTopScores(@PathVariable String game) {
return scoreService.getTopScores(game);
}
@PostMapping
public void addScore(@RequestBody Score score) {
scoreService.addScore(score);
}
}
package sk.tuke.gamestudio.service;
import org.springframework.web.client.RestTemplate;
import sk.tuke.gamestudio.entity.Score;
import java.util.Arrays;
import java.util.List;
public class ScoreServiceRestClient implements ScoreService {
private static final String URL = "http://localhost:8080/api/score";
private RestTemplate restTemplate = new RestTemplate();
@Override
public void addScore(Score score) {
restTemplate.postForEntity(URL, score, Score.class);
}
@Override
public List<Score> getTopScores(String gameName) {
return Arrays.asList(restTemplate.getForEntity(URL + "/" + gameName, Score[].class).getBody());
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment