gpt4 book ai didi

java - Spring MVC 和 AngularJS @RequestMapping

转载 作者:行者123 更新时间:2023-11-30 08:14:08 25 4
gpt4 key购买 nike

我已经使用 Spring-boot 和 AngularJS 以及 REST 端点应用程序构建了一个应用程序。在我制作的 Spring Controller 中,我对 @RequesMapping 有点卡住了。问题是,我有示例网址:

"localhost:8080/foo/bar/api/cardGenerated/0102". 

'01'是第一个参数,'02'是第二个参数。我如何映射到 @RequestMapping Spring Controller 以获取上面的 url。

这是我的 Controller :

@RestController
@RequestMapping("/api")
public class CardGeneratedResource {
@RequestMapping(value = "/cardGenerated/{branchCode}{cardType}",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<CardGenerated> get(@PathVariable("branchCode") String branchCode,
@PathVariable("cardType") String cardType,
HttpServletResponse response) {

log.debug("REST request to get CardGenerated : " + branchCode + " and " + cardType);

CardGenerated cardGenerated = cardGeneratedRepository.
findTopByBranchCodeAndCardTypeOrderByCardNumberDesc(branchCode, cardType);

if (cardGenerated == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

return new ResponseEntity<>(cardGenerated, HttpStatus.OK);
}
}

所以这是我的 AngularJS $resource:

'use strict';

angular.module('itmApp')
.factory('CardGenerated', function ($resource) {
return $resource('api/cardGenerated/:branchCode:cardType', {}, {
'query': { method: 'GET', isArray: true},
'get': {
method: 'GET',
transformResponse: function (data) {
data = angular.fromJson(data);
return data;
}
}
});
});

我总是收到 “无法加载资源:服务器响应状态为 404(未找到)”

最佳答案

这里你缺少 /

这里有两个路径变量。所以默认 url 是

localhost:8080/foo/bar/api/cardGenerated/FIRST_PATH_VARIABLE/SECOND_PATH_VARIABLE
  1. branchCode(第一个路径变量)
  2. cardType(第二个路径变量)

    @RequestMapping(value = "/cardGenerated/{branchCode}/{cardType}"

并且在前端注册工厂定义时也出现了同样的错误。

api/cardGenerated/:branchCode/:cardType'

所有的方法都是这样

@RestController
@RequestMapping("/api")
public class CardGeneratedResource {
@RequestMapping(value = "/cardGenerated/{branchCode}/{cardType}",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<CardGenerated> get(@PathVariable("branchCode") String branchCode,
@PathVariable("cardType") String cardType,
HttpServletResponse response) {

log.debug("REST request to get CardGenerated : " + branchCode + " and " + cardType);

CardGenerated cardGenerated = cardGeneratedRepository.
findTopByBranchCodeAndCardTypeOrderByCardNumberDesc(branchCode, cardType);

if (cardGenerated == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

return new ResponseEntity<>(cardGenerated, HttpStatus.OK);
}
}

angular factory 就像

'use strict';

angular.module('itmApp')
.factory('CardGenerated', function ($resource) {
return $resource('api/cardGenerated/:branchCode/:cardType', {}, {
'query': { method: 'GET', isArray: true},
'get': {
method: 'GET',
transformResponse: function (data) {
data = angular.fromJson(data);
return data;
}
}
});
});

注意:首先尝试使用任何 rest 客户端或 postman ,并确保后端 api 正常工作,并且正确传递 Angular 侧检查参数。

关于java - Spring MVC 和 AngularJS @RequestMapping,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29666123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com