Java 使用交叉原点时拒绝连接
我用的是角形和弹簧靴。 当我想在本地主机之外调用api时 我给我一个连接拒绝错误 角度服务:Java 使用交叉原点时拒绝连接,java,spring,apache,spring-boot,Java,Spring,Apache,Spring Boot,我用的是角形和弹簧靴。 当我想在本地主机之外调用api时 我给我一个连接拒绝错误 角度服务: export class ProfileService { private baseUrl = 'http://localhost:8090/users'; constructor(private http: HttpClient) { } getProfile(id: number): Observable<Object> { return this.http
export class ProfileService {
private baseUrl = 'http://localhost:8090/users';
constructor(private http: HttpClient) {
}
getProfile(id: number): Observable<Object> {
return this.http.get(`${this.baseUrl}` + '/load/' + `${id}`);
}
从angular获取请求的userscontroller:
@RestController
@RequestMapping("/users")
public class UsersController {
@Autowired
private IUsersService iUsersService;
@GetMapping("/list/grid")
public Iterable<UsersViewModel> getAllEmployees() {
return Dozer.mapList(iUsersService.getAll(), UsersViewModel.class);
}
@GetMapping("/load/{id}")
public UsersViewModel getUserById(@PathVariable(value = "id") Long userId){
return Dozer.mapClass(iUsersService.findById(userId).get(),UsersViewModel.class);
}
}
@RestController
@请求映射(“/users”)
公共类用户控制器{
@自动连线
私人IUSERS服务IUSERS服务;
@GetMapping(“/list/grid”)
公共IT可获取的getAllEmployees(){
返回Dozer.mapList(iUsersService.getAll(),UsersViewModel.class);
}
@GetMapping(“/load/{id}”)
public UsersViewModel getUserById(@PathVariable(value=“id”)Long userId){
返回Dozer.mapClass(iUsersService.findById(userId.get(),UsersViewModel.class);
}
}
删除setAllowedOriginates方法,然后重试。设置和空列表可能是您正在禁止服务器之外的所有请求请将此更改为config.setAllowedHeaders(Arrays.asList(“Origin”、“Content Type”、“Accept”)代码>
到
config.setAllowedHeader(Arrays.asList(“*”)代码>检查是否有帮助。我的代码如下所示。对于localhost是正确的,但实际上是错误的。您使用的是Spring Security吗?在这种情况下,请在问题中包括您的Spring Security配置否我不使用它请指定您使用的Spring启动版本?另外,在您的问题中,您没有使用Spring Boot,因为有不同的方法来设置CORS。我这样做是因为错误:CORS标头“Access Control Allow Origin”丢失在localhost窗体angular之外调用api时,angular配置了正确的url而不是localhost?这是什么?请解释。我问Angular服务的url(主机和端口)是否正确。如果在部署webapp时是localhost并在主机外部执行angular service,则可能是因为出现连接被拒绝错误。
@RestController
@RequestMapping("/users")
public class UsersController {
@Autowired
private IUsersService iUsersService;
@GetMapping("/list/grid")
public Iterable<UsersViewModel> getAllEmployees() {
return Dozer.mapList(iUsersService.getAll(), UsersViewModel.class);
}
@GetMapping("/load/{id}")
public UsersViewModel getUserById(@PathVariable(value = "id") Long userId){
return Dozer.mapClass(iUsersService.findById(userId).get(),UsersViewModel.class);
}
}