Java - Publicando Servicio SOAP

 
Vista:

Publicando Servicio SOAP

Publicado por Edinson (4 intervenciones) el 16/06/2020 00:20:19
Buenas,
Tengo un proyecto Maven en el cual he implementado un servicio, pero tengo problemas al momento de publicarlo localmente.

Mis WebMethod tienen la siguiente definición:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package soap.aldaz.interfaces;
 
import java.util.List;
 
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
 
import soap.aldaz.entity.ProductoBean;
 
@WebService(name = "Producto")
public interface Producto {
 
	@WebMethod
	public void save(@WebParam(name = "producto") ProductoBean bean);
 
	@WebMethod
	public List<ProductoBean> filtrarXPrecio(@WebParam(name = "minimo") double minimo, @WebParam(name = "maximo") double maximo);
}

La implementación de dichos WebMethod tienen la siguiente forma:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package soap.aldaz.ws;
 
import java.util.List;
 
import javax.jws.WebService;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import soap.aldaz.entity.ProductoBean;
import soap.aldaz.interfaces.Producto;
import soap.aldaz.services.ProductoService;
 
@WebService(endpointInterface = "soap.aldaz.interfaces.Producto")
@Component
public class ProductoImpl implements Producto {
 
	@Autowired
	private ProductoService service;
 
 
	@Override
	public void save(ProductoBean bean) {
		service.save(bean);
	}
 
	@Override
	public List<ProductoBean> filtrarXPrecio(double minimo, double maximo) {
		return service.filtrarXPrecio(minimo, maximo);
	}
}
ProductoService es una interface cuya implementación realiza la conexión a la BD y realiza las operaciones correspondientes, es decir, en realidad son DAO's. Estoy tratando de inyectar dicho objeto (lo he hecho de manera similar cuando utilizo @AutoWired en una clase @Controller y se inyecta correctamente, pero es la primera vez que trato de inyectar en un @WebService

Ahora, pretendo registrar el servicio:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package soap.aldaz.ws;
 
import javax.xml.ws.Endpoint;
 
import org.springframework.beans.factory.annotation.Autowired;
import soap.aldaz.interfaces.Producto;
 
@Component
public class ProductoRegistro {
 
	@Autowired
	private static Producto svc;
 
	public static void main(String[] args) {
 
		String url = "http://localhost:8065/ealdaz/producto?wsdl";
 
		Endpoint ep = Endpoint.publish(url, svc);
 
		System.out.print(ep.isPublished());
	}
}
Cuando ejecuto esto último como Java Application, me arroja un NullPointerException debido a que el objeto svc no se está inyectado.
Por favor una mano, llevo horas dándole vueltas. Soy nuevo en Java generalmente me muevo en .Net.
Saludos y gracias.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Publicando Servicio SOAP

Publicado por Edinson (4 intervenciones) el 16/06/2020 13:25:10
Claro, pero si supuestamente lo hago de la siguiente manera utilizando la anotación @PostConstruct, sí debería inyectar, sin embargo, tampo me funciona
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package soap.aldaz.ws;
 
import javax.annotation.PostConstruct;
import javax.xml.ws.Endpoint;
 
import org.springframework.beans.factory.annotation.Autowired;
import soap.aldaz.interfaces.Producto;
 
public class ProductoRegistro {
 
	private static Producto component;
 
	@Autowired
	private Producto autowiredComponent;
 
	@PostConstruct
	private void init() {
		component = this.autowiredComponent;
	}
 
	public static void main(String[] args) {
 
		String url = "http://localhost:8065/ealdaz/producto?wsdl";
 
		Endpoint ep = Endpoint.publish(url, component);
 
		System.out.print(ep.isPublished());
	}
}
No podría hacerle new directamente a la clase ProductoImpl porque no se inyectaría su atributo ProductoService. Debe haber alguna forma.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Publicando Servicio SOAP

Publicado por Costero (148 intervenciones) el 16/06/2020 16:37:54
Lo que no veo es donde en tu codigo le estas diciendo a Spring que wire todos tus components.

Crea una clase, que va rastrear tu package para wire tus components

1
2
3
4
5
6
7
8
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
 
