Springboot + post Request Interface

This article is Springboot + post request generation interface, and includes the cookie with the transmission parameters post request. Lombok new framework, the use of lombok can not add member variables in a class of get / set methods. Framework comes with an associated method. Such as you do not need to add the following

   public String getUsename() {
return usename;
}

public void setUsename(String usename) {
this.usename = usename;
}

 

 

 

 

 

 

<dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.14</version>
        </dependency>
    </dependencies>

 

java post request code as follows

 
 
com.course.server Package; 

Import com.course.bean.User;
Import io.swagger.annotations.Api;
Import io.swagger.annotations.ApiOperation;
Import jdk.internal.org.objectweb.asm.tree.analysis.Value ;
Import org.springframework.web.bind.annotation *;.

Import a javax.servlet.http.Cookie;
Import the javax.servlet.http.HttpServletRequest;
Import javax.servlet.http.HttpServletResponse;

@RestController
@Api (value = "/ ", description =" this is all my post, please ")
@ RequestMapping (" / V1 ")
public class MyPostMethod {
// this variable is used to hold our cookies information
Private static Cookie the cookie;
// simulation scenarios: user login is successful get to the cookies, then get access to the list of other interfaces

/ **
* simulate user login scenarios
@Param the Response *
* @param userName
* @param password
* @return
* /
@ RequestMapping (value = "/ the Login", Method, = RequestMethod.POST)
@ApiOperation (value = "login interface after a successful login get cookies", httpMethod = "POST")
public String the Login (HttpServletResponse the Response,
@RequestParam (value = "username", required = to true) String userName,
// @RequestParam (value = "username1", required = to true) String userName,
// @RequestParam the parameter format is passed param format
// username1 and the same parameters as the front end of the transfer,
// the userName String, can be inconsistent, and the front end
@RequestParam (value = "password", required = to true) String password) {
IF (userName.equals ( "zhangshan") && password.equals ( "111111")) {
the cookie = new new Cookie ( "the Login", "to true" );
response.addCookie (cookie);
return "Congratulations on your successful login";
} the else
return "user name or password is incorrect";

}

/ **
* Specifies the user to carry the cookie obtain a list of user information
* @param Request
* @param the user
@return *
* /
@ RequestMapping (value = "/ getUserList", Method, = RequestMethod.POST)
@ApiOperation (value = "get a list of users", httpMethod = "POST")
public String getUserList(HttpServletRequest request, @RequestBody User user) {
// must be written HttpServletRequest reques, cookie or not the value of the parameter format tape cookie values should need to pass @RequestBody json format

// Get Cookies
the User new new user1 = the User ();
cookies [] = Cookies request.getCookies (); // get the cookie value
for (cookie cookie: Cookies) {
. IF (the Cookie.getName () .equals ( "the Login") && cookie.getValue () the equals ( "to true") &&
. user.getName () the equals ( " zhangshan ") && user.getPassword () the equals (." 111111 ")) {

user1.setUsename (" Lisi ");
user1.setAge (" 18 is ");

return user1.toString ();
}

}

return" invalid parameters ";

}

}
 

User class (user information returned in the list)

 
 
com.course.bean Package Penalty for; 
Import lombok.Data;
@Data
public class {the User
Private String usename;
Private String password;
Private String name;
Private String Age;
// following because of the introduction lombox framework without the need to add member variables the get / set methods.
Public String getUsename // () {
// return usename;
//}
//
// public void setUsename (String usename) {
// this.usename = usename;
//}
//
// public String the getPassword () {
/ / password return;
//}
//
// public void the setPassword (String password) {
// = this.password password;
//}
//
// public String getName() {
// return name;
// }
//
// public void setName(String name) {
// this.name = name;
// }
//
// public String getAge() {
// return age;
// }
//
// public void setAge(String age) {
// this.age = age;
// }
}
 

 

main entrance

package com.example.demo;

        import org.springframework.boot.SpringApplication;
        import org.springframework.boot.autoconfigure.SpringBootApplication;
        import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan("com.course")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

 

Guess you like

Origin www.cnblogs.com/linxinmeng/p/12629859.html