.Net Webservice in Java ansprechen

y0dA

Erfahrenes Mitglied
Hi!
Wie kann ich ein in .Net entwickeltes Webservice in Java ansprechen? Sprich gibt es bspw. ein Eclipse Plugin mit welchen ich über ein .Net WSDL Java Klassen generieren lassen kann?

Also dieses Webservice benötigt als Parameter ein XML File und retourniert ebenfalls ein XML File. Für diese XML Files habe ich eine XSD und habe mir mit Hilfe dieser schon die Java Klassen mittels JAXP generiert (Eclipse Plugin). Nun würde mich interessieren wie ich das Service ansprechen soll, also mittels javax.xml.ws.Service oder gibts eben ein Eclipse Plugin welches mir mit dem WSDL den Client schreibt?

mfg
 
Ich hab zwar 2 Eclipse Plugins (XFire und Axis) mit welchen ich WDL 2 Java spielen kann, nur sind die nicht auf die beiden Produkte beschränkt?

Wie sieht es aus, wenn das Webservice einen String braucht --> Java String = .Net String?
 
Hallo,

hier mal ein kleines Beispiel wie man einen mit Visual Studio 2005 & C# erstellten Webservice mit JaxWS konsumieren kann:


Unser Webservice:

GreetingService.asmx
XML:
<%@ WebService Language="C#" CodeBehind="GreetingService.asmx.cs" Class="De.Tutorials.Example.Webservice.GreetingService" %>

GreetingService.asmx.cs
C#:
using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;

namespace De.Tutorials.Example.Webservice
{
    /// <summary>
    /// Zusammenfassungsbeschreibung für Service1
    /// </summary>
    [WebService(Namespace = "http://www.tutorials.de/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class GreetingService : System.Web.Services.WebService
    {

        [WebMethod]
        public string Greet(string Name)
        {
            return "Hello " +Name;
        }
    }
}

Die Web.config:
XML:
<?xml version="1.0" encoding="utf-8"?>

<configuration>
  
    <appSettings/>
    <connectionStrings/>
  
    <system.web>
        <!-- 
            Legen Sie beim Kompilieren debug="true" fest, um 
            Debugsymbole in die kompilierte Seite einzufügen. 
            Da dies die Leistung beeinträchtigt, sollte der 
            Wert nur beim Entwickeln auf "True" gesetzt werden.
        -->
        <compilation debug="true" />
        <!--
            Der Abschnitt <authentication> ermöglicht die Konfiguration 
            des Sicherheitsauthentifizierungsmodus, mit dem 
            ASP.NET eingehende Benutzer identifiziert. 
        -->
        <authentication mode="Windows" />
        <!--
            Der Abschnitt <customErrors> ermöglicht die Konfiguration 
            der Vorgehensweise bei unbehandelten Fehlern während 
            der Anforderungsausführung. Insbesondere können 
            Entwickler HTML-Fehlerseiten konfigurieren, die anstelle 
            einer Fehlerstapelüberwachung angezeigt werden.

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
        -->
    </system.web>
</configuration>

Das vom Webservice generierte WSDL:
XML:
<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://www.tutorials.de/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://www.tutorials.de/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
  <wsdl:types>
    <s:schema elementFormDefault="qualified" targetNamespace="http://www.tutorials.de/">
      <s:element name="Greet">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="Name" type="s:string" />
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:element name="GreetResponse">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="GreetResult" type="s:string" />
          </s:sequence>
        </s:complexType>
      </s:element>
    </s:schema>
  </wsdl:types>
  <wsdl:message name="GreetSoapIn">
    <wsdl:part name="parameters" element="tns:Greet" />
  </wsdl:message>
  <wsdl:message name="GreetSoapOut">
    <wsdl:part name="parameters" element="tns:GreetResponse" />
  </wsdl:message>
  <wsdl:portType name="GreetingServiceSoap">
    <wsdl:operation name="Greet">
      <wsdl:input message="tns:GreetSoapIn" />
      <wsdl:output message="tns:GreetSoapOut" />
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="GreetingServiceSoap" type="tns:GreetingServiceSoap">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="Greet">
      <soap:operation soapAction="http://www.tutorials.de/Greet" style="document" />
      <wsdl:input>
        <soap:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal" />
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:binding name="GreetingServiceSoap12" type="tns:GreetingServiceSoap">
    <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="Greet">
      <soap12:operation soapAction="http://www.tutorials.de/Greet" style="document" />
      <wsdl:input>
        <soap12:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap12:body use="literal" />
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="GreetingService">
    <wsdl:port name="GreetingServiceSoap" binding="tns:GreetingServiceSoap">
      <soap:address location="http://localhost:1097/GreetingService.asmx" />
    </wsdl:port>
    <wsdl:port name="GreetingServiceSoap12" binding="tns:GreetingServiceSoap12">
      <soap12:address location="http://localhost:1097/GreetingService.asmx" />
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

Da der Client Document / Literal SOAP verwendet müssen wir einige Klassen generieren:
Code:
D:\temp>wsimport -s source -extension -p de.tutorials.ws.wrappers -d generated http://localhost:1097/GreetingService.asmx?wsdl
parsing WSDL...


[WARNING] SOAP port "GreetingServiceSoap12": uses a non-standard SOAP 1.2 binding.
  line 61 of http://localhost:1097/GreetingService.asmx?wsdl

generating code...

Dabei werden folgende Klassen erzeugt:

Greet.java
Java:
package de.tutorials.ws.wrappers;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>Java class for anonymous complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType>
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="Name" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "name"
})
@XmlRootElement(name = "Greet")
public class Greet {

