这个http:/com.sun.net.httpserver解决方案不能跨JRE移植。更好的方法是使用正式的webservices api。javax.xml.ws为了引导最小的HTTP服务器.。import java.io._import javax.xml.ws._import javax.xml.ws.http._import javax.xml.transform._import javax.xml.transform.stream._@WebSer
viceProvider@ServiceMode(value=Service.Mode.PAYLOAD) class P extends Provider[Source] {
def invoke(source: Source) = new StreamSource( new StringReader("
Hello There!
"));}val address = "http://127.0.0.1:8080/"Endpoint.create(HTTPBinding.HTTP_BINDING, new P()).publish(address)println("Service running at "+address)println("Type [CTRL]+[C]
to quit!")Thread.sleep(Long.MaxValue)
编辑:这实际上是有效的!上面的代码看起来像Groovy之类的。下面是我测试过的Java翻译:import java.io.*;import javax.xml.ws.*;import javax.xml.ws.http.*;import javax.xml.transform.*;
import javax.xml.transform.stream.*;@WebServiceProvider@ServiceMode(value = Service.Mode.PAYLOAD)
public class Server implements Provider {
public Source invoke(Source request) {
return new StreamSource(new StringReader("
Hello There!
"));}
public static void main(String[] args) throws InterruptedException {
String address = "http://127.0.0.1:8080/";
Endpoint.create(HTTPBinding.HTTP_BINDING, new Server()).publish(address);
System.out.println("Service running at " + address);
System.out.println("Type [CTRL]+[C] to quit!");
Thread.sleep(Long.MAX_VALUE);
}}