【发布时间】:2020-03-07 05:00:45
【问题描述】:
我正在开发一个有角度的弹簧启动应用程序。
我提出以下要求
getListProduitImmobilierDTO(pagesize: number, page: number, search: Search): Observable<ProduitImmobilierDTO[]> {
const headerDict = {
'Content-Type': 'application/json',
Accept: 'application/json',
'Accept-Charset': 'charset=UTF-8',
'Access-Control-Allow-Headers': 'Content-Type'
};
const requestOptions = {
headers: new HttpHeaders(headerDict)
};
return this.http.get('/api/produitimmobilier/all/' + pagesize + '/' + page, requestOptions).pipe(map((jsonArray: any) =>jsonArray.map((jsonItem) => ProduitImmobilierDTO.fromJson(jsonItem))));
我使用 proxy.config.json 重定向请求,如下所示
{
"/api/*": {
"target": {
"host": "localhost",
"protocol": "http:",
"port": 8080
},
"secure": false,
"changeOrigin": true,
"logLevel": "info"
}
}
为了启动 Angular 应用程序,我使用以下命令
ng serve --proxy-config proxy.config.json
在服务器端,我有以下 spring boot 控制器:
@CrossOrigin(origins = "http://localhost:4200", maxAge = 3600)
@RestController
@RequestMapping({"/api"})
public class ProduitImmobilierController {
Logger logger = LoggerFactory.getLogger(ProduitImmobilierController.class);
@Autowired
private ProduitImmobilierService produitImmobilierService;
@RequestMapping(value = "/produitimmobilier/all/{pageSize}/{page}",
method = RequestMethod.GET,
produces = {"text/plain;charset=UTF-8", MediaType.APPLICATION_JSON_VALUE},
consumes = {"text/plain;charset=UTF-8", MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody List<ProduitImmobilierDTO> findAll(@PathVariable("pageSize") int pageSize, @PathVariable("page") int page){
logger.info("CONTROLLER PRODUITIMMOBILIERSERVICE CA PASSE");
return produitImmobilierService.findAll(pageSize, page);
}
它曾经工作过。我尝试修改请求,但在反转后,它不再起作用了。我有以下错误
An attempt to set a forbidden header has been blocked : Accept-Charset http.js:2346
Object { headers: {…}, status: 404, statusText: "Not Found", url: "http://localhost:4200/api/produitimmobilier/all/5/1", ok: false, name: "HttpErrorResponse", message: "Http failure response for http://localhost:4200/api/produitimmobilier/all/5/1: 404 Not Found", error: {…} }
你能帮我吗
【问题讨论】:
标签: angular spring-boot httpclient http-proxy