1.1.40. fejezet, SOAP szolgáltatás

Kapcsolódó hivatkozások

Mintakód

WSDL automatikusan generálódik az annotációk szerint (http://localhost:8080/Homework4SOAPServer/NewWebService?wsdl).

NewWebService.java

package hu.infokristaly.homework4soapserver;
 
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.sql.DataSource;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;
 
/**
 *
 * @author pzoli
 */
@WebService(serviceName = "NewWebService")
public class NewWebService {
    @Inject
    private HelloService helloService;
 
    @Resource(lookup = "jdbc/sample")
    private DataSource dataSource;
 
    @Resource
    WebServiceContext wsctx;
 
    @WebMethod(operationName = "helloWorld")
    public String helloWorld() {
        return "Hello World";
    }
 
    /**
     * This is a sample web service operation
     */
    @WebMethod(operationName = "hello")
    public String hello(@WebParam(name = "name") String txt) {
        String passwd = null;
        String userName = null;
        if (dataSource != null) {
            try {
                Connection conn = dataSource.getConnection();
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(NewWebService.class.getName()).log(Level.SEVERE, null, ex);
            }
            MessageContext mctx = wsctx.getMessageContext();
            Map http_headers = (Map) mctx.get(MessageContext.HTTP_REQUEST_HEADERS);
            if (http_headers != null) {
                List<String> userList = (List<String>) http_headers.get("Username");
                List<String> passList = (List<String>) http_headers.get("Password");
                if (passList != null && passList.size() > 0) {
                    passwd = passList.get(0);
                }
                if (userList != null && userList.size() > 0) {
                    userName = userList.get(0);
                }
            }
        }
        HelloFile entity = new HelloFile();
        entity.setName(txt);
        helloService.persist(entity);
        return "Hello " + txt + "[" + userName + ":" + passwd + "]" + " !";
    }
}

HelloService.java

package hu.infokristaly.soap;
 
import javax.ejb.Stateless;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
 
@Named
@Stateless
public class HelloService {
 
	@PersistenceContext(unitName="primary")
	private EntityManager em;
 
	public void persist(HelloFile file) {
		em.persist(file);
	}
}

pom.xml-be hozzá kell adni a függőségekhez:

		<dependency>
			<groupId>javax.jws</groupId>
			<artifactId>javax.jws-api</artifactId>
			<version>1.1</version>
		</dependency>
 
		<dependency>
			<groupId>javax.xml.ws</groupId>
			<artifactId>jaxws-api</artifactId>
			<version>2.2</version>
		</dependency>

A Wildfly standalone.xml-be be kell állítani a wsdl-host és wsdl-uri-scheme paramétert:

        <subsystem xmlns="urn:jboss:domain:webservices:2.0" statistics-enabled="${wildfly.webservices.statistics-enabled:${wildfly.statistics-enabled:false}}">
            <wsdl-host>192.168.1.167</wsdl-host>
            <wsdl-uri-scheme>https</wsdl-uri-scheme>
...
        </subsystem>