代码之家  ›  专栏  ›  技术社区  ›  newdev

在Spring引导应用程序中找不到AngularJS$HTTP路径404

  •  0
  • newdev  · 技术社区  · 6 年前

    我在查找$http请求中使用的正确路径时遇到问题,无法访问我的Spring引导应用程序中的@restcontroller。我将在下面包含相关代码,希望您能看到一些我不知道的东西。很抱歉,代码不是完全用英语编写的,但我认为它很容易理解。

    以下是我支持的目标后端REST控制器:

    @RestController
    @RequestMapping("/fileData")
    public class FileDataService {
    
        @Autowired
        HttpServletRequest request;
        @Autowired
        ServletContext ctx;
    
        @RequestMapping(method = RequestMethod.GET,
                value = "fileData/getKorisnici", 
                produces = MediaType.APPLICATION_JSON_VALUE)
        //@ResponseBody
        public Collection<Korisnik> getKorisnici() {
            return getFileData().getKorisnikValues();
        }
    
        /*@POST
        @Path("/addKorisnik")
        @Produces(MediaType.APPLICATION_JSON)
        @Consumes(MediaType.APPLICATION_JSON)*/
        @SuppressWarnings("rawtypes")
        @RequestMapping(method = RequestMethod.POST,
                value = "/addKorisnik", 
                produces = MediaType.APPLICATION_JSON_VALUE,
                consumes = MediaType.APPLICATION_JSON_VALUE)
        //@ResponseBody
        public ResponseEntity addKorisnik(Korisnik k) {
            if(getFileData().usernameExistsKorisnici(k.getKorisnickoIme())) {
                return ResponseEntity.status(HttpStatus.CONFLICT).build();
            }
            else if(getFileData().emailExistsKorisnici(k.getEmail())) {
                return ResponseEntity.status(HttpStatus.CONFLICT).build();
            }
            getFileData().getKorisnici().put(k.getIdKorisnik(), k);
            getFileData().writeData();
            return ResponseEntity.ok().build();
        }
    

    这里是前端:

    app.factory('korisnikFactory', function($http) {
    
        var factory = {};
        factory.getKorisnici = function() {
            //return $http.get('/drools-spring-v02-app/rest/fileData/getKorisnici');
            return $http.get('/fileData/getKorisnici');
        };
    
        factory.addKorisnik = function(korisnik) {
            return $http.post('/fileData/addKorisnik', korisnik);
        };
    
        return factory;
    
    });
    
    3 回复  |  直到 6 年前
        1
  •  1
  •   Nikhil Zurunge    6 年前

    在你的SpringBoot应用程序中有一个注释 @RequestMapping("/fileData") 在类上方定义 FileDataService ,因此每个请求调用的前缀都是 /fileData/.. 自动地。不用再写了,就像在你的电话里一样。

    @RequestMapping(method = RequestMethod.GET, value = "fileData/getKorisnici", produces = MediaType.APPLICATION_JSON_VALUE)

    在AngularJS中应该有绝对路径 return $http.get($location.absUrl() + 'fileData/getKorisnici'); (注意那些斜线)

    你可以参考我的一个关于SpringBoot+AngularJS的演示项目。 here

        2
  •  1
  •   Jayesh    6 年前

    我相信你的相对网址可能是错误的。首先,尝试获取SpringBoot应用程序的完整路径,并尝试使用restclient或postman查看SpringBoot应用程序对于get和post请求是否正常工作。在这一步之后,回到你的角度尝试$http.get([你的完整网址')如果成功,那么你将能够更新相对网址。

        3
  •  1
  •   Hema Pallav Jha    6 年前

    您在URL格式上犯了一个小错误,在您的类中,您已经声明 @RequestMapping("/fileData") 所以您的方法应该遵循前面的路径。 你不应该再包括 value = "fileData/getKorisnici" .

    试着把它改成 value = "/getKorisnici" 一切都会好起来的。

    你的前端没有什么可以改变的。