idea搭建spring boot 整合cxf开发webService
1.新建spring boot工程
2.创建实体类User
package com.lhw.vo; import java.io.Serializable; import java.util.Date; /** * Created by lin.hongwen@ztesoft.com * * @date 2018年04月22日 */ public class User implements Serializable{ private static final long serialVersionUID = -5939599230753662529L; private String userId; private String username; private String age; public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }
3.编写服务接口
package com.lhw.service; import com.lhw.vo.User; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; /** * Created by lin.hongwen@ztesoft.com * * @date 2018年04月23日 */ @WebService public interface UserService { @WebMethod public String getName (@WebParam(name = "userId") String userId); @WebMethod public User getUser(String userId); }
4.编写接口实现类
package com.lhw.service; import com.lhw.vo.User; import javax.jws.WebService; /** * Created by lin.hongwen@ztesoft.com * * @date 2018年04月24日 */
//注意 这里的targetNamespace 要以/结尾 否则会找不到方法 @WebService(targetNamespace = "http://service.lhw.com/", endpointInterface = "com.lhw.service.UserService") public class UserServiceImpl implements UserService{ @Override public String getName(String userId) { return "hello lhw:"+userId; } @Override public User getUser(String userId) { User user = new User(); user.setAge("24"); user.setUserId("100"); user.setUsername("linhw"); return user; } }
5.配置类CxfConfig
package com.lhw.config; import com.lhw.service.UserService; import com.lhw.service.UserServiceImpl; 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; /** * Created by lin.hongwen@ztesoft.com * * @date 2018年04月22日 */ @Configuration public class CxfConfig { @Bean public ServletRegistrationBean dispatcherServlet() { return new ServletRegistrationBean(new CXFServlet(), "/service/*"); } @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); } @Bean public UserService userService() { return new UserServiceImpl(); } @Bean public Endpoint endpoint() { EndpointImpl endpoint = new EndpointImpl(springBus(), userService()); endpoint.publish("/user"); return endpoint; } }
6.编写启动程序
package com.lhw; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Created by lin.hongwen@ztesoft.com * * @date 2018年04月22日 */ @SpringBootApplication public class start { public static void main(String[] args){ SpringApplication.run(start.class); } }
启动项目后。根据配置类的配置路径 这里是:http://localhost:8080/service/user?wsdl
即可看到wsdl信息

7.编写客户端代码
package com.lhw.service; import org.apache.cxf.endpoint.Client; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; /** * Created by lin.hongwen@ztesoft.com * 客户端调用代码 * @date 2018年04月25日 */ public class UserClient { public static void main(String[] args) throws Exception{ JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance(); Client client = clientFactory.createClient("http://localhost:8080/service/user?wsdl"); Object[] objects = client.invoke("getName","21"); System.out.println(objects[0].toString()); } }
运行main函数就可以看到结果
8.客户端代码2
@RequestMapping(value = "/test-bssWebService/{infName}", method = RequestMethod.POST) public void indexTest(HttpServletRequest request, @RequestBody String xml, @PathVariable String infName) { try { logger.info("----------------自测自己的webService服务端--------------------"+infName); String WSurl = "http://10.47.248.150:7008/iom-adapter/bss/BSSProcessSheet?wsdl"; org.apache.axis.client.Service service = new org.apache.axis.client.Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(new URL(WSurl)); call.setOperationName(infName); JSONObject jsonObject = JSONObject.parseObject(xml); int sheetType = Integer.valueOf(jsonObject.get("sheetType").toString()); int serviceType = Integer.valueOf(jsonObject.get("serviceType").toString()); String serialNo = jsonObject.get("serialNo").toString(); String serSupplier = jsonObject.get("serSupplier").toString(); String serCaller = jsonObject.get("serCaller").toString(); String callerPwd = jsonObject.get("callerPwd").toString(); String callTime = jsonObject.get("callTime").toString(); String attachRef = jsonObject.get("attachRef").toString(); String opPerson = jsonObject.get("opPerson").toString(); String opCorp = jsonObject.get("opCorp").toString(); String opDepart = jsonObject.get("opDepart").toString(); String opContact = jsonObject.get("opContact").toString(); String opTime = jsonObject.get("opTime").toString(); String opDetail = jsonObject.get("opDetail").toString(); String retStr = (String) call.invoke(new Object[]{sheetType, serviceType, serialNo, serSupplier, serCaller, callerPwd, callTime, attachRef, opPerson, opCorp, opDepart, opContact, opTime, opDetail}); System.out.print("------------------调用webService结果:"+retStr); } catch (Exception ex) { ex.printStackTrace(); } }

浙公网安备 33010602011771号