Java 将RestAssured转换为RestTemplate
谁能帮我把下面的代码重构成SpringRestTemplateJava 将RestAssured转换为RestTemplate,java,api,resttemplate,rest-assured,Java,Api,Resttemplate,Rest Assured,谁能帮我把下面的代码重构成SpringRestTemplatepostLogin是一种稍后在junit e2e测试中使用的方法 public class LoginLogoutAPI { private static final LoginLogoutAPI INSTANCE = new LoginLogoutAPI(); private static final String LOGIN_ENDPOINT = "/auth/login"; public static
postLogin
是一种稍后在junit e2e测试中使用的方法
public class LoginLogoutAPI {
private static final LoginLogoutAPI INSTANCE = new LoginLogoutAPI();
private static final String LOGIN_ENDPOINT = "/auth/login";
public static LoginLogoutAPI getInstance() {
return INSTANCE;
}
public ValidatableResponse postLogin(String login, String password) {
return given()
.contentType(JSON)
.body(getCustomerCredentialsJson(login, password))
.when()
.post(LOGIN_ENDPOINT)
.then()
.statusCode(SC_OK);
}
private Map<String, String> getCustomerCredentialsJson(String login, String password) {
Map<String, String> customer = new LinkedHashMap<>();
customer.put("login", login);
customer.put("password", password);
return customer;
}
}
公共类LoginLogoutAPI{
private static final LoginLogoutAPI INSTANCE=new LoginLogoutAPI();
私有静态最终字符串LOGIN_ENDPOINT=“/auth/LOGIN”;
公共静态LoginLogoutAPI getInstance(){
返回实例;
}
公共ValidatableResponse postLogin(字符串登录,字符串密码){
返回给定值()
.contentType(JSON)
.body(getCustomerCredentialsJson(登录名、密码))
.when()
.post(登录\终端)
.然后()
.状态代码(SC_OK);
}
私有映射getCustomerCredentialsJson(字符串登录,字符串密码){
Map customer=newlinkedhashmap();
customer.put(“登录”,login);
客户输入(“密码”,密码);
退货客户;
}
}
假设这里的一切都正确,我将实现Rest模板交换
方法来进行post调用,并在ValidatableResponse
中捕获响应
public class LoginLogoutAPI {
private static final LoginLogoutAPI INSTANCE = new LoginLogoutAPI();
private static final String LOGIN_ENDPOINT = "/auth/login";
public static LoginLogoutAPI getInstance() {
return INSTANCE;
}
public ValidatableResponse postLogin(String login, String password) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<byte[]> httpEntity = new HttpEntity<byte[]>(headers);
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(LOGIN_ENDPOINT)
.queryParam("login",login)
.queryParam("password",password);
URI uri=builder.buildAndExpand().toUri();
ResponseEntity<ValidatableResponse> rs = restTemplate.exchange(uri, HttpMethod.POST, httpEntity,ValidatableResponse.class);
return rs.getBody();
}
}
公共类LoginLogoutAPI{
private static final LoginLogoutAPI INSTANCE=new LoginLogoutAPI();
私有静态最终字符串LOGIN_ENDPOINT=“/auth/LOGIN”;
公共静态LoginLogoutAPI getInstance(){
返回实例;
}
公共ValidatableResponse postLogin(字符串登录,字符串密码){
HttpHeaders=新的HttpHeaders();
setContentType(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity HttpEntity=新的HttpEntity(标题);
UriComponentsBuilder=UriComponentsBuilder.fromURI字符串(登录\端点)
.queryParam(“登录”,登录)
.queryParam(“密码”,password);
URI=builder.buildAndExpand().toUri();
ResponseEntity rs=restemplate.exchange(uri、HttpMethod.POST、httpEntity、ValidatableResponse.class);
返回rs.getBody();
}
}
这是一个实现,但不是一个工作示例,因为我没有工作区设置。您必须用rest模板的完整URL替换您的LOGIN\u端点
如果你需要澄清,请告诉我 你到底想重构什么?整个街区?是的,整个街区