Create a soap Web Service in netbeans 6

Source: Internet
Author: User
Tags soapui netbeans

By Siegfried bolz,
3/27/08

 

This article describes how to create a web service in netbeans 6. In this post, I will discuss how to process soap messages before calling Web service operations.

In this example, I will combine JAX-WS 2.1 with netbeans 6.0.

Web Service Description Language (WSDL)

There are many ways to develop Web Services. One of them is to create a WSDL. First, you must understand the role of Web Services. You need to consider the input and output of various web service operations. In this example, we create only one operation named"Getcalculatevalues". The input includes two numbers and the result is the sum of the two numbers.

We will create the following two files:

WebServices. WSDL

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<definitions xmlns:ns1="soapwebservices.jdevelop.eu" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:ns="http://schemas.xmlsoap.org/soap/encoding/" name="SOAPWebServices" targetNamespace="soapwebservices.jdevelop.eu">
<types>
<xsd:schema>
<xsd:import namespace="soapwebservices.jdevelop.eu" schemaLocation="webservices.xsd"/>
</xsd:schema>
</types>
<message name="calculateValues">
<part name="calculateValues" element="ns1:calculateValues"/>
</message>
<message name="calculateValuesResponse">
<part name="calculateValuesResponse" element="ns1:calculateValuesResponse"/>
</message>
<portType name="SOAPWebServices">
<operation name="getCalculateValues">
<input message="ns1:calculateValues"/>
<output message="ns1:calculateValuesResponse"/>
</operation>
</portType>
<binding name="SOAPWebServicesPortBinding" type="ns1:SOAPWebServices">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="getCalculateValues">
<soap:operation soapAction="urn:http://blog.jdevelop.eu/services/getCalculateValues"/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="SOAPService">
<port name="WebServices" binding="ns1:SOAPWebServicesPortBinding">
<soap:address location="http://blog.jdevelop.eu:80/services"/>
</port>
</service>
</definitions>

WebServices. XSD

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:ns1="http://blog.jdevelop.eu/soapwebservices.xsd" xmlns:tns="soapwebservices.jdevelop.eu" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="soapwebservices.jdevelop.eu" version="1.0">
<xs:element name="calculateValues">
<xs:complexType>
<xs:sequence>
<xs:element name="value1" type="xs:decimal"/>
<xs:element name="value2" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="calculateValuesResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="result" type="xs:decimal"/>
<xs:element name="errormessage" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

Create a netbeans Application

Start netbeans 6 and create a new"Web Application"Create.

Name it"Soapwebservices", Remove"Context path"Option and click"Finish"Button.

Now, open"File-> New file-> Web Services"And select"Web service from WSDL".
If you do not have this menu, you need to install"Web Service plugin"(Tools-> plugins-> available plugins ).

Enter"Serviceimpl"As the Web service name, use"EU. jdevelop. soapwebservices. Service"As the package name and browse to the created"WebServices. WSDL"File.

After you click the "finish" button, you will see the following screen:

Now you need to modify the URL mode. Open"Sun-jaxws.xml"File and switch URL mode with"/Soapwebservices".

For"Web. xml"The file performs the same operation.

Open"Index. jsp"File and insert"Body"Tag:

<jsp:forward page="soapwebservices"></jsp:forward>

Now you can test the created pseudo web service. Right-click the project name and select"Clean and build".

Then, clickRun".

Open"Http: // localhost: 8084"To view the results.

You can see the WSDL and XML modes.

Next, you need to insert the business logic. Add the following Java class accept package structure:

Serviceimpl. Java

package eu.jdevelop.soapwebservices.service;

import eu.jdevelop.soapwebservices.CalculateValues;
import eu.jdevelop.soapwebservices.CalculateValuesResponse;
import eu.jdevelop.soapwebservices.SOAPWebServices;
import eu.jdevelop.soapwebservices.wrapper.impl.CalculateValuesWrapper;
import javax.jws.WebService;

/**
* This is the Service-Implementation of the Web Service. Here are the
* operations which can be called from web clients.
*
* @author Siegfried Bolz
*/
@WebService(serviceName = "SOAPService", portName = "WebServices", endpointInterface = "eu.jdevelop.soapwebservices.SOAPWebServices", targetNamespace = "soapwebservices.jdevelop.eu", wsdlLocation = "WEB-INF/wsdl/ServiceImpl/webservices.wsdl")
public class ServiceImpl implements SOAPWebServices {

public CalculateValuesResponse getCalculateValues(CalculateValues calculateValues) {

try {
CalculateValuesWrapper wrapper = new CalculateValuesWrapper();
return wrapper.getResult(calculateValues);

} catch (Exception x) {
throw new IllegalStateException(x);
}
}

} // .EOF

