我们使用JAX-RS一些相当基本的POJO的实体,具有许多@GET
和@POST
注解的方法是@Produce
和@Consume
MediaType.APPLICATION_JSON
,MediaType.APPLICATION_XML
.没什么了不起的.
我的问题是我应该如何最好地验证进来的数据?
我们没有XML Schema,尽管我可以生成一个.我需要以某种方式挂钩这看起来不太吸引人,我还没有找到一个简洁的例子.
我们可以使用"bean验证",但我再次不确定如何将其挂钩并调用它.
最后(我认为)我们可以为isValidForXXX()
实体POJO 添加一些方法,并在我们有一个交给我们的实例时调用它们.
有人建议吗?
我有一个Java类用于JUnit 4.x. 在每个@Test方法中,我创建了一个新的HttpServer,使用了端口9090.第一个调用工作查找,但后续的错误与"地址已被使用:绑定".
这是一个例子:
@Test
public void testSendNoDataHasValidResponse() throws Exception {
InetSocketAddress address = new InetSocketAddress(9090);
HttpHandler handler = new HttpHandler() {
@Override
public void handle(HttpExchange exchange) throws IOException {
byte[] response = "Hello, world".getBytes();
exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.length);
exchange.getResponseBody().write(response);
exchange.close();
}
};
HttpServer server = HttpServer.create(address, 1);
server.createContext("/me.html", handler);
server.start();
Client client = new Client.Builder(new URL("http://localhost:9090/me.html"), 20, "mykey").build();
client.sync();
server.stop(1);
assertEquals(true, client.isSuccessfullySynchronized());
}
Run Code Online (Sandbox Code Playgroud)
很明显,HttpServer仅在每个方法中保存,并在结束前停止.我没有看到什么继续保持任何套接字打开.第一次测试通过,后续测试每次都失败.
有任何想法吗?
使用更正方法编辑:
@Test
public void testSendNoDataHasValidResponse() throws Exception {
server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 1);
HttpHandler handler …
Run Code Online (Sandbox Code Playgroud)