如何使用Spring Boot后端部署Angular 7 front
我在将Angular应用程序与Spring Boot REST后端连接时遇到问题。有什么简单的方法可以让它同时在一个本地主机端口上运行吗 如果使用默认设置运行应用程序,则前端将从端口4200启动 后端应用程序将在application.yml或application.properties文件中的端口集上启动 检查在哪个端口运行后端应用程序:如何使用Spring Boot后端部署Angular 7 front,angular,spring-boot,angular-cli,Angular,Spring Boot,Angular Cli,我在将Angular应用程序与Spring Boot REST后端连接时遇到问题。有什么简单的方法可以让它同时在一个本地主机端口上运行吗 如果使用默认设置运行应用程序,则前端将从端口4200启动 后端应用程序将在application.yml或application.properties文件中的端口集上启动 检查在哪个端口运行后端应用程序: server: port: ${PORT:10101} 接下来,使用package.json文件创建一个proxy.config.json文件,如下所
server:
port: ${PORT:10101}
接下来,使用package.json文件创建一个proxy.config.json文件,如下所示:
{
"/api/*": {
"target": "http://localhost:10101",
"secure": false,
"logLevel": "debug"
}
}
然后将package.json文件添加到启用前端应用程序条目的脚本中:
"scripts": {
"ng": "ng",
"start": "ng serve --proxy-config proxy.config.json",
...
以及从终端启动前端:
npm启动
@Injectable请求后端的示例:
@Injectable()
export class MyService {
constructor(private http: HttpClient) {
}
searchClients(words: string): Observable<ClientDTO[]> {
return this.http.get<ClientDTO[]>('api/client/search?searchWords=' + encodeURIComponent(words));
}
}
和后端@RestController:
@RestController
@RequestMapping(path = "api/client")
public class ClientController {
private final ClientService clientService;
@Autowired
public ClientController(ClientService clientService) {
this.clientService = clientService;
}
@GetMapping(path = "search")
public ResponseEntity<List<ClientDTO>> searchClient(@RequestParam String searchWords) {
return ResponseEntity.ok(clientService.searchClient(searchWords));
}
}
请添加说明。出现了什么问题,犯了什么错误。请参阅有关如何提问的教程。请参阅“如何提问”页面以获得澄清此问题的帮助。检查本课程