This commit is contained in:
Redkale
2017-05-12 21:30:18 +08:00
parent 83680c46e8
commit 225745a282
6 changed files with 164 additions and 11 deletions

View File

@@ -0,0 +1,26 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.redkale.test.rest;
import org.redkale.util.AsyncHandler;
/**
*
* @author zhangjx
*/
public class HelloAsyncHandler implements AsyncHandler {
@Override
public void completed(Object result, Object attachment) {
System.out.println("-----HelloAsyncHandler--------result : " + result + ", attachment: " + attachment);
}
@Override
public void failed(Throwable exc, Object attachment) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}

View File

@@ -1,5 +1,6 @@
package org.redkale.test.rest;
import java.util.Map;
import javax.persistence.Id;
import org.redkale.convert.json.JsonFactory;
import org.redkale.net.http.*;
@@ -28,9 +29,19 @@ public class HelloEntity {
@RestBody
private byte[] bodys;
@RestBody
private Map<String, String> bodymap;
@RestAddress
private String clientaddr;
public HelloEntity() {
}
public HelloEntity(int id) {
this.helloid = id;
}
/** 以下省略getter setter方法 */
public int getHelloid() {
return helloid;
@@ -104,6 +115,14 @@ public class HelloEntity {
this.bodys = bodys;
}
public Map<String, String> getBodymap() {
return bodymap;
}
public void setBodymap(Map<String, String> bodymap) {
this.bodymap = bodymap;
}
@Override
public String toString() {
return JsonFactory.root().getConvert().convertTo(this);

View File

@@ -34,10 +34,11 @@ public class HelloService implements Service {
}
//增加记录
public RetResult<HelloEntity> createHello(UserInfo info, HelloEntity entity) {
public RetResult<HelloEntity> createHello(UserInfo info, HelloEntity entity, @RestBody Map<String, String> body) {
System.out.println("增加记录----------------" + nodeid + ": body =" + body + ", entity =" + entity);
entity.setCreator(info == null ? 0 : info.getUserid()); //设置当前用户ID
entity.setCreatetime(System.currentTimeMillis());
source.insert(entity);
if (source != null) source.insert(entity);
return new RetResult<>(entity);
}
@@ -88,7 +89,24 @@ public class HelloService implements Service {
//异步查询单个
@RestMapping(name = "asyncfind")
public CompletableFuture<HelloEntity> asyncFindHello(@RestParam(name = "#") int id) { //通过 /pipes/hello/find/1234、/pipes/hello/jsfind/1234 查询对象
if (source != null) source.findAsync(HelloEntity.class, id);
if (source != null) source.findAsync(HelloEntity.class, id);
System.out.println("------------进入asyncfind1-------");
return CompletableFuture.completedFuture(new HelloEntity());
}
//异步查询单个
@RestMapping(name = "asyncfind2")
public void asyncFindHello(AsyncHandler hander, @RestParam(name = "#") int id) { //通过 /pipes/hello/find/1234、/pipes/hello/jsfind/1234 查询对象
if (source != null) source.findAsync(HelloEntity.class, id);
System.out.println("-----------进入asyncfind2--------" + hander);
hander.completed(new HelloEntity(id), id);
}
//异步查询单个
@RestMapping(name = "asyncfind3")
public void asyncFindHello(HelloAsyncHandler hander, @RestParam(name = "#") int id) { //通过 /pipes/hello/find/1234、/pipes/hello/jsfind/1234 查询对象
if (source != null) source.findAsync(HelloEntity.class, id);
System.out.println("-----------进入asyncfind3--------" + hander);
hander.completed(new HelloEntity(id), id);
}
}

View File

@@ -45,6 +45,16 @@ public class _DynHelloRestServlet1 extends SimpleRestServlet {
url = "http://127.0.0.1:" + port + "/pipes/hello/listmap?map={'a':5}";
System.out.println("listmap: " + Utility.postHttpContent(url, headers, null));
url = "http://127.0.0.1:" + port + "/pipes/hello/create?entity={}";
System.out.println("增加记录: " + Utility.postHttpContent(url, headers, "{'a':2,'b':3}"));
url = "http://127.0.0.1:" + port + "/pipes/hello/asyncfind/111111";
System.out.println("listmap: " + Utility.postHttpContent(url, headers, null));
url = "http://127.0.0.1:" + port + "/pipes/hello/asyncfind2/22222";
System.out.println("listmap: " + Utility.postHttpContent(url, headers, null));
url = "http://127.0.0.1:" + port + "/pipes/hello/asyncfind3/333333";
System.out.println("listmap: " + Utility.postHttpContent(url, headers, null));
}
@AuthIgnore
@@ -55,7 +65,7 @@ public class _DynHelloRestServlet1 extends SimpleRestServlet {
bean.setClientaddr(req.getRemoteAddr());
bean.setResname(req.getHeader("hello-res"));
UserInfo user = currentUser(req);
RetResult<HelloEntity> result = service.createHello(user, bean);
RetResult<HelloEntity> result = service.createHello(user, bean, req.getBodyJson(Map.class));
resp.finishJson(result);
}
@@ -135,4 +145,20 @@ public class _DynHelloRestServlet1 extends SimpleRestServlet {
int id = Integer.parseInt(req.getRequstURILastPath());
resp.finishJson(service.asyncFindHello(id));
}
@AuthIgnore
@HttpMapping(url = "/hello/asyncfind2/")
public void asyncfind2(HttpRequest req, HttpResponse resp) throws IOException {
HelloService service = _servicemap == null ? _service : _servicemap.get(req.getHeader(Rest.REST_HEADER_RESOURCE_NAME, ""));
int id = Integer.parseInt(req.getRequstURILastPath());
service.asyncFindHello(resp.createAsyncHandler(), id);
}
@AuthIgnore
@HttpMapping(url = "/hello/asyncfind3/")
public void asyncfind3(HttpRequest req, HttpResponse resp) throws IOException {
HelloService service = _servicemap == null ? _service : _servicemap.get(req.getHeader(Rest.REST_HEADER_RESOURCE_NAME, ""));
int id = Integer.parseInt(req.getRequstURILastPath());
service.asyncFindHello(resp.createAsyncHandler(HelloAsyncHandler.class), id);
}
}