我正在尝试独立的JAX-WS Web服务,服务器和客户端(意思是,不在Java EE容器内运行).显示独立服务器端的好SO帖子就是这个.
对于客户端,我发现以下三种似乎有效的方法(使用wsimport生成客户端存根之后):
public static void main(String[] args) throws Exception {
String serviceURL = "http://localhost:9000/soap?wsdl";
{ // WAY 1
URL url = new URL(serviceURL);
QName qname = new QName("urn:playground:jax-ws", "MyService");
Service service = Service.create(url, qname);
IHello port = service.getPort(IHello.class);
System.out.println(port.sayHello("Long John"));
}
{ // WAY 2
MyService service = new MyService();
IHello port = service.getHelloPort();
((javax.xml.ws.BindingProvider) port).getRequestContext().put(javax.xml.ws.BindingProvider.ENDPOINT_ADDRESS_PROPERTY, serviceURL);
System.out.println(port.sayHello("Long John"));
}
{ // WAY 3
URL url = new URL(serviceURL);
QName qname = new QName("urn:playground:jax-ws", …Run Code Online (Sandbox Code Playgroud) 之间的关系是什么:
......特别是location前者的schemaLocation属性与后者的属性之间的关系?
虽然我可以使用目录文件(在JAX-WS/wsimport工具链中)来"覆盖" 元素的schemaLocation属性,xsd:import但似乎我不能location对wsdl:import语句的属性执行相同的操作.
我有以下示例XML文件:
<a xmlns="http://www.foo.com">
<b>
</b>
</a>
Run Code Online (Sandbox Code Playgroud)
使用XPath表达式/foo:a/foo:b(在其中'foo'正确配置NamespaceContext)我可以正确计算b节点数,并且代码Saxon-HE-9.4.jar在CLASSPATH上和不在时都可以工作.
然而,当我分析了namespace-相同的文件不知道 DocumentBuilderFactory,XPath表达式"/ A/B"正确计数的数量b节点只当Saxon-HE-9.4.jar是没有在CLASSPATH.
代码如下:
import java.io.*;
import java.util.*;
import javax.xml.xpath.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import javax.xml.namespace.NamespaceContext;
public class FooMain {
public static void main(String args[]) throws Exception {
String xmlSample = "<a xmlns=\"http://www.foo.com\"><b></b></a>";
{
XPath xpath = namespaceUnawareXpath();
System.out.printf("[NS-unaware] Number of 'b' nodes is: %d\n",
((NodeList) xpath.compile("/a/b").evaluate(stringToXML(xmlSample, false),
XPathConstants.NODESET)).getLength());
}
{
XPath xpath …Run Code Online (Sandbox Code Playgroud) 有一种方法可以多次执行配方吗?在下面的SSCCE中,该配方仅执行一次:
$ cat Makefile
.PHONY: a b
a: b b
b:
echo "beta"
$ make
echo "beta"
beta
Run Code Online (Sandbox Code Playgroud) 我理解map没有在数组上调用未定义的索引,我知道未定义的索引不同于显式分配'undefined'值的数组索引(它不是吗?).然而,如何区分数组中的空洞和undefined值?
以下代码:
foo.js
var arr = [,,undefined,,,,3,,,4];
console.log("A hole the same as undefined? "+(arr[0]===undefined?"yes":"no"));
var _ignored = arr.map(function(x) {console.log("f called on ["+x+"]");});
Run Code Online (Sandbox Code Playgroud)
...运行时产生:
$ node foo.js
A hole the same as undefined? yes
f called on [undefined]
f called on [3]
f called on [4]
Run Code Online (Sandbox Code Playgroud)
......类似的结果是更换时必须有map有forEach.
根据MDN的这个解释:
this指的是全局对象然而,以下内容:
var globalThis = this;
function a() {
console.log(typeof this);
console.log(typeof globalThis);
console.log('is this the global object? '+(globalThis===this));
}
a();
Run Code Online (Sandbox Code Playgroud)
...放在文件中foo.js产生:
$ nodejs foo.js
object
object
is this the global object? false
Run Code Online (Sandbox Code Playgroud) 如果空数组在JavaScript中[]是假的,那么为什么当用作三元运算符中的谓词时,运算符会逃避第一个选项?
console.log([] == false); // prints true
console.log([] ? 0 : 1); // prints 0 !
Run Code Online (Sandbox Code Playgroud) 在ECMAScript6中,迭代器是一个对象,其next()方法返回一个具有两个属性的对象:{value, done}。例如:
function createIterator(ra) {
let i = 0;
return {
next() {
if (i < ra.length)
return {value: ra[i++],
done: false};
else
return {value: undefined,
done: true};
}
};
}
Run Code Online (Sandbox Code Playgroud)
创建了迭代器后,发现它本身并没有太大用处,因为没有内置语法可利用迭代器。相反,必须执行以下操作:
const it = createIterator(['a', 'b', 'c']);
while (true) {
const o = it.next();
if (o.done)
break;
else
console.log(o.value);
}
Run Code Online (Sandbox Code Playgroud)
另一方面Symbol.iterator,“可迭代”是具有属性的对象,该属性必须是返回迭代器的无参数函数:
function createIterable(ra) {
let it = createIterator(ra);
return {
[Symbol.iterator]() {return it;}
};
}
Run Code Online (Sandbox Code Playgroud)
...,当我们创建一个可迭代对象时,我们终于可以使用以下for of语法:
for (let …Run Code Online (Sandbox Code Playgroud) 我有一个vectorofstrings和一个空vector的int:
vector<string> ints_as_strings;
vector<int> ints_as_ints;
Run Code Online (Sandbox Code Playgroud)
目标是将前者转变为后者。
std::transform与 lambda 一起工作:
std::transform(ints_as_strings.begin(),
ints_as_strings.end(),
back_inserter(ints_as_ints),
[](const string& s) {return std::stoi(s);});
Run Code Online (Sandbox Code Playgroud)
然而我只想使用std::stoi.
传递我的函数的地址mystoi是有效的:
int mystoi(string s) {return stoi(s);}
std::transform(ints_as_strings.begin(),
ints_as_strings.end(),
back_inserter(ints_as_ints),
&mystoi);
Run Code Online (Sandbox Code Playgroud)
但是,直接传递地址是std::stoi行不通的。这是因为额外的参数(但是提供了默认值)吗?
我在我的应用程序的web.xml设置Cookie路径(如建议尝试在这里)到:
<session-config>
<cookie-config>
<path>/</path>
</cookie-config>
</session-config>
Run Code Online (Sandbox Code Playgroud)
因此,我分别向localhost:8080/application-a和部署了两个相同的Web应用程序localhost:8080/application-b。
每个应用程序都是一个servlet:
public class ControllerServlet extends HttpServlet{
@Override
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
HttpSession session = req.getSession(false);
if (session == null) {
session = req.getSession(true);
System.out.printf("No session was present - new one created with JSESSIONID=[%s]\n", session.getId());
} else {
System.out.printf("JSESSIONID cookie was present and HttpSession objects exists with JSESSIONID=[%s]\n", session.getId());
}
}
}
Run Code Online (Sandbox Code Playgroud)
我将应用程序部署到Tomcat 8.5容器(与Tomcat 9进行了尝试,并且行为相同)。当我使用浏览器访问时application-a,会看到以下内容:
……在Tomcat日志上,我读到:
No session was present …Run Code Online (Sandbox Code Playgroud)