Angular2服务无法从Spring Controller获取数据

时间:2017-08-10 09:13:41

标签: angular spring-boot angular2-services

我是角色的新手,我正在开展一个涉及angular2和Springboot的项目。

我的问题是:

  1. 我在springboot中创建了一个REST控制器。
  2. 在Angular中,我已经在我的组件中注入了一项服务并实施了 在ngOnInit内调用服务方法。
  3. 一切正常,预期的召唤来自棱角分明 前缀为springBoot的REST CONTROLLER。
  4. 但问题是,我在Angular中未定义。我没有得到任何数据 在角度。
  5. 我可以在Eclipse中看到REST控制器返回数据,但我没有用角度来获取它。
  6. 即使我从浏览器中删除了网址,我也可以在浏览器上获取JSON。
  7. 我已经确认了JSON,并且它是正确的。
  8. 以下是我的角色服务:

    import { Component , ViewContainerRef ,ViewEncapsulation} from '@angular/core';
    import { OnInit } from '@angular/core' ;
    import { ProfileService } from './profile.service' ;
    import { Observable } from 'rxjs/Observable';
    import { Profile } from './profile';
    
    import {FormsModule} from '@angular/forms' ;
    import { Overlay ,overlayConfigFactory } from 'angular2-modal' ;
    import { Modal , BSModalContext } from 'angular2-modal/plugins/bootstrap';
    import {CustomModalContext , ProfileCreate } from './profile-create.component' ;
    
    @Component({
        selector: 'app-root',
        templateUrl: './app.component.html',
        styleUrls: ['./app.component.scss'],
        providers: [ProfileService , Modal]  
    })
    export class AppComponent implements OnInit{
        title = 'Permission Matrix';
    
    constructor(private _profileService : ProfileService ,  public modal : Modal){}
    profiles: Profile[]; 
    
    ngOnInit(){
        console.log("inside component ");
        this.getHeroes();
    }
    
    getHeroes(): void {
        this._profileService
            .getProfiles()
            .then(prfiles => {
              this.profiles = prfiles;
              console.log(this.profiles);
            });
    }
    
    getCreateUpdateScreen(){
        return this.modal.open(ProfileCreate, overlayConfigFactory({num1 : 2, num2 : 3 }, BSModalContext));
    }
    

    下面是我的角色成分:

    export class Profile {
        profil_id : string ;
        profil_name_lang: string;
        profil_name_short: string;
        geschaeftsbereich: string;
        spezialberatung: string;
    }
    

    下面是我在angular中的映射类:

    package permissionbackendservice.permissionmatrix.controller;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    import permissionbackendservice.permissionmatrix.domain.Profile;
    import permissionbackendservice.permissionmatrix.dto.ProfileDto;
    import permissionbackendservice.permissionmatrix.service.ProfileService;
    
    @RestController
    public class ProfileController {
    
        private final ProfileService profileservice;            
    
        @Autowired
        ProfileController(ProfileService profileservice) {
            this.profileservice = profileservice;
        }
    
        @RequestMapping("/profiles")
        public List<Profile> readprofiles(){
            // This Function will fetch all the profiles in the database
            List<Profile> profileList = null ;                       
            try {
                profileList = profileservice.readProfiles();
            } catch(Exception e) {
                e.printStackTrace();
            }
    
            return profileList ; 
        }
    
        // function to delete an existing profiles
        @RequestMapping(method = RequestMethod.POST ,value = "/deleteprofile" )
        public void deleteProfile(@RequestBody List<Profile> profile){                    
            try {
                profileservice.deleteProfile(profile);
            } catch(Exception e) {
                e.printStackTrace();
            }                       
        }
    
        @RequestMapping(method = RequestMethod.POST ,value = "/saveprofile")
        public void addProfile(@RequestBody Profile profile){ // this function is for creating new profile
            profileservice.addProfile(profile);
        }
    
        @RequestMapping(method = RequestMethod.POST ,value = "/updateprofile")
        public void updateProfile(@RequestBody Profile profile){  // this function is for updating new profile
            profileservice.updateProfile(profile);
        }
    
        /*
        @RequestMapping(method = RequestMethod.POST, value = "/saveprofile") // function to both add and update profiles in database 
        public void saveProfile(@RequestParam(value = "profilNameLang") String profileNameLang, @RequestParam(value = "profilNameShort") String  profileNameShort , @RequestParam(value = "geschaeftsbereich") String geschaeftsbereich, @RequestParam(value = "profileId") String  profileId) {
            // In this function control will handled in two ways .If the profileId is null then it means its an addition of new profile as in this case profile id
            // will be generated in code and not as an user input ,otherwise if the profile id is not null then its a case  of update .
            if(profileId == null || profileId.isEmpty()){
                                                   profileservice.addProfile(profileNameLang, geschaeftsbereich, profileNameShort);
            } else {
                profileservice.updateProfile(profileNameLang, geschaeftsbereich, profileNameShort, profileId);
            }                       
        }            
        */          
    }
    

    下面是我的弹簧控制器:

    <a href="data:application/json; YOUR JSON DATA HERE" download="yourfilename.json">Save</a>
    

0 个答案:

没有答案