该文档已移动调用Rest API方法

时间:2018-05-30 10:53:59

标签: rest spring-mvc spring-boot curl restful-architecture

我有一个基本的SpringBoot应用程序。使用Spring Initializer,JPA,嵌入式Tomcat,Thymeleaf模板引擎和包作为可执行的JAR文件。 我已经定义了这个Rest方法来更新用户

@PutMapping(path = "/api/users/{id}", 
                        consumes = "application/json", 
                        produces = "application/json")
    public ResponseEntity<User> updateUser
                                    (HttpServletRequest request, 
                                    @PathVariable long id,
                                    User user) {

        System.out.println(user);
        saveUser (user)
        return ResponseEntity.ok(user);

    }

我使用

从控制台调用此方法
curl -X PUT -H "Content-Type: application/json" -H "Authorization: Bearer eyJhbGciOiJIazUxMiJ9.eyJzdWIiOiJyaWNhcmQub2xsZUBnbWFpbC5jb20iLCJleHAiOjE1MjgxMTM3NTIsImlhdCI6MTUyNzUwODk1Mn0.QdxabtU1U87pYvyTstT1EG3E6uVpLo2mXCF0FF8iD6acKoAXKl_A0-eV_GrpOFg5FF1qR6B7llI5_USJL85YTQ" -d {"id":1,"username":"pere.peris@gmail.com","email":"pere.peris@gmail.com","firstName”:”Pere”,”lastName”:”PERIS”,”country”:”CAT”,”enabled":true} "http://127.0.0.1:2233/elcor/api/users/1"

但我在控制台看到的内容真的很奇怪

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="https://www.google.com/gmail/">here</A>.
</BODY></HTML>
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="https://www.google.com/gmail/">here</A>.
</BODY></HTML>
curl: (3) Port number ended with 'R'
curl: (3) Port number ended with 'P'
curl: (3) Port number ended with 'E'
curl: (3) Port number ended with 'i'
curl: (3) Port number ended with 'a'
curl: (3) Port number ended with 'm'
curl: (3) Port number ended with 'm'
curl: (3) Port number ended with 'c'
curl: (3) Port number ended with 't'
curl: (3) Port number ended with 't'
curl: (3) Port number ended with 't'
curl: (3) Port number ended with 't'
curl: (3) [globbing] bad range specification in column 14
curl: (3) Port number ended with 't'
{"id":1,...."enabled":false}

2 个答案:

答案 0 :(得分:1)

你必须转义你提供给-d参数的正文内容并用引号括起来,如下所示:

-d "{\"id\":1,\"username\":\"pere.peris@gmail.com\",\"email\":\"pere.peris@gmail.com\",\"firstName\":\"Pere\",\"lastName\":\"PERIS\",\"country\":\"CAT\",\"enabled\":true}"

否则引号之外的所有内容都将被视为shell的命令。

答案 1 :(得分:0)

网址存在问题。尝试在记录器中打印请求URL,或从代码中打印到控制台。我确定它不符合您的卷曲要求。