繁体   English   中英

Spring MVC和AngularJS @RequestMapping

[英]Spring MVC and AngularJS @RequestMapping

我已经使用REST End Point应用程序使用Spring-boot和AngularJS构建了一个应用程序。 我在自己制作的Spring Controller中对@RequesMapping感到有些@RequesMapping 问题是,我有示例网址:

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

“ 01”是第一个参数,“ 02”是第二个参数。 我如何映射到@RequestMapping Spring控制器以获取上面的网址。

这是我的控制器:

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

我总是收到'Failed to load resource: the server responded with a status of 404 (Not Found)'

在这里你缺少/。

这里有两个路径变量。所以默认的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);
    }
}

棱角工厂就像

'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;
                }
            }
        });
    });

注意:首先尝试与任何其他客户端或邮递员联系,并确保后端api正常运行,并且正确传递了角度侧检查参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM