Я новичок в веб-сервисах, у меня есть 2 простых java проекта (maven), один - сервис, а другой - клиент, мне просто нужно использовать это, используя soap ssl. Это мой веб-сервис ( интерфейс + реализация):
@WebService
@SOAPBinding(style = Style.RPC)
public interface CalculService {
@WebMethod
@WebResult
public int test(int val1, int val2);
}
///// impl///
@WebService(endpointInterface =
"wstest.CalculService")
public class CalculImpl implements CalculService {
public int test(int val1, int val2) {
return val1 + val2;
}
}
Теперь, после нескольких часов поиска в Google, я не мог найти что-то простое для понимания, поэтому я нашел этот код ниже, который не показался мне сложным:
public class ClientSoap {
public static void main(String[] args) throws IOException {
// Code to make a webservice HTTP request
String responseString = "";
String outputString = "";
String wsEndPoint = "http://localhost:8883/myservice";
URL url = new URL(wsEndPoint);
URLConnection connection = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) connection;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
String xmlInput = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ "<soapenv:Header/><soapenv:Body> <generaleRequest xmlns=\"http://wstest/CalculService/\"><arg0>50</arg0>"
+ "</generaleRequest></soapenv:Body></soapenv:Envelope>";
byte[] buffer = new byte[xmlInput.length()];
buffer = xmlInput.getBytes();
bout.write(buffer);
byte[] b = bout.toByteArray();
String SOAPAction = "generale";
httpConn.setRequestProperty("Content-Length", String.valueOf(b.length));
httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
httpConn.setRequestProperty("SOAPAction", SOAPAction);
httpConn.setRequestMethod("POST");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
OutputStream out = httpConn.getOutputStream();
// Write the content of the request to the outputstream of the HTTP
// Connection.
out.write(b);
out.close();
// Ready with sending the request.
// Read the response.
InputStreamReader isr = new InputStreamReader(httpConn.getInputStream(), Charset.forName("UTF-8"));
BufferedReader in = new BufferedReader(isr);
// Write the SOAP message response to a String.
while ((responseString = in.readLine()) != null) {
outputString = outputString + responseString;
}
// Write the SOAP message formatted to the console.
String formattedSOAPResponse = formatXML(outputString);
System.out.println(formattedSOAPResponse);
}
// format the XML in pretty String
private static String formatXML(String unformattedXml) {
try {
Document document = parseXmlFile(unformattedXml);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setAttribute("indent-number", 3);
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(document);
StreamResult xmlOutput = new StreamResult(new StringWriter());
transformer.transform(source, xmlOutput);
return xmlOutput.getWriter().toString();
} catch (TransformerException e) {
throw new RuntimeException(e);
}
}
// parse XML
private static Document parseXmlFile(String in) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(in));
return db.parse(is);
} catch (IOException | ParserConfigurationException | SAXException e) {
throw new RuntimeException(e);
}
}
}
Пожалуйста, я не делал знать, что положить в эти 2 строки: 1-String wsEndPoint = "http://localhost: 8883 / myservice ";
2- String xmlInput = "
- делает каждый soap я хочу использовать через ssl мне нужно использовать XML? пожалуйста, ребята, извините за мой нубизм в веб-сервисах. для начинающих. спасибо