@Configuration
@ComponentScan("soap.aldaz.ws")
public class AppConfig {
 
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class ProductoRegistro {
 
 
	public static void main(String[] args) {
 
                AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
                Producto producto = ctx.getBean(ProductoImpl.class);
 
		String url = "http://localhost:8065/ealdaz/producto?wsdl";
 
		Endpoint ep = Endpoint.publish(url, producto);
 
		System.out.print(ep.isPublished());
	}
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Publicando Servicio SOAP

Publicado por Edinson (4 intervenciones) el 16/06/2020 21:01:26
Hola.
Gracias por responder. Parece ser que por ahí va la solución.

He implementado la clase AppConfig que mencionas y la he invocado de la siguiente manera en el main:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package soap.aldaz.ws;
 
import java.io.IOException;
import java.net.ServerSocket;
import javax.xml.ws.Endpoint;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 
import soap.aldaz.interfaces.Producto;
 
public class ProductoRegistro {
 
	private static AnnotationConfigApplicationContext ctx;
 
	public static void main(String[] args) throws IOException {
		ctx = new AnnotationConfigApplicationContext(AppConfig.class);
		Producto producto = ctx.getBean(ProductoImpl.class);
 
		ServerSocket ss = new ServerSocket(0);
 
		int port = ss.getLocalPort();
 
		ss.close();
 
		String url = "http://localhost:" + port + "/ealdaz/producto?wsdl";
 
		Endpoint ep = Endpoint.publish(url, producto);
 
		System.out.print(ep.isPublished());
	}
}
Sin embargo, ahora me tira otro error más extenso:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
jun 16, 2020 1:39:43 PM org.springframework.context.support.AbstractApplicationContext refresh
ADVERTENCIA: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productoDAOImpl': Unsatisfied dependency expressed through field 'sessionFactory'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.hibernate.SessionFactory' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'productoDAOImpl': Unsatisfied dependency expressed through field 'sessionFactory'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.hibernate.SessionFactory' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:116)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
	at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:89)
	at soap.aldaz.ws.ProductoRegistro.main(ProductoRegistro.java:15)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.hibernate.SessionFactory' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1695)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1253)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)
	... 14 more
Pareciera que el error empieza en el DAO. Mi clase de acceso a datos tiene la siguiente forma:

Interface:
1
2
3
4
5
6
7
8
9
10
11
12
package soap.aldaz.dao;
 
import java.util.List;
 
import soap.aldaz.entity.ProductoBean;
 
public interface ProductoDAO {
 
	public void save(ProductoBean bean);
 
	public List<ProductoBean> filtrarXPrecio(double minimo, double maximo);
}
Implementación:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package soap.aldaz.dao;
 
import java.util.List;
 
import javax.persistence.Query;
 
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import soap.aldaz.entity.ProductoBean;
 
@Repository
public class ProductoDAOImpl implements ProductoDAO {
 
	@Autowired
	private SessionFactory sessionFactory;
 
	@Override
	public void save(ProductoBean bean) {
		Session sesion = sessionFactory.getCurrentSession();
 
		try {
			sesion.saveOrUpdate(bean);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
 
	@SuppressWarnings("unchecked")
	@Override
	public List<ProductoBean> filtrarXPrecio(double minimo, double maximo) {
		Session sesion = sessionFactory.getCurrentSession();
		Query query = null;
 
		try {
			String hql = "select p from ProductoBean Where precio between ?1 and ?2";
			query = sesion.createQuery(hql);
			query.setParameter(1, minimo);
			query.setParameter(2, maximo);
		} catch (Exception e) {
			e.printStackTrace();
		}
 
		return query.getResultList();
	}
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Publicando Servicio SOAP

Publicado por Costero (148 intervenciones) el 17/06/2020 04:38:36
Le falta el SessionFactory Bean como dice el error.

En tu clase AppConfig an~adele esto haber si funciona. En quizas tenga que an~adir mas dependency a tu maven.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@Bean
public LocalSessionFactoryBean sessionFactory() {
   LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
   sessionFactory.setDataSource(restDataSource());
   sessionFactory.setPackagesToScan(
       new String[] { "soap.aldaz.ws" }
   );
   sessionFactory.setHibernateProperties(hibernateProperties());
 
   return sessionFactory;
}
 
@Bean
public DataSource restDataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("drivr"); // aqui pones los valores correspondientes a tu base de datos
    dataSource.setUrl("url"));
    dataSource.setUsername("uname");
    dataSource.setPassword("passwd");
    return dataSource;
}
 
Properties hibernateProperties() {
    return new Properties() {
        {
            setProperty("hibernate.hbm2ddl.auto", create);
            setProperty("hibernate.dialect", dielect_to_use);
        }
    };
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar