客户端发送的请求在语法上不正确。通过curl在命令行中发布数据时出现错误

时间:2018-08-27 16:51:51

标签: java spring-mvc curl

当我尝试通过curl在命令行中发布数据时,它显示以下错误。我尝试与邮递员,数据发布成功。预先感谢

我的控制器

@Controller
@RequestMapping(value = "registration")
public class RegistrationController {

    public RegistrationController(){

    }

    @Autowired
    private RegistrationService registrationService;

    @PostMapping("/create")
   // @RequestMapping(value = "/create", method = RequestMethod.POST, consumes="application/json")
    public @Async ResponseEntity<String> create(@RequestBody Registration registration) {
        try {
            // registrationService.create(registration);
            if (registration == null) {
                return new ResponseEntity("Registration failed.", HttpStatus.BAD_REQUEST);
            } else {
                registrationService.create(registration);
                return new ResponseEntity(HttpStatus.CREATED);
            }

        } catch (Exception e) {
            return new ResponseEntity(e.getMessage(), HttpStatus.BAD_REQUEST);

        }
    }

    @PutMapping("/update")
    public ResponseEntity<String> update(@RequestBody Registration registration) {
        try {
            // registrationService.create(registration);
            if (registration == null) {
                return new ResponseEntity("Registration update failed.", HttpStatus.BAD_REQUEST);
            } else {
                registrationService.update(registration);
                return new ResponseEntity(HttpStatus.CREATED);
            }

        } catch (Exception e) {
            return new ResponseEntity(e.getMessage(), HttpStatus.BAD_REQUEST);

        }
    }

    @GetMapping("/getById/{id}")
    public ResponseEntity<Registration> GetById(@PathVariable int id) {
        try {
            // registrationService.create(registration);
            if (id == 0) {
                return new ResponseEntity("Failed to retrive value.", HttpStatus.BAD_REQUEST);
            } else {
                Registration res = registrationService.getById(id);
                return new ResponseEntity(res, HttpStatus.OK);
            }

        } catch (Exception e) {
            return new ResponseEntity(e.getMessage(), HttpStatus.BAD_REQUEST);

        }
    }

    @DeleteMapping("/delete/{id}")
    public ResponseEntity<String> delete(@PathVariable int id) {
        try {
            // registrationService.create(registration);
            if (id == 0) {
                return new ResponseEntity("Failed to delete.", HttpStatus.BAD_REQUEST);
            } else {
                boolean res = registrationService.delete(id);
                if (res) {
                    return new ResponseEntity(HttpStatus.OK);
                }else{
                    return new ResponseEntity("failed to delete.", HttpStatus.BAD_REQUEST);
                }
            }

        } catch (Exception e) {
            return new ResponseEntity(e.getMessage(), HttpStatus.BAD_REQUEST);

        }
    }

    @GetMapping("/getAll")
    public ResponseEntity<List<Registration>> getAll() {
        try {
            // registrationService.create(registration);

            List<Registration> res = registrationService.getAll();
            return new ResponseEntity(res, HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity(e.getMessage(), HttpStatus.BAD_REQUEST);

        }
    }

}

当我尝试通过curl在命令行中发布数据时,它显示了以下错误:

C:\>curl -v POST -H "Content-Type: application/json" -d 
'{
"firstName":"David",
"lastName":"Smith",
"email":"david@gmail.com",
"address":"kathmandu",
"phone":"9876544434",
"username":"david",
"password":"davidm@123"
}' 

http://localhost:8080/LMS/registration/create

我点击后输入以下命令的消息:

* Rebuilt URL to: POST/

* Could not resolve host: POST

* Closing connection 0

curl: (6) Could not resolve host: POST
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#1)

> POST /LMS/registration/create HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.55.1
> Accept: */*
> Content-Type: application/json
> Content-Length: 122
>
* upload completely sent off: 122 out of 122 bytes

< HTTP/1.1 400 Bad Request

< Server: GlassFish Server Open Source Edition  4.1.1

< X-Powered-By: Servlet/3.1 JSP/2.3 (GlassFish Server Open Source Edition  4.1.1  Java/Oracle Corporation/1.8)

< Access-Control-Allow-Origin: *

< Access-Control-Allow-Credentials: true

< Access-Control-Allow-Methods: ACL, CANCELUPLOAD, CHECKIN, CHECKOUT, COPY, DELETE, GET, HEAD, LOCK, MKCALENDAR, MKCOL, MOVE, OPTIONS, POST, PROPFIND, PROPPATCH, PUT, REPORT, SEARCH, UNCHECKOUT, UNLOCK, UPDATE, VERSION-CONTROL

< Access-Control-Max-Age: 3600

< Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Key, Authorization

< Content-Language:

< Content-Type: text/html

< Date: Mon, 27 Aug 2018 16:31:04 GMT

< Connection: close

< Content-Length: 1109`
<
> 
>        ` <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
>         <html xmlns="http://www.w3.org/1999/xhtml">
>         <head>
>         <title>GlassFish Server Open Source Edition  4.1.1  - Error report</title>
>         <style type="text/css">
>     <!--
>     H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;}
> 
>     H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;}
> 
>     H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;}
> 
>     BODY 
>     {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;}
> 
>     B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} 
>     P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}
>     A {color : black;}
>     HR {color : #525D76;}--></style> 
>     </head>
>     <body>
>     <h1>HTTP Status 400 - Bad Request</h1>
>     <hr/>
>     <p>
>     <b>type</b> 
>     Status report
>     </p>
>     <p>
>     <b>message</b>
>     Bad Request</p>
>     <p><b>description</b>
>     The request sent by the client was syntactically incorrect.</p>
>     <hr/>
>     <h3>GlassFish Server Open Source Edition  4.1.1 </h3>
>     </body>
>     </html>* 
>     Closing connection 1

这是我的注册对象

@Entity
@Table(name = "registration")
@NamedQueries({
    @NamedQuery(name = "Registration.findAll", query = "SELECT r FROM Registration r")})
public class Registration implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;
    @NotNull
    @Size(min = 1, max = 50)
    @Column(name = "first_name")
    private String firstName;
    @NotNull
    @Size(min = 1, max = 50)
    @Column(name = "last_name")
    private String lastName;
    // @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="Invalid email")//if the field contains email address consider using this annotation to enforce field validation
    @NotNull
    @Size(min = 1, max = 100)
    @Column(name = "email")
    private String email;
    @NotNull
    @Size(min = 1, max = 100)
    @Column(name = "address")
    private String address;
    // @Pattern(regexp="^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$", message="Invalid phone/fax format, should be as xxx-xxx-xxxx")//if the field contains phone or fax number consider using this annotation to enforce field validation
    @NotNull
    @Size(min = 1, max = 15)
    @Column(name = "phone")
    private String phone;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 20)
    @Column(name = "username")
    private String username;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 20)
    @Column(name = "password")
    private String password;

    public Registration() {
    }

    public Registration(Integer id) {
        this.id = id;
    }

    public Registration(Integer id, String firstName, String lastName, String email, String address, String phone, String username, String password) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.address = address;
        this.phone = phone;
        this.username = username;
        this.password = password;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Registration)) {
            return false;
        }
        Registration other = (Registration) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.gaurav.lms.entity.Registration[ id=" + id + " ]";
    }

}

0 个答案:

没有答案