This commit is contained in:
@@ -1,12 +1,16 @@
|
||||
package org.redkale.test.rest;
|
||||
|
||||
import org.redkale.convert.json.JsonFactory;
|
||||
import org.redkale.net.http.*;
|
||||
import org.redkale.source.FilterBean;
|
||||
|
||||
public class HelloBean implements FilterBean {
|
||||
|
||||
private int helloid;
|
||||
|
||||
@RestHeader("hello-res")
|
||||
private String res;
|
||||
|
||||
public int getHelloid() {
|
||||
return helloid;
|
||||
}
|
||||
@@ -15,8 +19,17 @@ public class HelloBean implements FilterBean {
|
||||
this.helloid = helloid;
|
||||
}
|
||||
|
||||
public String getRes() {
|
||||
return res;
|
||||
}
|
||||
|
||||
public void setRes(String res) {
|
||||
this.res = res;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonFactory.root().getConvert().convertTo(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,7 +2,10 @@ package org.redkale.test.rest;
|
||||
|
||||
import javax.persistence.Id;
|
||||
import org.redkale.convert.json.JsonFactory;
|
||||
import org.redkale.net.http.*;
|
||||
import org.redkale.source.VirtualEntity;
|
||||
|
||||
@VirtualEntity
|
||||
public class HelloEntity {
|
||||
|
||||
@Id
|
||||
@@ -16,6 +19,12 @@ public class HelloEntity {
|
||||
|
||||
private long createtime;
|
||||
|
||||
@RestHeader("hello-res")
|
||||
private String resname;
|
||||
|
||||
@RestAddress
|
||||
private String clientaddr;
|
||||
|
||||
public int getHelloid() {
|
||||
return helloid;
|
||||
}
|
||||
@@ -56,6 +65,22 @@ public class HelloEntity {
|
||||
this.creator = creator;
|
||||
}
|
||||
|
||||
public String getClientaddr() {
|
||||
return clientaddr;
|
||||
}
|
||||
|
||||
public void setClientaddr(String clientaddr) {
|
||||
this.clientaddr = clientaddr;
|
||||
}
|
||||
|
||||
public String getResname() {
|
||||
return resname;
|
||||
}
|
||||
|
||||
public void setResname(String resname) {
|
||||
this.resname = resname;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonFactory.root().getConvert().convertTo(this);
|
||||
|
||||
@@ -9,7 +9,6 @@ import org.redkale.source.DataSource;
|
||||
import org.redkale.source.Flipper;
|
||||
import org.redkale.util.Sheet;
|
||||
|
||||
|
||||
/**
|
||||
* 类说明:
|
||||
* Flipper : Source组件中的翻页对象
|
||||
@@ -37,18 +36,19 @@ public class HelloService implements Service {
|
||||
}
|
||||
|
||||
//修改记录
|
||||
public void updateHello(HelloEntity entity) { //通过 /hello/update?bean={...} 修改对象
|
||||
entity.setUpdatetime(System.currentTimeMillis());
|
||||
source.update(entity);
|
||||
public void updateHello(@RestAddress String clientAddr, HelloEntity entity) { //通过 /hello/update?bean={...} 修改对象
|
||||
System.out.println("修改记录: clientAddr = " + clientAddr + ", entity =" + entity);
|
||||
if (entity != null) entity.setUpdatetime(System.currentTimeMillis());
|
||||
if (source != null) source.update(entity);
|
||||
}
|
||||
|
||||
//修改记录
|
||||
@RestMapping(name = "partupdate")
|
||||
@RestMapping(name = "partupdate")
|
||||
public void updateHello(HelloEntity entity, @RestParam("cols") String[] columns) { //通过 /hello/partupdate?bean={...} 修改对象
|
||||
entity.setUpdatetime(System.currentTimeMillis());
|
||||
source.updateColumns(entity, columns);
|
||||
}
|
||||
|
||||
|
||||
//查询Sheet列表
|
||||
public Sheet<HelloEntity> queryHello(HelloBean bean, Flipper flipper) { //通过 /hello/query/offset:0/limit:20?bean={...} 查询Sheet列表
|
||||
return source.querySheet(HelloEntity.class, flipper, bean);
|
||||
@@ -66,4 +66,4 @@ public class HelloService implements Service {
|
||||
public HelloEntity findHello(@RestParam("#") int id) { //通过 /hello/find/1234 查询对象
|
||||
return source.find(HelloEntity.class, id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,16 +15,13 @@ import org.redkale.service.*;
|
||||
*/
|
||||
public abstract class RetCodes {
|
||||
|
||||
static {
|
||||
load(RetCodes.class);
|
||||
}
|
||||
protected static final Map<Integer, String> rets = new HashMap<>();
|
||||
|
||||
|
||||
protected RetCodes() {
|
||||
}
|
||||
//-----------------------------------------------------------------------------------------------------------
|
||||
|
||||
protected static final Map<Integer, String> rets = new HashMap<>();
|
||||
|
||||
protected static void load(Class clazz) {
|
||||
for (Field field : clazz.getFields()) {
|
||||
if (!Modifier.isStatic(field.getModifiers())) continue;
|
||||
@@ -158,4 +155,9 @@ public abstract class RetCodes {
|
||||
|
||||
@RetLabel("用户设备ID无效")
|
||||
public static final int RET_USER_APPTOKEN_ILLEGAL = 30020030;
|
||||
|
||||
|
||||
static {
|
||||
load(RetCodes.class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,45 @@
|
||||
package org.redkale.test.rest;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.*;
|
||||
import javax.annotation.Resource;
|
||||
import org.redkale.net.http.*;
|
||||
import org.redkale.service.RetResult;
|
||||
import org.redkale.source.Flipper;
|
||||
import org.redkale.util.*;
|
||||
import org.redkale.util.AnyValue.DefaultAnyValue;
|
||||
|
||||
@WebServlet(value = {"/hello/*"}, repair = true)
|
||||
public class _DynHelloRestServlet extends SimpleRestServlet {
|
||||
public class _DynHelloRestServlet1 extends SimpleRestServlet {
|
||||
|
||||
@Resource
|
||||
private HelloService _service;
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
final int port = 8888;
|
||||
HelloService service = new HelloService();
|
||||
HttpServer server = new HttpServer();
|
||||
RestHttpServlet servlet = Rest.createRestServlet(SimpleRestServlet.class, "", HelloService.class, false);
|
||||
Field field = servlet.getClass().getDeclaredField("_service");
|
||||
field.setAccessible(true);
|
||||
field.set(servlet, service);
|
||||
server.addHttpServlet(servlet, "/pipes", null, "/hello/*");
|
||||
|
||||
DefaultAnyValue conf = DefaultAnyValue.create("port", "" + port);
|
||||
server.init(conf);
|
||||
server.start();
|
||||
Thread.sleep(100);
|
||||
|
||||
HelloEntity entity = new HelloEntity();
|
||||
entity.setHelloname("my name");
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
headers.put("hello-res", "my res");
|
||||
String url = "http://127.0.0.1:" + port + "/pipes/hello/update?entity={}&bean2={}";
|
||||
System.out.println(Utility.postHttpContent(url, headers, null));
|
||||
|
||||
}
|
||||
|
||||
@AuthIgnore
|
||||
@WebAction(url = "/hello/create")
|
||||
public void create(HttpRequest req, HttpResponse resp) throws IOException {
|
||||
@@ -34,8 +60,9 @@ public class _DynHelloRestServlet extends SimpleRestServlet {
|
||||
@AuthIgnore
|
||||
@WebAction(url = "/hello/update")
|
||||
public void update(HttpRequest req, HttpResponse resp) throws IOException {
|
||||
String clientaddr = req.getRemoteAddr();
|
||||
HelloEntity bean = req.getJsonParameter(HelloEntity.class, "bean");
|
||||
_service.updateHello(bean);
|
||||
_service.updateHello(clientaddr, bean);
|
||||
resp.finishJson(RetResult.success());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user