【问题标题】:AngularJS $http path 404 not found in Spring Boot App在 Spring Boot 应用程序中找不到 AngularJS $http 路径 404
【发布时间】:2018-08-20 23:50:29
【问题描述】:

我无法找到在 $http 请求中使用的正确路径,以便在我的 Spring Boot 应用程序中访问 @RestController。我将在下面包含相关代码,希望您能看到我没有看到的东西。我很抱歉代码不是全英文的,但我认为它很容易理解。

这是我应该定位的后端休息控制器:

@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;

});

【问题讨论】:

    标签: angularjs spring rest spring-boot


    【解决方案1】:

    在您的 springboot 应用程序中,在 FileDataService 类上方定义了一个注释 @RequestMapping("/fileData"),因此每个请求调用都会自动以 /fileData/.. 为前缀。无需像在 get 调用中那样再次编写它

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

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

    你可以参考我在 SpringBoot+AngularJS here987654321@上的一个Demo项目

    【讨论】:

      【解决方案2】:

      我相信您的相对网址在这里可能是错误的。首先,尝试获取 Spring Boot 应用程序的完整路径,并尝试使用 RestClient 或 Postman 来查看 Spring Boot 应用程序对于 GET 和 POST 请求是否正常工作。完成此步骤后,返回您的 Angular 尝试 $http.get([your-full-url') 如果可行,那么您将能够更新相对 url。

      【讨论】:

        【解决方案3】:

        您在 URL 形成方面犯了一个小错误,在您的课程中,您已声明 @RequestMapping("/fileData"),因此您的方法应遵循此之前的路径。 您不应再次包含value = "fileData/getKorisnici"

        只需尝试将其更正为 value = "/getKorisnici" 一切都会正常工作。

        你的前端没有任何改变。

        【讨论】:

          猜你喜欢
          • 2018-07-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-12-29
          • 1970-01-01
          • 2013-11-27
          相关资源
          最近更新 更多