我正在使用Jersey查看一个很好的REST教程.在页面下方,有一个构建的Web资源,该资源TodoResource本身包含两个实例变量
public class TodoResource {
@Context
UriInfo uriInfo;
@Context
Request request;
String id;
public TodoResource(UriInfo uriInfo, Request request, String id) {
this.uriInfo = uriInfo;
this.request = request;
this.id = id;
}
}
Run Code Online (Sandbox Code Playgroud)
我想知道如何初始化UriInfo和Request实例变量?我知道使用@Context注释可以注入信息,但这会发生在什么时候?这会由泽西岛自动处理吗?
我是RESTful webservices的新手,并试图@OneToMany从独立的客户端应用程序更新我的关系,但我无法做到这一点.我正在使用Glassfish 3.1.1附带的JAX-RS的Jersey实现.
我有一个与班级A有@OneToMany关系的班级B.
MyRestClient 是我的独立客户端,它调用我在Glassfish 3.1.1上部署的RESTful Web服务.
MyRestClient.java
public class MyRestClient {
public static void main(String[] args) {
Client client = Client.create();
WebResource resource = client.resource("http://localhost:8080/myapp/rest/a/update/123");
B b1 = new B("debris");
ClientResponse response = resource.put(ClientResponse.class, b1);
System.out.println(response.getEntity(A.class).getTitle() + " has " + response.getEntity(A.class).getBList().size() + " Bs.");
}
}
Run Code Online (Sandbox Code Playgroud)
AResource 是一个EJB会话bean,我将其用作RESTful Web服务.
AResource.java
@Stateless
@Path("/a")
public class AResource {
@EJB
private AManager aManager;
@PUT
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_XML)
@Path("/update/{id}")
public Response updateA(B b, @PathParam("id") …Run Code Online (Sandbox Code Playgroud) 我希望有一个构造函数,其参数可以自动地由所有子类继承,但Java不允许我这样做
class A {
public A(int x) {
// Shared code here
}
}
class B extends A {
// Implicit (int x) constructor from A
}
class C extends A {
// Implicit (int x) constructor from A
}
Run Code Online (Sandbox Code Playgroud)
我不希望有写B(int x),C(int x)等等.每个子类.是否有更聪明的方法来解决这个问题?
解决方案#1.创建一个init()可以在构造函数之后调用的方法.这是有效的,虽然对于我的特定设计,我想要求用户在构造函数中指定在编译时验证的某些参数(例如,不通过varargs/reflection).
鉴于我使用CheckBoxModel获得了ExtJS网格,获取复选框所有记录列表的最佳方法是什么?
(我在JavaScript的上下文中写这个,但是会接受任何语言的算法正确答案)
如何在字符串数组中找到每个元素的最短子字符串,其中子字符串不包含在任何其他元素中,忽略大小写?
假设我有一个输入数组,例如:
var names = ["Anne", "Anthony", "LouAnn", "Kant", "Louise", "ark"];
Run Code Online (Sandbox Code Playgroud)
输出应该是这样的:
var uniqueNames = ["ne", "h", "ua", "ka", "i", "r"];
Run Code Online (Sandbox Code Playgroud)
出于我的目的,您可以安全地假设没有元素将完全包含在另一个元素中.
我的想法:
似乎人们可能会蛮力这样做,其方式如下:
var names = ["Anne", "Anthony", "LouAnn", "Kant", "Louise", "ark"];
var uniqueNames = [], nameInd, windowSize, substrInd, substr, otherNameInd, foundMatch;
// For each name
for (nameInd = 0; nameInd < names.length; nameInd++)
{
var name = names[nameInd];
// For each possible substring length
windowLoop:
for (windowSize = 1; windowSize <= name.length; windowSize++)
{
// For each …Run Code Online (Sandbox Code Playgroud) 在使用Java 6 API编写注释处理器时,我遇到了以特定方式处理所有地图的需求,但我显然误解了API的目的或如何调用它.这是让我不开心的代码:
import javax.lang.model.element.Element;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
import javax.annotation.processing.ProcessingEnvironment;
...
public String doThing(Element el, ProcessingEnvironment processingEnv) {
// Utilities from the ProcessingEnvironment
Types typeUtils = processingEnv.getTypeUtils();
Elements elementUtils = processingEnv.getElementUtils();
// The type of the element I'm handling
TypeMirror elType = el.asType();
// Compare the element's type to Map
TypeMirror mapType = elementUtils.getTypeElement("java.util.Map").asType();
System.out.println(elType + " > " + mapType + " = " + typeUtils.isSubtype(elType, mapType));
System.out.println(mapType + " > " + elType + " …Run Code Online (Sandbox Code Playgroud) 我正在使用泽西客户端来打电话给休息网络服务.
我的web服务正在使用json,所以我需要让json调用我的webservice提供者.
我是以下面的方式做的.
JSONObject object=new JSONObject();
object.put("name", employee.getName());
object.put("empId", employee.getEmpId());
object.put("organizationName", employee.getOrganizationName());
ClientResponse response = service.path("rest").path("vtn").path("addEmplyee")
.type(MediaType.APPLICATION_JSON_TYPE).post(ClientResponse.class, object);
Run Code Online (Sandbox Code Playgroud)
但我得到以下例外:
09:52:01,625 ERROR [[mvc-dispatcher]] servlet mvc-dispatcher的Servlet.service()抛出异常 com.sun.jersey.api.client.ClientHandlerException:Java类型的消息体编写器,类net.sf. 在com.sun.jersey.client.url连接.URLConnectionClientHandler的com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:288)中找不到json.JSONObject和MIME媒体类型application/json. _invoke(URLConnectionClientHandler.java:204)位于com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:147)的com.sun.jersey.api.client.Client.handle(Client.java:648)在com.sun.jersey.api.client.WebResource.handle(WebResource.java:670)的com.sun.jersey.api.client.WebResource.access $ 200(WebResource.java:74)com.sun.jersey. api.client.WebResource $ Builder.post(WebResource.java:563)位于com.nec.jp.pflow.unc的com.nec.jp.pflow.unc.service.EmployeeService.addEmployee(EmployeeService.java:44). controller.EmployeeController.addCus tomer(EmployeeController.java:29)位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)的sun.reflect.NativeMethodAccessorImpl.invoke0(本地方法)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
但是,如果我将我的json转换为字符串表示形式,如:
String input = "{\"name\" : \"" + employee.getName() + "\",\"empId\" : \"" + employee.getEmpId() + "\",\"organizationName\" : \"" + employee.getOrganizationName() + "\"}";
ClientResponse response = service.path("rest").path("vtn").path("addEmplyee")
.type(MediaType.APPLICATION_JSON_TYPE).post(ClientResponse.class, input);
Run Code Online (Sandbox Code Playgroud)
然后它工作正常.
请建议如何在不获得上述异常的情况下放置我的JSON对象.什么是最好的方法?
提前致谢.
我得到了上述解决方案.现在我正在使用jackson-mapper api将POJO转换为json.
以下是代码段.
ObjectMapper mapper = new ObjectMapper();
ClientResponse response = …Run Code Online (Sandbox Code Playgroud) 我使用Jersey的Client类和Jackson一起调用RESTful Web服务来处理与JSON的序列化.我也在使用该JSONConfiguration.FEATURE_POJO_MAPPING设置让Jackson自动将我的POJO序列化为JSON.
我正在发送我的POJO进行消费MediaType.APPLICATION_FORM_URLENCODED和生产的远程服务MediaType.APPLICATION_JSON_TYPE.
我是否必须创建自己的MessageBodyWriter实现来处理POJO序列化application/x-www-form-urlencoded,或者Jersey是否提供了使用我的POJO注释为我执行此操作的实现?
我正在尝试编写一个复制文件的简单tcp客户端/服务器应用程序.我希望服务器列出客户端可以复制的文件.到目前为止我的代码是这样的:
import java.io.*;
public class GetFileList
{
public static void main(String args[]) throws IOException{
File file = new File(".");
File[] files = file.listFiles();
System.out.println("Current dir : " + file.getCanonicalPath());
for (int fileInList = 0; fileInList < files.length; fileInList++)
{
System.out.println(files[fileInList].toString());
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
Current dir : C:\Users\XXXXX\Documents\NetBeansProjects\Test
.\build
.\build.xml
.\manifest.mf
.\nbproject
.\src
.\UsersXXXXXDocumentsNetBeansProjectsTestsrcfile2.txt
Run Code Online (Sandbox Code Playgroud)
我的问题是它给了我父目录而不是当前目录.我的GetFileList.java位于C:\Users\XXXXX\Documents\NetBeansProjects\Test\src但它显示C:\Users\Alick\Documents\NetBeansProjects\Test任何人都可以帮我解决这个问题吗?
我将如何等待多个商店加载?我有一个案例,我需要在加载两个不同的商店时做一些工作,所以使用store.on("load", fn)一个商店是不够的.