    @XmlElement(name = "Name")
    private String name;

    /**
     * Gets the value of the name property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    
    public String getName() {
        return name;
    }

    /**
     * Sets the value of the name property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setName(String value) {
        this.name = value;
    }

}

GreetResponse:
Java:
package de.tutorials.ws.wrappers;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>Java class for anonymous complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType>
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="GreetResult" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "greetResult"
})
@XmlRootElement(name = "GreetResponse")
public class GreetResponse {

    @XmlElement(name = "GreetResult")
    protected String greetResult;

    /**
     * Gets the value of the greetResult property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getGreetResult() {
        return greetResult;
    }

    /**
     * Sets the value of the greetResult property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setGreetResult(String value) {
        this.greetResult = value;
    }

}

GreetingServiceSoap:
Java:
package de.tutorials.ws.wrappers;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;


/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.1.1 in JDK 6
 * Generated source version: 2.1
 * 
 */
@WebService(name = "GreetingServiceSoap", targetNamespace = "http://www.tutorials.de/")
@XmlSeeAlso({
    ObjectFactory.class
})
public interface GreetingServiceSoap {


    /**
     * 
     * @param name
     * @return
     *     returns java.lang.String
     */
    @WebMethod(operationName = "Greet", action = "http://www.tutorials.de/Greet")
    @WebResult(name = "GreetResult", targetNamespace = "http://www.tutorials.de/")
    @RequestWrapper(localName = "Greet", targetNamespace = "http://www.tutorials.de/", className = "de.tutorials.ws.wrappers.Greet")
    @ResponseWrapper(localName = "GreetResponse", targetNamespace = "http://www.tutorials.de/", className = "de.tutorials.ws.wrappers.GreetResponse")
    public String greet(
        @WebParam(name = "Name", targetNamespace = "http://www.tutorials.de/")
        String name);

}

ObjectFactory:
Java:
package de.tutorials.ws.wrappers;

import javax.xml.bind.annotation.XmlRegistry;


/**
 * This object contains factory methods for each 
 * Java content interface and Java element interface 
 * generated in the de.tutorials.ws.wrappers package. 
 * <p>An ObjectFactory allows you to programatically 
 * construct new instances of the Java representation 
 * for XML content. The Java representation of XML 
 * content can consist of schema derived interfaces 
 * and classes representing the binding of schema 
 * type definitions, element declarations and model 
 * groups.  Factory methods for each of these are 
 * provided in this class.
 * 
 */
@XmlRegistry
public class ObjectFactory {


    /**
     * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: de.tutorials.ws.wrappers
     * 
     */
    public ObjectFactory() {
    }

    /**
     * Create an instance of {@link Greet }
     * 
     */
    public Greet createGreet() {
        return new Greet();
    }

