我有一个基于纳米HTTP的网络服务器,应该将其调用委托给球衣2.22.2。在webserver类构造函数中,我将ApplicationHandler声明为实例变量:
import java.applet.Applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Option3 extends Frame implements ActionListener {
Button objButton1;
Button objButton2;
Button objButton3;
TextField objTextField;
Option3() {
setTitle("Option 3");
setSize (300,300); // is better to control the frame's size by using panels with appropriate layout managers.
objButton1 = new Button("A");
objButton2 = new Button("B");
objButton3 = new Button("C");
objTextField = new TextField(100);
objButton1.addActionListener(this);
objButton2.addActionListener(this);
objButton3.addActionListener(this);
Panel panel = new Panel(); // set a layout to this panel based on how you want the components to be displayed.
panel.add(objButton1);
panel.add(objButton2);
panel.add(objButton3);
panel.add(objTextField);
add(panel);
show();
}
public static void main (String args[]) {
Frame objFrame = new Option3();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == objButton1 ) { //delimit this kind of statements using curly braces to avoid confusion and bugs.
System.out.println("A");
} else if (e.getSource() == objButton2 ) {
System.out.println("B");
} else {
System.out.println("C");
}
}
}
然后在构造函数中,我初始化它并注册一个Sample资源类:
ApplicationHandler newHandler;
在处理Http请求的方法上,我创建了一个ContainerRequest并在应用程序处理程序上执行apply方法:
Object[] instances = new Object[1];
instances[0] = new SampleResource();
ResourceConfig app = new ResourceConfig();
app.registerInstances(instances);
newHandler = new ApplicationHandler(app);
以下是SampleResource类的代码:
SecurityContext secContext = new SecurityContext() {
@Override
public Principal getUserPrincipal() {
return new Principal() {
@Override
public String getName() {
return "user";
}
};
}
@Override
public boolean isUserInRole(String s) {
return true;
}
@Override
public boolean isSecure() {
return true;
}
@Override
public String getAuthenticationScheme() {
return null;
}
};
PropertiesDelegate propertiesDelegate = new PropertiesDelegate() {
Map<String, Object> props = new HashMap<>();
@Override
public Object getProperty(String s) {
return props.get(s);
}
@Override
public Collection<String> getPropertyNames() {
return props.keySet();
}
@Override
public void setProperty(String s, Object o) {
props.put(s, o);
}
@Override
public void removeProperty(String s) {
props.remove(s);
}
};
ContainerRequest request = new ContainerRequest(new URI("http://localhost:2000"), new URI("/test"), session.getMethod().toString(), secContext, propertiesDelegate);
Future<ContainerResponse> responseFuture = newHandler.apply(request);
ContainerResponse response = responseFuture.get();
Object entity = response.getEntity();
}
这样做的主要原因是我想调用一个自定义API,将对象注入带注释的资源类中。 单步执行代码,我得到的只是一个NotFoundException。
答案 0 :(得分:0)
如果要将自定义对象注入资源类,可以在两个waus中执行此操作
要使用@Context,您需要扩展java.security.Principal对象并声明对象字段,并且可以使用安全上下文来实例化和分配值。
用户@InJect,您需要注册org.glassfish.hk2.utilities.binding.AbstractBinder,如下所示
returnTo
}