Ilogic. Java

package eu.jdevelop.soapwebservices.logic;

/**
* Use this interface to create logic-implementations for
* each web service operation.
*
* @author Siegfried Bolz
*/
public interface ILogic<T, V> {

public T doAction(V var)
throws Exception;
} // .EOF

Calculatevalueslogic. Java

package eu.jdevelop.soapwebservices.logic.impl;

import eu.jdevelop.soapwebservices.CalculateValues;
import eu.jdevelop.soapwebservices.CalculateValuesResponse;
import eu.jdevelop.soapwebservices.logic.ILogic;
import java.math.BigDecimal;

/**
* This implementation is normaly used for executing operations.
* Here we calculate some values.
*
* @author Siegfried Bolz
*/
public class CalculateValuesLogic implements ILogic<CalculateValuesResponse, CalculateValues>{

public CalculateValuesResponse doAction(CalculateValues var) throws Exception {

CalculateValuesResponse response = new CalculateValuesResponse();

try {
/**
* Simple addition of two values
*/
BigDecimal value1 = var.getValue1();
BigDecimal value2 = var.getValue2();

double sum = value1.doubleValue() + value2.doubleValue();

response.setResult(BigDecimal.valueOf(sum));

} catch (Exception x) {
/**
* On errors, return a valid bean with values. Do not send null!
*/
CalculateValuesResponse errorResponse = new CalculateValuesResponse();
errorResponse.setResult(BigDecimal.valueOf(0.0));
errorResponse.setErrormessage("An error has occurred!");
return errorResponse;
}

return response;
}

} // .EOF

Iwrapper. Java

package eu.jdevelop.soapwebservices.wrapper;

/**
* Use this interface to create wrapper-implementations for
* each web service operation.
*
* @author Siegfried Bolz
*/
public interface IWrapper<T, V> {

public T getResult(V var)
throws Exception;
} // .EOF

Calculatevalueswrapper. Java

package eu.jdevelop.soapwebservices.wrapper.impl;

import eu.jdevelop.soapwebservices.CalculateValues;
import eu.jdevelop.soapwebservices.CalculateValuesResponse;
import eu.jdevelop.soapwebservices.logic.impl.CalculateValuesLogic;
import eu.jdevelop.soapwebservices.wrapper.IWrapper;

/**
* The wrapper calls the logic-implementation. Exchange or modify
* the wrapper if you want to use other logic-implementations.
*
* @author Siegfried Bolz
*/
public class CalculateValuesWrapper implements IWrapper<CalculateValuesResponse, CalculateValues>{

public CalculateValuesResponse getResult(CalculateValues var) throws Exception {
CalculateValuesLogic logic = new CalculateValuesLogic();
return logic.doAction(var);
}

} // .EOF

After that, you should be able to see a similar File View:

Test

Download, install, and startSoapui', URL: http://www.soapui.org. Import the soapui project file"SOAPWebServices-soapui-project.xml"(In this example ).
Open"Request1"And submit the request (click the green arrow ).

Congratulations, the Web service is running normally.

Potential problems

If the following error occurs:

Caused by: java.lang.LinkageError: JAXB 2.0 API is being loaded from the bootstrap classloader, but this RI (from jar: file:/C:/temp/SOAPWebServices/build/web/WEB-INF/lib/jaxb-impl.jar!/com/sun/xml/bind/v2/model/impl/ModelBuilder.class) needs 2.1 API. Use the endorsed directory mechanism to place jaxb-api.jar in the bootstrap classloader. (See http://java.sun.com/j2se/1.5.0/docs/guide/standards/)

This indicates that your JAX-WS version is newer than jaxb 2.0 and JAX-WS 2.0, and it is part of JDK 6 ..
To solve this problem, you only need$ Netbeans_home/java1/modules/EXT/jaxws21/API/Copy all jar files in$ Tomcat_install_dir/endorsed/(If the "endorsed" directory does not exist, you must create it ).

For more information, visit the http://wiki.netbeans.org/FaqEndorsedDirTomcat

Download

Netbeans 6 project: Download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.