Bonjour,
Ce sujet va avec la discussion ici:
https://www.developpez.net/forums/d2...ffichage-page/
J'ai un véritable problème bloquant, et je n'arrive pas à trouver une solution satisfaisante.
Je dois construire un projet, avec Spring, qui va vers une page HTML, mais comme la page HTML sera avec un framework Javascript (comme Vue.JS), je dois pouvoir appeler des Rest Controller.
Autant je vais sur la page HTML, mais pour les services REST, il est totalement à la ramasse.
Vital, comme j'ai un existant, je dois packager en WAR (et non en JAR).
Pour commencer, l'architecture du projet:
J'ai le POM suivant:
Pour la DTO, je crée la DTO bidon suivante:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 <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>com.spring.tomcat.war.test</groupId> <artifactId>com.spring.tomcat.war.test</artifactId> <version>0.0.1-SNAPSHOT</version> <name>com.spring.tomcat.war.test</name> <description>com.spring.tomcat.war.test</description> <packaging>war</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <encoding>UTF-8</encoding> <java.version>1.8</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.10.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <finalName>${project.artifactId}</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <executions> <execution> <id>default-war</id> <phase>prepare-package</phase> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
Je définis le service suivant:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package com.spring.tomcat.war.test.dto; public class TestDTO { private Integer numero; private String message; public Integer getNumero() { return numero; } public void setNumero(Integer numero) { this.numero = numero; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
Dont l'implémentation est:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 package com.spring.tomcat.war.test.service; import java.util.List; import com.spring.tomcat.war.test.dto.TestDTO; public interface ServiceTest { List<TestDTO> getTest(); }
On arrive au controller:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package com.spring.tomcat.war.test.service; import java.util.ArrayList; import java.util.List; import java.util.Random; import org.springframework.stereotype.Service; import com.spring.tomcat.war.test.dto.TestDTO; @Service public class ServiceTestImpl implements ServiceTest{ public ServiceTestImpl() { System.out.println("Constructeur Test"); } @Override public List<TestDTO> getTest() { Random random = new Random(); final int number = random.nextInt(20); List<TestDTO> lReturn = new ArrayList<>(); TestDTO dto; for(int i = 0; i < number;i++) { dto = new TestDTO(); dto.setNumero(i); dto.setMessage("Message "+i); } return lReturn; } }
Et la classe de configuration:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package com.spring.tomcat.war.test.controller; import java.util.List; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.spring.tomcat.war.test.dto.TestDTO; import com.spring.tomcat.war.test.service.ServiceTest; @RestController @RequestMapping("/test") public class TestController { private final ServiceTest serviceTest; public TestController(ServiceTest serviceTest) { System.err.println("Test"); this.serviceTest = serviceTest; } @GetMapping(value = "/all",produces = {MediaType.APPLICATION_JSON_VALUE}) public ResponseEntity<List<TestDTO>> getAll(){ return ResponseEntity.ok(serviceTest.getTest()); } }
Le lancement (ici par Eclipse) permet de voir que les constructeurs sont appelé, dans le bon ordre, et si on debug, on a bien des valeurs de classes (et non pas null):
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.spring.tomcat.war.test.configuration; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration @ComponentScan(basePackages = {"com.spring.tomcat.war.test.controller","com.spring.tomcat.war.test.service"}) public class TestConfiguration extends SpringBootServletInitializer implements WebMvcConfigurer{ @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(TestConfiguration.class); } }
Et la page index.html est appelée:
Seulement, pas le service REST:
Et pas de trace dans la console d'Eclipse.
Bref, je sais que j'ai loupé quelque chose, mais je n'arrive pas à mettre la main dessus.
Cordialement.
Partager