1

I have a Jax-Ws Metro project with Spring container. I cannot autowire a field inside SoapHandler. I tried everything from internet resources but no success. The field is always null. Field name is "paymentPortalService".

My Handler class:

public class CustomSoapHeaderHandler extends SpringBeanAutowiringSupport implements SOAPHandler<SOAPMessageContext> {
    private static final Logger log = Logger.getLogger(CustomSoapHeaderHandler.class);

    @Autowire
    private PaymentPortalService paymentPortalService;
}

I have a proxy service which calls a remote service. Added this handler to handlerchain in remote service call method:

@Service
public class CustomServiceProxy {
    private final Logger log = Logger.getLogger(CustomServiceProxy.class);
    private RemoteServicePort pspPort;


    public CustomServiceProxy() {
        try {


        pspPort = new RemoteService_Service(new URL("https://x?wsdl")).getRemoteServicePort();

        Binding binding = ((BindingProvider) pspPort).getBinding();
        List<Handler> handlers = binding.getHandlerChain();
        handlers.add(new CustomSoapHeaderHandler());
        binding.setHandlerChain(handlers);
    }
}

I tried to change @Autowire to @Resource and some other solutions from internet, no success. I am using Apache Tomcat 8 without EE container.

Thanks in advance!

0bj3ct
  • 1,400
  • 4
  • 22
  • 51
  • And why should it not be `null`. You are creating instances of the `CustomSoapHeaderHandler` yourself. It isn't managed by any kind of container be it jersey or Spring. So nothing will be injected. Instead of hacking I suggest a read of the jersey documentation which explains pretty clear how to setup jersey with Spring. – M. Deinum Aug 04 '16 at 12:53
  • Thanks for your reply! Yes, I know this is a silly mistake, I changed it to: Autowired private CustomSoapHeaderHandler customSoapHeaderHandler; But it still does not work. The handler object is null. I also added Component annotation to the CustomSoapHeaderHandler class. – 0bj3ct Aug 04 '16 at 13:05
  • As mentioned read [the documentation](https://jersey.java.net/documentation/latest/spring.html) instead of trying the wrong things. Next to that your `SoapHandler` needs to be a `@Component` and I assume you have setup component-scanning. (And please remove the extends `SpringBeanAutowiringSupport` as that adds nothing. – M. Deinum Aug 04 '16 at 13:08
  • This is not a Jersey, but a Metro project. Can you please provide a documentation showing Client side handler with Spring DI in Jax-Ws (Metro)? Component scan is working fine for other classes in the same package. – 0bj3ct Aug 04 '16 at 13:10
  • You are only using Jax-WS to construct a client to call the web service I assume? The rest is Spring? If so then instead of manually configuring things in java, use the [`JaxWsPortProxyFactoryBean`](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/remoting/jaxws/JaxWsPortProxyFactoryBean.html) to do the configuration. However your main issue simply is that your handler isn't a spring managed bean, just make it one. – M. Deinum Aug 04 '16 at 13:12
  • Thanks for your help, I will try. – 0bj3ct Aug 04 '16 at 13:22

1 Answers1

-1
Add this jar from ivy

<dependency org="org.springframework" name="spring-aop" rev="4.2.1.RELEASE" transitive="false"/>
<dependency org="org.springframework" name="spring-beans" rev="4.2.1.RELEASE" transitive="false"/>
<dependency org="org.springframework" name="spring-context" rev="4.2.1.RELEASE" transitive="false"/>
<dependency org="org.springframework" name="spring-core" rev="4.2.1.RELEASE" transitive="false"/>
<dependency org="org.springframework" name="spring-expression" rev="4.1.1.RELEASE" transitive="false"/>
<dependency org="org.springframework" name="spring-web" rev="4.2.1.RELEASE" transitive="false"/>
<dependency org="org.springframework" name="spring-webmvc" rev="4.2.1.RELEASE" transitive="false"/>
<dependency org="org.springframework" name="spring-jdbc" rev="4.2.5.RELEASE" transitive="false"/>
<dependency org="org.springframework" name="spring-tx" rev="4.2.5.RELEASE" transitive="false"/>
<dependency org="com.fasterxml.jackson.core" name="jackson-annotations" rev="2.7.4" transitive="false"/>
<dependency org="com.fasterxml.jackson.core" name="jackson-core" rev="2.7.4" transitive="false"/>
<dependency org="com.fasterxml.jackson.core" name="jackson-databind" rev="2.7.4" transitive="false"/>
<dependency org="mysql" name="mysql-connector-java" rev="5.1.39" transitive="false"/>
<dependency org="org.apache.commons" name="commons-dbcp2" rev="2.1.1" transitive="false"/>
<dependency org="org.apache.commons" name="commons-pool2" rev="2.4.2" transitive="false"/>

A response object bean looks like

@Component
public class ResponseObjectBean {
    @JsonProperty("code")
    private int statusCode;
    @JsonProperty("msg")
    private String statusMsg;
    @JsonProperty("data")
    private Object data;

    //getter and setter 
}

A log in controller

@RestController
public class LogInController {

    @Autowired
    ResponseObjectBean responseData;

    @Autowired
    LogInService logInService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)

    public ResponseEntity<ResponseObjectBean> userLogIn(@RequestBody UserInfoBean userInfoBean) {
        Object data = null;
        String msg = null;
        int statusCode;
        try {
            data = logInService.logIn(userInfoBean);

            if (data != null) {
                msg = "success";
                statusCode = 200;
            } else {
                msg = "fail";
                statusCode = 503;
            }
        } catch (DataAccessException e) {

                msg = "fail";
                statusCode = 503;
        } catch (Exception e) {

                msg = "fail";
                statusCode = 503;
            e.printStackTrace();
        }

        responseData.setStatusCode(statusCode);
        responseData.setStatusMsg(msg);
        responseData.setData(data);
        return new ResponseEntity<ResponseObjectBean>(responseData, HttpStatus.OK);

    }

}


A log in Service class looks like

@Service
@Transactional
public class LogInService {

    @Autowired
    LogInDao logInDao;

    public UserInfoBean logIn(UserInfoBean userInfoBean) throws DataAccessException {
        return logInDao.upSertUser(userInfoBean);

    }

}


A log in dao class
@Repository
public class LogInDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public UserInfoBean upSertUser(UserInfoBean userInfoBean) throws Exception {
        StringBuilder sql = new StringBuilder();
        int count;
        UserInfoBean infoBean = null;

        try {
            // your logic for db operation
                return infoBean;
            }
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            sql = null;
            infoBean = null;
            run = null;
        }

    }

}

for use jdbc templete add these line in your spring.xml

    <!-- jdbcTemplate uses dataSource -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- dataSource configuration -->
    <bean id="dataSource" destroy-method="close"
        class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="your driverClassName" />
        <property name="url" value="your url" />
        <property name="username" value="db username" />
        <property name="password" value="db password" />
    </bean>