white knight

导航

 

参考 https://www.cnblogs.com/fuxin41/p/6289162.html

 

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.xx</groupId>
    <artifactId>webservice</artifactId>
    <version>0.0.1-dev-SNAPSHOT</version>
    <packaging>war</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.3.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc7</artifactId>
            <version>12.1.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-oracle</artifactId>
            <version>1.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.11</version>
        </dependency>
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.1.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

 

WebServiceConfig.java

package com.xx.ws.config;

import com.xx.ws.service.UserService;
import com.xx.ws.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;

@Configuration
public class WebServiceConfig {
    @Bean
    public ServletRegistrationBean dispatcherServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/ws/*");
    }

    @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("/userService");
        return endpoint;
    }
}

 

UserService.java

package com.xx.ws.service;

import com.xx.ws.entity.DataEntity;
import com.xx.ws.entity.ResponseEntity;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import java.util.List;

@WebService
public interface UserService {
    @WebMethod
    ResponseEntity createUser(@WebParam(name="data") DataEntity data);

    @WebMethod
    ResponseEntity updateUser(@WebParam(name="guid") String guid, @WebParam(name="data") DataEntity data);

    @WebMethod
    ResponseEntity deleteUser(@WebParam(name="guid") String guid);

    @WebMethod
    List<DataEntity> queryByIds(@WebParam(name="ids") List<String> ids);
}

 

UserServiceImpl.java

package com.xx.ws.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.jws.WebService;

import com.xx.ws.SecDao;
import com.xx.ws.entity.DataEntity;
import com.xx.ws.entity.ResponseEntity;
import com.xx.ws.util.RsaUtil;
import org.springframework.beans.factory.annotation.Autowired;

@WebService(endpointInterface="com.xx.ws.service.UserService",
targetNamespace="http://service.ws.xx.com/")
public class UserServiceImpl implements UserService {
    @Autowired
    private SecDao secDao;

    public ResponseEntity createUser(DataEntity data) {
        Map<String, String> attrs = data.getAttrs();
        String userId = attrs.get("userId");
        String roleId = attrs.get("roleId");

        String uuid = getUUID();

        ResponseEntity resp;
        boolean exi = secDao.exist(userId);
        boolean roleexi = secDao.existRole(roleId);
        if (exi) {
            resp = new ResponseEntity("用户已存在", "300", uuid, false);
        } else if (!roleexi) {
            resp = new ResponseEntity("不存在该权限", "300", uuid, false);
        } else {
            secDao.save(userId, attrs.get("userName"), attrs.get("password"), roleId);
            resp = new ResponseEntity("success!!!", "200", uuid, true);
        }

        return resp;
    }

    public ResponseEntity updateUser(String guid, DataEntity data) {
        Map<String, String> attrs = data.getAttrs();
        String userId = guid;
        String roleId = attrs.get("roleId");

        String uuid = getUUID();

        ResponseEntity resp;
        boolean exi = secDao.exist(userId);
        boolean roleexi = secDao.existRole(roleId);

        if (!exi) {
            resp = new ResponseEntity("不存在该用户", "300", uuid, false);
        } else if (!roleexi) {
            resp = new ResponseEntity("不存在该权限", "300", uuid, false);
        } else {
            secDao.update(userId, attrs.get("userName"), attrs.get("password"), roleId);
            resp = new ResponseEntity("success!!!", "200", uuid, true);
        }

        return resp;
    }

    public ResponseEntity deleteUser(String guid) {
        ResponseEntity resp;
        String uuid = getUUID();
        boolean exi = secDao.exist(guid);
        if (!exi) {
            resp = new ResponseEntity("不存在该用户", "300", uuid, false);
        } else {
            secDao.delete(guid);
            resp = new ResponseEntity("success!!!", "200", uuid, true);
        }

        return resp;
    }// queryByIds方法
    public List<DataEntity> queryByIds(List<String> ids) {
        List<Map<String, Object>> objectDatas = secDao.getByIds(ids);

        List<DataEntity> datas = new ArrayList<DataEntity>();
        for (int i = 0; i < objectDatas.size(); i++) {
            Map<String, String> map = new HashMap<String, String>();
            Map<String, Object> tmp = objectDatas.get(i);

            map.put("userId", tmp.get("userId") + "");
            map.put("userName", tmp.get("userName") + "");
            map.put("roleId", tmp.get("roleId") + "");

            DataEntity data = new DataEntity();
            data.setAttrs(map);

            datas.add(data);
        }

        return datas;
    }

    private String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }
}

 

Application.java

package com.xx.ws;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

 

ServletInitializer.java  如果需要打成war包,增加此文件

package com.xx.ws;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

/**
 * Created by Administrator on 2017/11/14.
 */
public class ServletInitializer extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
}

 

posted on 2018-03-13 13:45  white knight  阅读(524)  评论(0编辑  收藏  举报