我想在我的应用程序中为所有Jersey请求添加Access-Control-Allow-Origin
标头。是否可以通过Jersey servlet执行此操作以使其适用于应用程序?
ServletRegistration jerseyServletRegistration = ctx.addServlet("JerseyServlet", new ServletContainer());
jerseyServletRegistration.setInitParameter("com.sun.jersey.config.property.packages", "com.production.resource");
jerseyServletRegistration.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", "true");
jerseyServletRegistration.setLoadOnStartup(1);
jerseyServletRegistration.addMapping("/api/*");
答案 0 :(得分:0)
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientRequest;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.filter.ClientFilter;
public final class JerseySample {
public static void main(final String[] args) {
final Client client = Client.create();
client.addFilter(new ClientFilter() {
@Override
public ClientResponse handle(final ClientRequest request) {
final Map<String, List<Object>> headers = request.getHeaders();
final List<Object> valueList = new ArrayList<Object>();
valueList.add("Sample Value");
headers.put("Sample-Header", valueList);
return getNext().handle(request);
}
});
...
}
}
答案 1 :(得分:0)
Jersey 2.9.9(2019)使用标准的JAX-RS API:
public override View GetView(int position, View convertView, ViewGroup parent)
{
...
convertView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.layout6, null);
convertView.setLayoutParams(new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 300));//300 is your custom height
...
}
我建议只使用一个过滤器实例,并使其成为线程安全。
Javadoc中有一条注释:
必须使用@Provider注释实现此接口的过滤器
似乎不是真的。