【问题标题】:Spring MVC and AngularJS @RequestMappingSpring MVC 和 AngularJS @RequestMapping
【发布时间】:2015-06-22 08:27:15
【问题描述】:

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

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

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

这是我的控制器:

@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)'

【问题讨论】:

    标签: java angularjs spring spring-mvc angular-resource


    【解决方案1】:

    你在这里错过了/

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

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

    【讨论】:

    • @Kahfi Maulana 欢迎,这是我的荣幸。如果它解决了您的问题,请接受并支持答案。
    猜你喜欢
    • 2011-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-23
    • 1970-01-01
    • 1970-01-01
    • 2013-05-13
    相关资源
    最近更新 更多