我有一个问题,连接角应用程序与Spring休息后端。有什么简单的方法可以让它在一个本地主机端口上一起运行吗?
发布于 2019-01-25 19:12:10
如果您使用默认设置(角CLI: ng serve)运行应用程序,前端将在端口4200上启动。
后端应用程序将在application.yml (或application.properties)文件中的端口设置上启动。
检查运行后端应用程序的端口:
server:
port: ${PORT:10101}接下来,创建一个proxy.config.json文件(例如,使用package.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));
}
}https://stackoverflow.com/questions/54368813
复制相似问题