Spring Boot + Apache CXF. Publi sh конечные точки только с аннотациями - PullRequest
1 голос
/ 04 августа 2020

Согласно официальной документации Apache CXF, мы создаем SOAP сервисов весной таким образом:

Класс конфигурации

import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

@Configuration
public class WebServiceConfiguration {

    private static final String BINDING_URI = "http://www.w3.org/2003/05/soap/bindings/HTTP/";

    @Bean
    public ServletRegistrationBean<CXFServlet> disServlet() {
        return new ServletRegistrationBean<>(new CXFServlet(), "/soap-api/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        SpringBus bus = new SpringBus();
        bus.setProperty("org.apache.cxf.stax.maxTextLength", 1024 * 1024 * 1024);
        return bus;
    }

    @Bean
    public Endpoint userServiceEndpoint(UserService userService) {
        EndpointImpl endpoint = new EndpointImpl(springBus(), userService);
        endpoint.setBindingUri(BINDING_URI);
        endpoint.publish("/users");
        return endpoint;
    }

}

Сгенерировано из интерфейса WSDL SOAP:

/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.3.2
 * Generated source version: 2.2
 * 
 */
@WebService(name = "UserServiceSoap", targetNamespace = "http://User.no/webservices/")
@XmlSeeAlso({ObjectFactory.class})
public interface UserServiceSoap {

    @WebMethod(operationName = "GetUser", action = "http://User.no/webservices/GetUser")
    @WebResult(name = "GetUserResult", targetNamespace = "http://User.no/webservices/")
    @RequestWrapper(localName = "UserServiceSoap", targetNamespace = "http://User.no/webservices/", className = "no.user.webservices.generated.UserServiceSoap")
    @ResponseWrapper(localName = "UserServiceSoapResponse", targetNamespace = "http://User.no/webservices/", className = "no.user.webservices.generated.UserServiceSoapResponse")
    public String getUser(@WebParam(name = "username", targetNamespace = "http://User.no/webservices/") String username);       

}

Реализация услуги:

@Service
@WebService(
        endpointInterface = "no.altinn.webservices.generated.UserServiceSoap",
        serviceName = "UserServiceSoapService",
        targetNamespace = "http://User.no/webservices/",
        portName = "UserServiceSoapPort"
)
@BindingType("http://java.sun.com/xml/ns/jaxws/2003/05/soap/bindings/HTTP/")
public class UserService implements UserServiceSoap {

    @Override
    public String getUser(String requestUsername) {
        //logic
    }

}

Что я хочу?
Есть ли уже реализованный способ в конечную точку publi sh SOAP без создания bean-компонента в конфигурации Spring. Я хочу сделать это с аннотацией реализации сервиса, например (@SoapEndpoint):

    @Service
    @SoapEndpoint(bindingUri = "http://www.w3.org/2003/05/soap/bindings/HTTP/", 
                  publish = "/users")                      
    @WebService(
            endpointInterface = "no.altinn.webservices.generated.UserServiceSoap",
            serviceName = "UserServiceSoapService",
            targetNamespace = "http://User.no/webservices/",
            portName = "UserServiceSoapPort"
    )
    @BindingType("http://java.sun.com/xml/ns/jaxws/2003/05/soap/bindings/HTTP/")
    public class UserService implements UserServiceSoap {
    
        @Override
        public String getUser(String requestUsername) {
            //logic
        }
    
    }

1 Ответ

1 голос
/ 04 августа 2020

Временное решение: Создайте пустой интерфейс с именем SoapService и @SoapEndpoint и инициализируйте endpoints вручную

Интерфейс SoapService:

public interface SoapService {

}

@ Интерфейс SoapEndpoint:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface SoapEndpoint {

    String bindingUri() default "http://www.w3.org/2003/05/soap/bindings/HTTP/";
    
    String publish();
}

Инициализировать конечные точки вручную

@Configuration
public class WebServiceConfiguration {

    private static final String BINDING_URI = "http://www.w3.org/2003/05/soap/bindings/HTTP/";

    @Autowired
    private List<SoapService> endpoints;

    @Bean
    public ServletRegistrationBean<CXFServlet> disServlet() {
        return new ServletRegistrationBean<>(new CXFServlet(), "/soap-api/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        SpringBus bus = new SpringBus();
        bus.setProperty("org.apache.cxf.stax.maxTextLength", 1024 * 1024 * 1024);
        return bus;
    }

    @PostConstruct
    public void init() {
        for (SoapService bean : endpoints) {
            if (bean.getClass().getAnnotation(SoapEndpoint.class) == null) {
                throw new IllegalArgumentException("Missed @SoapEndpoint for " + bean.getClass().getName());
            }
            EndpointImpl endpoint = new EndpointImpl(springBus(), bean);
            endpoint.setBindingUri(BINDING_URI);
            endpoint.publish(bean.getClass().getAnnotation(SoapEndpoint.class).publish());
        }
    }
}

Добавить SoapService в классы реализации

@Service
@SoapEndpoint(publish = "/users")
@WebService(endpointInterface = "no.altinn.webservices.generated.UserServiceSoap", serviceName = "UserServiceSoapService", targetNamespace = "http://User.no/webservices/", portName = "UserServiceSoapPort")
@BindingType("http://java.sun.com/xml/ns/jaxws/2003/05/soap/bindings/HTTP/")
public class UserService implements UserServiceSoap, SoapService {

    @Override
    public String getUser(String requestUsername) {
        //logic
    }

}

--- Результат (я добавил OrganitionService который реализует OrganizationServiceSoap) ---

Soap результат

...