    /**
     * Create an instance of {@link GreetResponse }
     * 
     */
    public GreetResponse createGreetResponse() {
        return new GreetResponse();
    }

}

GreetingService:
Java:
package de.tutorials.ws.wrappers;

import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceFeature;


/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.1.1 in JDK 6
 * Generated source version: 2.1
 * 
 */
@WebServiceClient(name = "GreetingService", targetNamespace = "http://www.tutorials.de/", wsdlLocation = "http://localhost:1097/GreetingService.asmx?wsdl")
public class GreetingService
    extends Service
{

    private final static URL GREETINGSERVICE_WSDL_LOCATION;

    static {
        URL url = null;
        try {
            url = new URL("http://localhost:1097/GreetingService.asmx?wsdl");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        GREETINGSERVICE_WSDL_LOCATION = url;
    }

    public GreetingService(URL wsdlLocation, QName serviceName) {
        super(wsdlLocation, serviceName);
    }

    public GreetingService() {
        super(GREETINGSERVICE_WSDL_LOCATION, new QName("http://www.tutorials.de/", "GreetingService"));
    }

    /**
     * 
     * @return
     *     returns GreetingServiceSoap
     */
    @WebEndpoint(name = "GreetingServiceSoap")
    public GreetingServiceSoap getGreetingServiceSoap() {
        return (GreetingServiceSoap)super.getPort(new QName("http://www.tutorials.de/", "GreetingServiceSoap"), GreetingServiceSoap.class);
    }

    /**
     * 
     * @param features
     *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the <code>features</code> parameter will have their default values.
     * @return
     *     returns GreetingServiceSoap
     */
    @WebEndpoint(name = "GreetingServiceSoap")
    public GreetingServiceSoap getGreetingServiceSoap(WebServiceFeature... features) {
        return (GreetingServiceSoap)super.getPort(new QName("http://www.tutorials.de/", "GreetingServiceSoap"), GreetingServiceSoap.class, features);
    }

    /**
     * 
     * @return
     *     returns GreetingServiceSoap
     */
    @WebEndpoint(name = "GreetingServiceSoap12")
    public GreetingServiceSoap getGreetingServiceSoap12() {
        return (GreetingServiceSoap)super.getPort(new QName("http://www.tutorials.de/", "GreetingServiceSoap12"), GreetingServiceSoap.class);
    }

    /**
     * 
     * @param features
     *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the <code>features</code> parameter will have their default values.
     * @return
     *     returns GreetingServiceSoap
     */
    @WebEndpoint(name = "GreetingServiceSoap12")
    public GreetingServiceSoap getGreetingServiceSoap12(WebServiceFeature... features) {
        return (GreetingServiceSoap)super.getPort(new QName("http://www.tutorials.de/", "GreetingServiceSoap12"), GreetingServiceSoap.class, features);
    }

}

package-info:
Java:
@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.tutorials.de/", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package de.tutorials.ws.wrappers;


Nun können wir unseren Webservice Client bauen:
Java:
/**
 * 
 */
package de.tutorials;

import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import de.tutorials.ws.wrappers.GreetingServiceSoap;

/**
 * @author Thomas.Darimont
 * 
 */
public class JaxWsWebServiceClientExample {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        //
        Service service = Service.create(new URL(
                "http://localhost:1097/GreetingService.asmx?wsdl"), new QName(
                "http://www.tutorials.de/", "GreetingService"));

        GreetingServiceSoap greetingService = service.getPort(new QName(
                "http://www.tutorials.de/", "GreetingServiceSoap"),
                GreetingServiceSoap.class);
        System.out.println(greetingService);
        System.out.println(greetingService.greet("Tom"));

        //Man kann auch den von wsimport Generierten Webservice
                //Wrapper benutzen
        System.out.println(new GreetingService().getGreetingServiceSoap().greet("Tom"));

    }
}

Ausgabe:
Code:
JAX-WS RI 2.1.1 in JDK 6: Stub for http://localhost:1097/GreetingService.asmx
Hello Tom

Gruß Tom
 

Anhänge

  • de.tutorials.webservices.jaxws.example.zip
    4,9 KB · Aufrufe: 136
  • De.Tutorials.Example.Webservice.zip
    15,6 KB · Aufrufe: 117
Zuletzt bearbeitet von einem Moderator:
Hallo,

ich habe hier einen Server mit einem Web-Service in .Net. Dazu habe ich ein Tool, auch in .Net, um den Service aufzurufen. Der Service erwartet als Parameter einen Benutzernamen und ein Passwort (beides Strings). Mit dem Tool funktioniert das problemlos.

Jetzt habe ich soapUI genommen und wollte mir aus der WSDL einen Java-Client generieren. SoapUI hat auch einen Java-Client generiert, aber wenn ich aus diesem den Service aufrufe, dann erhalte ich diese Meldung:

Code:
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
 xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
 xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0">
   <SOAP-ENV:Body>
      <SOAP-ENV:Fault id="ref-1">
         <faultcode id="ref-2">SOAP-ENV:Server</faultcode>
         <faultstring id="ref-3">**** System.Runtime.Remoting.RemotingException - Server encountered an internal error. For more information, turn off customErrors in the server's .config file.</faultstring>
         <detail xsi:type="a1:ServerFault" xmlns:a1="http://schemas.microsoft.com/clr/ns/System.Runtime.Serialization.Formatters">
            <exceptionType id="ref-4">System.Runtime.Remoting.RemotingException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</exceptionType>
            <message id="ref-5">**** System.Runtime.Remoting.RemotingException - Server encountered an internal error. For more information, turn off customErrors in the server's .config file.</message>
            <stackTrace xsi:null="1"/>
            <exception xsi:null="1"/>
         </detail>
      </SOAP-ENV:Fault>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Was könnte da das Problem sein.

Noch etwas. Der Java-Client sowie das Tool werden auf einem Rechner gestartet der direkt mit dem Server-Rechner über ein Netzwerk verbunden ist, kein Proxy, Firewall, etc.

Ich bin für Tipps dankbar.
 
Mir wird nicht ganz ersichtlich, wie man mit dem Tool "wsimport" umgehen muss und wie vorallem wo ich die generierten Klassen in meiner Applikation einbaue?!

Ich bekomme immer folgende Meldung beim Zugriff auf den Service:
 

Anhänge

  • bla.jpg
    bla.jpg
    15,9 KB · Aufrufe: 273
Zuletzt bearbeitet:

Neue Beiträge

Zurück