0

当从 rest 方法返回时,我有这个特定的模型抛出“(原因:CORS 标头'Access-Control-Allow-Origin'丢失)。” 收到时。但是当我尝试返回其他模型时,它被成功接收。我已经把@CrossOrigin我的控制器。

这是我的模型/班级

@Entity
@Table(name = "tbl_project_config")
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY, region = "projectConfig")
public class ProjectConfg implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "projId")
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer dbServerId;

    @Column(name = "dept")
    private Integer dept;

    @Column(name = "name")
    private String name;

    @Column(name = "display_name")
    private String displayName;

    @Column(name = "description")
    private String description;

    @Column(name = "path")
    private String pth;

    @Column(name = "username")
    private String usename;

    @Column(name = "password")
    private String password;

    @Column(name = "application")
    private Integer application;

    @Column(name = "type")
    private String Type;

    @Column(name = "alive")
    private int alive;

    @OneToMany(targetEntity =ProjectHealth.class, fetch = FetchType.LAZY)
    @JoinColumn(name = "projId", updatable = false)
    private Set<ProjectHealth> projectHealthSet;

    public ProjectConfg() {}

   //getters setters


控制器


@CrossOrigin(origins = "http://localhost:4200", maxAge = 3600 )
@Controller
@RequestMapping(value = { "/Config/Project" }, produces = { "application/json" })
public class ProjectConfgController {

    @Autowired
    ProjectService projectService;

    @GetMapping(path="/{type}")
    @ResponseStatus(HttpStatus.OK)
    public @ResponseBody List<ProjectConfg> getList(@PathVariable String type) {

        List<ProjectConfg > resultList = projectService.getAll(type, 0);

        return  resultList;
    }


角服务

@Injectable()
export class ProjectService {
    private url: string;

    constructor(private http: HttpClient) {
        this.url= AppGlobalSetup.getHttps() + 'Config/Project/';
    }

    public get(type: string): Observable<any> {
        return this.http.get(this.url+ type);
    }


我有这个拦截器

@Injectable()
export class CustomInterceptor implements HttpInterceptor {

    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        if (!req.headers.has('Content-Type')) {
            req = req.clone({ headers: req.headers.set('Content-Type', 'application/json') });
        }
        req = req.clone({ headers: req.headers.set('Accept', 'application/json') });
        return next.handle(req); 
    }
}


整个设置适用于其他模型。我只有这个特定的模型,CORS在收到时会返回错误。请帮我解决CORCS问题。

4

1 回答 1

0

如果我的假设正确,您在控制器中错过了 @CrossOrigin(origins = " ", allowedHeaders = " ") 。请找到以下控制器格式

@CrossOrigin(origins = "*", allowedHeaders = "*")
@Controller
public class HomeController 
{
  @GetMapping(path="/")
  public String homeInit(Model model) {
  return "home";
  }
}
于 2020-03-25T10:54:00.940 回答