Tengo la intención de configurar una aplicación Primavera-angular. Empecé de inmediato con un Hello World
-ejemplo para probar cómo configurar el entorno. Lo que terminé haciendo:
Creación de una Primavera-proyectos y la creación de una aplicación angular dentro de esta aplicación. Ahora puedo acceder Spring-REST-Controllers
a través del HttpClient
Módulo angular. (Código de ejemplo véase más adelante).
La ventaja: puedo usar mvn package para empacar los Angular- y primaveral partes en un frasco y simplemente desplegarlo en mi Tomcat. Por desgracia, cuando corro ng serve
sólo el frontend se ejecuta y no puedo acceder a los datos en mi servidor. ¿Hay una manera de configurar mi entorno para que pueda tener la ventaja de una solución de un solo proyecto y seguir utilizando ng servir para probarlo?
Lo que he intentado:
Empaque el frasco y lo ejecuta a través del terminal ( java -jar %jar-file%
) y utilizando localhost:8080/hello
como un camino para mi HttpClient
en lugar de un simple /hello
. Eso no funcionó con tristeza.
El código que tengo hasta ahora:
app.component.ts
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'HelloWorld';
message: string;
constructor(private http : HttpClient) {}
ngOnInit() : void {
//this is where I tried to use localhost:8080/hello instead
this.http.get('/hello').subscribe( data => {
console.log('DATA', data);
this.message = data['message'];
});
}
}
Descansa-Controller:
package com.example.helloWorld.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@GetMapping("/hello")
public String sayHello() {
return "{\"message\": \"Hello, World!\"}";
}
}
pom.xml http://maven.apache.org/xsd/maven-4.0.0.xsd "> 4.0.0
<groupId>com.example</groupId>
<artifactId>helloWorld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>helloWorld</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-mockmvc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.1.0.RELEASE</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>ng</executable>
<workingDirectory>src/main/ui</workingDirectory>
<arguments>
<argument>build</argument>
</arguments>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Para hacer como esto
this.http.get('/hello').subscribe( data => {
console.log('DATA', data);
this.message = data['message'];
});
Es necesario hacer algunas configuraciones de proxy. Crear un proxy-config.json
archivo dentro de su proyecto en el mismo directorio en el que package.json
está allí.
{
"/": {
"target": "http://localhost:8080",
"secure": false
}
}
y en package.json
el interior de scripts
la actualización "start"
del sistema con "start": "ng serve --proxy-config proxy-config.json",
Después de eso intento npm start
de comandos para ejecutar el proyecto.