如何在Angular/Spring Boot中正确配置代理以发出post请求



我有一个Angular应用程序,在那里我配置了一个代理,它将我的api调用路由到我的网关,并从我的网关路由到我各自的微服务。到目前为止,它看起来至少在路由部分起作用。但当我提出请求时,我得到了一个错误:405这个方法(帖子(是不允许的。但在我的后端,我调用的api是一个post请求。我真的不明白为什么会发生这种情况,我想我的代理或网关可能坏了?

Gateway和Microservice是通过jhipster生成的,并且完全没有更改。它接受微服务中的URL来调用该方法。

我的Proxy.conf.json

{
"/api": {
"target": "http://localhost:8080",
"secure": false,
"pathRewrite": {
"^/api": "/api"
},
"logLevel": "debug"
},
"/moniestaproduction": {
"target": "http://localhost:8080",
"secure": false,
"pathRewrite": {
"^/moniestaproduction": "/moniestaproduction"
},
"logLevel": "debug"
}
}

我的微服务中的后端方法:

@PostMapping("/materials-add")
public ResponseEntity<MaterialDTO> createMaterial(@RequestBody MaterialDTO materialDTO) throws URISyntaxException {
log.debug("REST request to save Material : {}", materialDTO);
if (materialDTO.getId() != null) {
throw new BadRequestAlertException("A new material cannot already have an ID", ENTITY_NAME, "idexists");
}
MaterialDTO result = materialService.save(materialDTO);
return ResponseEntity.created(new URI("/api/materials/" + result.getId()))
.headers(HeaderUtil.createEntityCreationAlert(applicationName, true, ENTITY_NAME, result.getId().toString()))
.body(result);
}

我试着在类lvl上添加这一行,但没有成功:

@RequestMapping(value = "/api", method = {RequestMethod.POST})

这就是我从Angular调用api的方式:

addMaterial(material: MaterialModel): Observable<HttpResponse<any>> {
return this.http.post<MaterialModel>(environment.api_server_url + 'moniestaproduction/api/materials-add', material, {
observe: 'response'
});
console.log("HALLO RESPONSE")
}

编辑:

我试图通过邮递员访问,但它仍然没有工作,同样的错误:405

我修复了它,我必须使用的url是http://localhost:8080/services/moniestaproduction/api/materials-add。我认为gateway已经添加了服务部分,我真的不知道在哪里以及如何添加,但现在它可以工作了。

最新更新