SpringBoot與WebService的簡單實現

獻 醜發表於2020-11-20

SpringBoot與JAX-WS

服務端
依賴

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.4.1</version>
        </dependency>
        
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

介面

import javax.jws.WebService;
import java.util.List;

@WebService(targetNamespace = "http://localhost")
public interface UserService {

    void saveUser(User user);

    void updateUser(User user);

    void deleteUser(Integer id);

    List<User> findAllUser();

    User findById(Integer id);
}

假裝實現

@Service
public class UserServiceImpl implements UserService {
    @Override
    public void saveUser(User user) {
        System.out.println("儲存使用者:" + user.getId() + "----" + user.getName() + "----" + user.getSex());
    }

    @Override
    public void updateUser(User user) {
        System.out.println("修改使用者:" + user.getId() + "----" + user.getName() + "----" + user.getSex());
    }

    @Override
    public void deleteUser(Integer id) {
        System.out.println("刪除使用者:" + id);
    }

    @Override
    public List<User> findAllUser() {
        User user1 = new User(1,"1",1);
        User user2 = new User(2,"2",2);
        User user3 = new User(3,"2",3);

        List<User> users = new ArrayList<>();
        users.add(user1);
        users.add(user2);
        users.add(user3);

        return users;
    }

    @Override
    public User findById(Integer id) {

        return new User(1,"1",1);
    }
}


配置類

@Configuration
public class JaxWsConfig {

    @Autowired
    private Bus bus;

    @Autowired
    private UserService userService;

    @Bean
    public Endpoint createEndpoint(){
        Endpoint endpoint = new EndpointImpl(bus,userService);
        endpoint.publish("/userService");
        return endpoint;
    }
}

客戶端
沒有介面的實現類和釋出服務的功能,只有實體類和介面,並且與服務端一樣
訪問服務端:

	@Test
    void contextLoads() {
        //建立工廠物件
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        //設定訪問路徑
        factory.setAddress("http://localhost:8080/services/userService");
        //設定介面
        factory.setServiceClass(UserService.class);
        //建立介面的代理類
        UserService userService = (UserService) factory.create();
        //呼叫服務端介面
        userService.saveUser(new User(1l,"烏托邦",2));
    }

服務端列印輸出了證明成功

SpringBoot與JAX-RS

依賴

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>



        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
            <version>3.4.1</version>
        </dependency>

        <!--json轉換-->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-rs-extension-providers</artifactId>
            <version>3.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jettison</groupId>
            <artifactId>jettison</artifactId>
            <version>1.4.1</version>
        </dependency>

restful風格service,

public interface UserService {

    @POST
    void saveUser(User user);

    @PUT
    void updateUser(User user);

    @DELETE
    @Path("/{id}")
    void deleteUser(@PathParam("id") Integer id);

    @GET
    List<User> findAllUser();

    @GET
    @Path("/{id}")
    //轉json
    @Produces(MediaType.APPLICATION_JSON)
    User findById(@PathParam("id")Integer id);
}

實現類跟ws的一樣
配置類

@Configuration
public class JaxRsConfig {

    @Autowired
    private Bus bus;

    @Autowired
    private UserService userService;

    @Bean
    public Server createdServer(){
        JAXRSServerFactoryBean endpoint = new JAXRSServerFactoryBean();

        //設定訪問地址
        endpoint.setAddress("/userService");
        //設定bus
        endpoint.setBus(bus);
        //設定實體類物件
        endpoint.setServiceBean(userService);
        return endpoint.create();
    }

}

瀏覽器訪問
訪問

相關文章