jax-rs的客户端完整实例

本地接口:

@Override
    public Response formsubGet(String accountContent, char inputContent,
            String opEntrustWay, String opStation, String sysName,String sdkType,String comsFunc,String password,
            char passwordType) {
        return this.formsubPost(accountContent, inputContent, opEntrustWay, opStation,sysName,sdkType,comsFunc, password, passwordType);
    }

@Override
    public Response formsubPost(String accountContent, char inputContent,
            String opEntrustWay, String opStation, String sysName, String sdkType,String comsFunc,String password,
            char passwordType) {
        Response loginAuth = null;

本地实现类:
public class AuthLoginServerImpl implements AuthLoginServer
{
    private static final Logger logger = Logger.getLogger(AuthLoginServerImpl.class);
    
    private AuthLoginClient authLoginClient;

@Override
    public Response formsubGet(String accountContent, char inputContent,
            String opEntrustWay, String opStation, String sysName,String sdkType,String comsFunc,String password,
            char passwordType) {
        return this.formsubPost(accountContent, inputContent, opEntrustWay, opStation,sysName,sdkType,comsFunc, password, passwordType);
    }

@Override
    public Response formsubPost(String accountContent, char inputContent,
            String opEntrustWay, String opStation, String sysName, String sdkType,String comsFunc,String password,
            char passwordType) {
        Response loginAuth = null;
        try {
            loginAuth = authLoginClient.login(accountContent, inputContent, opEntrustWay, opStation, sysName,sdkType,comsFunc,password, passwordType);
        } catch (Exception e) {
             logger.info("调用login认证异常:"+ e.getMessage());
             loginAuth.setRetCode("-1");
             loginAuth.setRetMessage("login方法认证失败!");
        }
        return loginAuth;
    }

public void setAuthLoginClient(AuthLoginClient authLoginClient) {
        this.authLoginClient = authLoginClient;
    }
    
}

客户端类:
@Path("/mds")
@Produces("application/json;charset=UTF-8")
public interface AuthLoginClient
{
    @POST
    @Consumes({ MediaType.APPLICATION_FORM_URLENCODED + ";charset=UTF-8",
            MediaType.MULTIPART_FORM_DATA + ";charset=UTF-8" })
    @Path("/formsub.json")
    /*
     * F_OP_SRC首字母大写,无法将所有参数进行封装处理,因为对象属性是通过geter获取,结果得到的属性名为f_OP_SRC
     */
    public Response login(@FormParam("AccountContent") String accountContent, @FormParam("InputContent") char inputContent,
            @FormParam("OpEntrustWay") String opEntrustWay, @FormParam("OpStation") String opStation,
            @FormParam("sys_name") String sysName, @FormParam("sdk_type") String sdkType,
            @FormParam("coms_func") String comsFunc,@FormParam("Password") String password,
            @FormParam("PasswordType") char passwordType);
}

xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://cxf.apache.org/jaxrs
    http://cxf.apache.org/schemas/jaxrs.xsd">
    
    <!-- 资源文件配置 -->
    <bean id="propertiesBean"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <!-- 不需要加载log4j.properties -->
                <value>classpath:global.properties</value>
            </list>
        </property>
        <property name="fileEncoding">
            <value>UTF-8</value>
        </property>
    </bean>

<jaxrs:client id="authComClient"
        serviceClass="com.citics.esb.rest.client.AuthComClient"
        address="${url}">
        <jaxrs:features>
            <bean class="org.apache.cxf.feature.LoggingFeature" />
        </jaxrs:features>
        <jaxrs:providers>
            <bean class="org.apache.cxf.jaxrs.provider.json.JSONProvider">
                <property name="dropRootElement" value="true" /><!--默认false-->
                <property name="supportUnwrapped" value="true" /><!--默认false-->
            </bean>
        </jaxrs:providers>
    </jaxrs:client>
    
    
    <jaxrs:server id="cutIdtiByPassServer" address="/identifications/jsonapi">
        <jaxrs:serviceBeans>
            <bean class="com.citics.esb.rest.server.impl.AuthComServerImpl">
                <property name="authComClient" ref="authComClient"></property>
            </bean>
        </jaxrs:serviceBeans>
        <jaxrs:features>
            <bean class="org.apache.cxf.feature.LoggingFeature" />
        </jaxrs:features>
        <jaxrs:providers>
            <bean class="org.apache.cxf.jaxrs.provider.json.JSONProvider">
                <property name="ignoreNamespaces" value="true" />
                <property name="dropRootElement" value="false" />
            </bean>
        </jaxrs:providers>
    </jaxrs:server>

<jaxrs:client id="authLoginClient"
        serviceClass="com.citics.esb.rest.client.AuthLoginClient"
        address="${loginUrl}">
        <jaxrs:features>
            <bean class="org.apache.cxf.feature.LoggingFeature" />
        </jaxrs:features>
        <jaxrs:providers>
            <bean class="org.apache.cxf.jaxrs.provider.json.JSONProvider">
                <property name="dropRootElement" value="true" /><!--默认false-->
                <property name="supportUnwrapped" value="true" /><!--默认false-->
            </bean>
        </jaxrs:providers>
    </jaxrs:client>
    
    <jaxrs:server id="formsubServer" address="/mds">
        <jaxrs:serviceBeans>
            <bean class="com.citics.esb.rest.server.impl.AuthLoginServerImpl">
                <property name="authLoginClient" ref="authLoginClient"></property>
            </bean>
        </jaxrs:serviceBeans>
        <jaxrs:features>
            <bean class="org.apache.cxf.feature.LoggingFeature" />
        </jaxrs:features>
        <jaxrs:providers>
            <bean class="org.apache.cxf.jaxrs.provider.json.JSONProvider">
                <property name="ignoreNamespaces" value="true" />
                <property name="dropRootElement" value="false" />
            </bean>
        </jaxrs:providers>
    </jaxrs:server>
    
</beans>
返回的Response类:
@XmlRootElement(name = "Response")
@XmlAccessorType(XmlAccessType.NONE)
public class Response
{
    @XmlElement(name = "RET_CODE")
    private String retCode;
    
    @XmlElement(name = "RET_MESSAGE")
    private String retMessage;
    
    @XmlElement(name = "RESULTSET")
    private ResultSet resultSet;
    
    
    public String getRetCode()
    {
        return retCode;
    }
    
    
    public void setRetCode(String retCode)
    {
        this.retCode = retCode;
    }
    
    
    public String getRetMessage()
    {
        return retMessage;
    }
    
    
    public void setRetMessage(String retMessage)
    {
        this.retMessage = retMessage;
    }
    
    
    public ResultSet getResultSet()
    {
        return resultSet;
    }
    
    
    public void setResultSet(ResultSet resultSet)
    {
        this.resultSet = resultSet;
    }
    
}

@XmlAccessorType(XmlAccessType.FIELD)
public class ResultSet
{
    @XmlElement(name = "Account")
    private String account;
    
    @XmlElement(name = "BranchNo ")
    private String branchNo ;
    
    @XmlElement(name = "ClientID ")
    private String clientID ;
    
    @XmlElement(name = "SysnodeId")
    private int sysnodeId;
    
    public String getAccount() {
        return account;
    }
    public void setAccount(String account) {
        this.account = account;
    }
    public String getBranchNo() {
        return branchNo;
    }
    public void setBranchNo(String branchNo) {
        this.branchNo = branchNo;
    }
    public String getClientID() {
        return clientID;
    }
    public void setClientID(String clientID) {
        this.clientID = clientID;
    }
    public int getSysnodeId() {
        return sysnodeId;
    }
    public void setSysnodeId(int sysnodeId) {
        this.sysnodeId = sysnodeId;
    }
}

上一篇:Java反射机制DOME


下一篇:Linux文件管理常用命令