@Path("/account")
public class AccountController {
//*************************************Login******************************************
@GET
@Path("/login")
@Produces(MediaType.APPLICATION_JSON+ ";charset=UTF-8")
public Response login(@QueryParam("userName")String userName,@QueryParam("password")String password,@QueryParam("verificationCode")String verificationCode){
if(StringUtils.isBlank(userName)){
return Response.paramErrorResponse("请输入用户名!");
}
if(StringUtils.isBlank(password)){
return Response.paramErrorResponse("请输入登陆密码!");
}
if(StringUtils.isBlank(verificationCode)){
return Response.paramErrorResponse("请输入验证码");
}
String ip = cxfUtil.getClientIpCxfRest();
//1.验证验证码
//2.验证用户名与密码
try {
loginManager.login(userName, password,ip);
} catch (Exception e) {
return Response.errorResponse(e.getMessage());
}
return Response.successResponse(null);
}
@POST
@Path("/accountisexist")
@Produces(MediaType.APPLICATION_JSON+ ";charset=UTF-8")
public Response accountIsExist(@QueryParam("userName")String userName){
if(StringUtils.isBlank(userName)){
return Response.paramErrorResponse("请输入用户名!");
}
if(commonManager.isExistUserName(userName)){
return Response.successResponse(true);
}else{
return Response.successResponse(false);
}
}
//*************************************************register******************************
@POST
@Path("/register")
@Produces(MediaType.APPLICATION_JSON+ ";charset=UTF-8")
public Response register(@FormParam("userName") String userName,@FormParam("password")String password,@FormParam("mobile")String mobile,@FormParam("verificationCode")String verificationCode){
if(StringUtils.isBlank(userName)){
return Response.paramErrorResponse("请输入用户名!");
}
if(StringUtils.isBlank(password)){
return Response.paramErrorResponse("请输入登陆密码!");
}
if(StringUtils.isBlank(mobile)){
return Response.paramErrorResponse("请输入手机号!");
}
if(StringUtils.isBlank(verificationCode)){
return Response.paramErrorResponse("请输入验证码");
}
String ip = cxfUtil.getClientIpCxfRest();
//1.验证手机验证码
//2.注册用户
try {
registerManager.register(userName, password, 1, mobile, ip);
} catch (Exception e) {
e.printStackTrace();
return Response.errorResponse(e.getMessage());
}
return Response.successResponse(null);
}
}
junit
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest
public class AccountControllerTest {
public final String REST_SERVICE_URL = "http://localhost:8080/p2p/data/ws/rest";
private static final String TYPE_XML = "application/xml";
private static final String TYPE_JSON = "application/json";
@Test
public void testLogin() {
//(String userName,String password,String verificationCode)
WebClient client = WebClient.create(REST_SERVICE_URL);
client.path("/account/login","123456").accept(
TYPE_JSON).type(TYPE_JSON).query("userName", "test").query("password", "12345678").query("verificationCode", "654286315");
String response = client.get(String.class);
System.out.println(response);
}
static Client client = null;
@BeforeClass
public static void init() {
//register(JacksonFeatures.class).
client = ClientBuilder.newBuilder().build();
}
@Test
public void test(){
WebTarget target = client.target(REST_SERVICE_URL).path("/account/login").queryParam ("userName", "test").queryParam("password", "123456789").queryParam("verificationCode", "654286315");
String response = target.request(TYPE_JSON).get(String.class);
System.out.println(response);
}
@Test
public void testAccountIsExist() {
WebTarget target = client.target(REST_SERVICE_URL).path("/account/accountisexist").queryParam ("userName", "testfsd");
String response = target.request(TYPE_JSON).post(null,String.class);
System.out.println(response);
}
@Test
public void testRegister() {
Form f = new Form();
f.param("userName", "adminnn");
f.param("password", "123456789");
f.param("mobile", "13916145180");
f.param("verificationCode", "12345");
WebTarget target = client.target(REST_SERVICE_URL).path("/account/register");
String response = target.request(TYPE_JSON).post(Entity.form(f),String.class);
System.out.println(response);
}
}
public class Response {
private String status;
private String msg;
private Object data;
public Response(String status, String msg, Object data) {
super();
this.status = status;
this.msg = msg;
this.data = data;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
/**
* 参数错误
* **/
public static Response paramErrorResponse(String errorMessage){
return new Response(ConstantsUtil.CommonCode.PARAM_ERROR_CODE,errorMessage,null);
}
public static Response paramErrorResponse(){
return new Response(ConstantsUtil.CommonCode.PARAM_ERROR_CODE,ConstantsUtil.CommonMessage.PARAM_ERROR_MESSAGE,null);
}
/**
* 获取数据错误
* **/
public static Response errorResponse(String errorMessage){
return new Response(ConstantsUtil.CommonCode.FAILED_CODE,errorMessage,null);
}
public static Response errorResponse(){
return new Response(ConstantsUtil.CommonCode.FAILED_CODE,ConstantsUtil.CommonMessage.FAILED_MESSAGE,null);
}
/**
* 获取数据成功
* **/
public static Response successResponse(Object data){
return new Response(ConstantsUtil.CommonCode.SUCCESS_CODE,ConstantsUtil.CommonMessage.SUCCESS_MESSAGE,data);
}
public static Response successResponse(){
return new Response(ConstantsUtil.CommonCode.SUCCESS_CODE,ConstantsUtil.CommonMessage.SUCCESS_MESSAGE,null);
}
/**
* 用户未登录
* **/
public static Response noLoginResponse(){
return new Response(ConstantsUtil.CommonCode.NO_LOGIN,ConstantsUtil.CommonMessage.NO_LOGIN_MESSAGE,null);
}
}
===================================================================================
@Path("/capital")
public class CapitalController {
@Autowired
private CapitalManager capitalManager;
/**
* 检索获得当前用户的资金账户
*
* @param userid
* @return
*/
@POST
@Path("/queryCapitalInfo")
@Produces(MediaType.APPLICATION_JSON + ";charset=UTF-8")
@Consumes(MediaType.APPLICATION_JSON)
public List<ThirdPaymentAccount> queryCapitalInfo(Account acc) {
List<ThirdPaymentAccount> result = capitalManager.queryCapitalInfo(acc);
return result;
}
}
JUnit
package com.vcredit.jdev.p2p.capital.modal;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import com.vcredit.jdev.p2p.Application;
import com.vcredit.jdev.p2p.entity.Account;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest
public class CapitalControllerTest {
public final String REST_SERVICE_URL = "http://localhost:8080/p2p/data/ws/rest";
static Client client = null;
// @Autowired
// private CapitalController c;
@BeforeClass
public static void init() {
//register(JacksonFeatures.class).
client = ClientBuilder.newClient();
client.register(JacksonJsonProvider.class);
}
@Test
public void test() {
String path = "/capital/queryCapitalInfo";
WebTarget target = client.target(REST_SERVICE_URL).path(path);
Account c = new Account();
c.setAccountSequence(1l);
Response response = target.request(MediaType.APPLICATION_JSON).post(Entity.json(c));
int statusCode = response.getStatus();
String content = response.readEntity(String.class);
System.out.println("@" + content);
System.out.println("retrieveFile1() statusCode:" + statusCode);
assert (statusCode == 200);
}
}
======================================================
@BeforeClass
public static void init() {
//register(JacksonFeatures.class).
client = ClientBuilder.newBuilder().build();
client.register(JacksonJsonProvider.class);
}
@Before
public void setUp() throws Exception {
}
@Test
public void testLogin(){
WebTarget target = client.target(REST_SERVICE_URL).path("/account/login");
Map body = new HashMap();
body.put("userName", "admin");
body.put("password", "123456789");
body.put("verificationCode", "test");
String response = target.request(TYPE_JSON).post(Entity.json(body),String.class);
System.out.println(response);
}
/**
* {
"userName":"admin",
"password":"123456789",
"verificationCode":"123456789"
}
*
*
* */
@POST
@Path("/login")
public Response login(Map<String, Object> paramMap){
String userName = (String) paramMap.get("userName");
String password = (String) paramMap.get("password");
String verificationCode = (String) paramMap.get("verificationCode");
if(StringUtils.isBlank(userName)){
return Response.paramErrorResponse("请输入用户名!");
}
if(StringUtils.isBlank(password)){
return Response.paramErrorResponse("请输入登陆密码!");
}
if(StringUtils.isBlank(verificationCode)){
return Response.paramErrorResponse("请输入验证码");
}
String ip = cxfUtil.getClientIpCxfRest();
//1.验证验证码
//2.验证用户名与密码
try {
loginManager.login(userName, password,ip);
} catch (Exception e) {
return Response.errorResponse(e.getMessage());
}
return Response.successResponse();
}