要解决这个问题,你需要进行以下步骤:
npm install -g @angular/cli
ng new angular-app
cd angular-app
mvn archetype:generate -DgroupId=com.example -DartifactId=spring-boot-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
cd spring-boot-app
pom.xml
文件中添加以下依赖项:
org.springframework.boot
spring-boot-starter-web
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello from Spring Boot!";
}
}
mvn spring-boot:run
http-proxy-middleware
库:npm install http-proxy-middleware --save
proxy.conf.json
,并添加以下内容:{
"/api": {
"target": "https://localhost:8080",
"secure": false,
"changeOrigin": true,
"logLevel": "debug"
}
}
这将配置一个代理,将所有以/api
开头的请求发送到https://localhost:8080
。
package.json
文件中,将start
命令修改为以下内容:"start": "ng serve --proxy-config proxy.conf.json"
ng serve
http://localhost:4200
,然后在开发者工具的控制台中查看输出。你应该看到Angular应用程序将请求代理到Spring Boot应用程序。这样,你就可以通过https访问代理的本地Spring Boot应用程序了。在Angular应用程序中,使用/api
前缀来代理所有与Spring Boot应用程序的交互。在proxy.conf.json
文件中,你可以根据实际情况调整代理的目标URL和其他配置。