Spring MVC和AngularJS @RequestMapping [英] Spring MVC and AngularJS @RequestMapping

查看:366
本文介绍了Spring MVC和AngularJS @RequestMapping的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经与弹簧引导和 AngularJS REST 终点应用程序的应用程序。我在春天控制器有点套牢 @RequesMapping 我做了。问题是,我有这个例子网址:

 本地主机:8080 /富/酒吧/ API / cardGenerated / 0102。

01是第一个参数,02是第二个参数。我怎样才能映射到 @RequestMapping 春季控制器获得上述的URL。

下面是我的控制器:

  @RestController
@RequestMapping(/ API)
公共类CardGeneratedResource {
@RequestMapping(值=/ cardGenerated / {分支code} {} cardType
            方法= RequestMethod.GET,
            产生= MediaType.APPLICATION_JSON_VALUE)
    @Timed
    公共ResponseEntity< CardGenerated>得到(@PathVariable(分支code)字符串分支code,
                                             @PathVariable(cardType)字符串cardType,
                                             HttpServletResponse的响应){        log.debug(REST请求获得CardGenerated:+分支code +和+ cardType);        CardGenerated cardGenerated = cardGeneratedRepository。
                findTopByBranch codeAndCardTypeOrderByCardNumberDesc(分支code,cardType);        如果(cardGenerated == NULL){
            返回新ResponseEntity<>(HttpStatus.NOT_FOUND);
        }        返回新ResponseEntity<>(cardGenerated,HttpStatus.OK);
    }
}

所以这是我AngularJS $资源

 使用严格的;angular.module('itmApp')
    .factory('CardGenerated',函数($资源){
        返回$资源(API / cardGenerated /:分支code:cardType',{},{
            '查询':{方法:GET,IsArray的:真正},
            '得到':{
                方法:GET,
                transformResponse:功能(数据){
                    数据= angular.fromJson(数据);
                    返回的数据;
                }
            }
        });
    });

我总是无法加载资源:服务器的状态为回应404(未找到)


解决方案

下面你缺少的 /

您有两个路径变量here.so默认URL是

 本地主机:8080 /富/酒吧/ API / cardGenerated / FIRST_PATH_VARIABLE / SECOND_PATH_VARIABLE


  1. 分支code(第一路径variabel)

  2. cardType(第二路径变量)

    @RequestMapping(值=/ cardGenerated / {分支code} / {} cardType


而在前端方太犯同样的错误,而登记工厂定义。

  API / cardGenerated /:分支code /:cardType

所有方法类似于

  @RestController
@RequestMapping(/ API)
公共类CardGeneratedResource {
@RequestMapping(值=/ cardGenerated / {分支code} / {} cardType
            方法= RequestMethod.GET,
            产生= MediaType.APPLICATION_JSON_VALUE)
    @Timed
    公共ResponseEntity< CardGenerated>得到(@PathVariable(分支code)字符串分支code,
                                             @PathVariable(cardType)字符串cardType,
                                             HttpServletResponse的响应){        log.debug(REST请求获得CardGenerated:+分支code +和+ cardType);        CardGenerated cardGenerated = cardGeneratedRepository。
                findTopByBranch codeAndCardTypeOrderByCardNumberDesc(分支code,cardType);        如果(cardGenerated == NULL){
            返回新ResponseEntity<>(HttpStatus.NOT_FOUND);
        }        返回新ResponseEntity<>(cardGenerated,HttpStatus.OK);
    }
}

和角度厂是像

 使用严格的;angular.module('itmApp')
    .factory('CardGenerated',函数($资源){
        返回$资源(API / cardGenerated /:分支code /:cardType',{},{
            '查询':{方法:GET,IsArray的:真正},
            '得到':{
                方法:GET,
                transformResponse:功能(数据){
                    数据= angular.fromJson(数据);
                    返回的数据;
                }
            }
        });
    });

注意:第一个与任何其他客户端或邮递员尝试,并确保后端API工作是否正常也是角边检查参数正确传递

I have built an application with Spring-boot and AngularJS with the REST End Point application. I got a little stuck with @RequesMapping in Spring Controller I've made. The problem is, I have the example url:

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

'01' is first parameter and '02' is second parameter. How can I mapped into @RequestMapping Spring controller to get a url above.

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

so this is my 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;
                }
            }
        });
    });

I always got 'Failed to load resource: the server responded with a status of 404 (Not Found)'.

解决方案

Here you are missing / .

You have two path variable here.so default url is

localhost:8080/foo/bar/api/cardGenerated/FIRST_PATH_VARIABLE/SECOND_PATH_VARIABLE

  1. branchCode (First path variabel)
  2. cardType (Second path variable)

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

And in frontend side too the same mistake while registering factory definition.

api/cardGenerated/:branchCode/:cardType'

All method is like

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

and angular factory is like

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

NOTE: First try with any rest client or postman and make sure backend api is working properly also angular side check parameters are being passed correctly.

这篇关于Spring MVC和AngularJS @RequestMapping的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