java实现webservice实例

 web.xml





<!-- webservice -->
	<servlet>
		<servlet-name>CXFServlet</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>CXFServlet</servlet-name>
		<url-pattern>/webservice/*</url-pattern>
	</servlet-mapping>

webservice.xml

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:jaxws="http://cxf.apache.org/jaxws"  
    xsi:schemaLocation="  
        http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans.xsd  
        http://cxf.apache.org/jaxws  
        http://cxf.apache.org/schemas/jaxws.xsd">  
    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
  
    <!--下面的class属性值一定要跟你项目中服务实现类的包路径完全一致-->

    <bean id="service" class="*.*.*.HxWebServiceImpI"/>
    <jaxws:endpoint id="webService" implementor="#service" address="/HxWebService" />
</beans> 

 

HxWebService.java
@WebService
public interface HxWebService {
    public String GetXQZSRecord(@WebParam(name="ordId") String ordId); 
}
HxWebServiceImpI.java

//webservice访问名称

@WebService(endpointInterface = "*.interfaces.HxWebService", serviceName = "GetXQZSRecord")
public class HxWebServiceImpI implements HxWebService {

 private String resultXml;
    private static Properties props = new Properties();
    private static final org.apache.commons.logging.Log LOG = LogFactory.getLog(HxWebServiceImpI.class);

public String GetXQZSRecord(String ordId) {
        /*Document dom = null;
		try {
			dom = DocumentHelper.parseText(ordId);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Element root = dom.getRootElement();
		String orderId = root.element("OrdId").getText();*/
        // 通过orderId查询
        UserTestItems item = new UserTestItems();
        try {
            props.load(HxWebServiceImpI.class.getResourceAsStream("/jdbc.properties"));
            final String url = props.getProperty("jdbc.url");
            final String name = props.getProperty("jdbc.driver");
            final String user = props.getProperty("jdbc.username");
            final String password = props.getProperty("jdbc.password");
            Connection conn = null;
            Statement statement = null;
            Statement statement1 = null;
            Statement statement2 = null;
            ResultSet rs = null;
            ResultSet rs1 = null;
            ResultSet rs2 = null;
            Class.forName(name);// 指定连接类型
            conn = DriverManager.getConnection(url, user, password);// 获取连接


            if (conn != null) {
                System.out.println("获取连接成功");

                statement = conn.createStatement();
                String sql = "select * from user_test_items_t where order_id='" + ordId + "'";
                rs = statement.executeQuery(sql);
                //System.out.println(rs.first());
                if (!rs.next()) {
                    // 订单号不存在
                    resultXml = "<Response><ResultCode>E</ResultCode><Message>orderId:"
                            + ordId + "不存在</Message></Response>";
                } else {
                    String testStatus= rs.getString("test_status");
                    if(testStatus =="INIT"){
                        resultXml = "<Response><ResultCode>F</ResultCode><Message>orderId:"
                                + ordId + "用户没有测评信息</Message></Response>";
                        return  resultXml;
                    }
                    if(testStatus=="OVERDUE"){
                        resultXml = "<Response><ResultCode>H</ResultCode><Message>orderId:"
                                + ordId + "用户已过期</Message></Response>";
                        return  resultXml;
                    }
                    long id = Long.parseLong(rs.getString("id"));
                    long questionId = Long.parseLong(rs.getString("question_id"));
                    double version = Double.parseDouble(rs.getString("version"));
                    item.setReportAt(rs.getDate("test_at"));
                    String answer =null;
                    String genescore =null;
                    if(id!=0){
                        String sql1 = "select * from user_test_answer where item_id='" + id + "'";
                        statement1 = conn.createStatement();
                        rs1 = statement1.executeQuery(sql1);
                        if(rs1.next()){
                            answer = rs1.getString("answer");
                        }
                        String sql2 = "select * from user_test_genescore where item_id='" + id + "'";
                        statement2 = conn.createStatement();
                        rs2 = statement2.executeQuery(sql2);
                        if(rs2.next()){
                            genescore = rs2.getString("genescore");
                        }
                    }
                   

//这个是xml最后的返回格式自由拼接
                        resultXml = "<Response>" +
                                "<ResultCode>S</ResultCode>" +
                                "<RecordDate>" + rs.getString("test_at").split(" ")[0] + "</RecordDate>" +
                                "<RecordTime>" + rs.getString("test_at").split(" ")[1] + "</RecordTime>" +
                                "<TotalScore>" + TotalScore + "</TotalScore>" +
                                "<NinthScore>" + NinthScore + "</NinthScore>" +
                                "<Comment>" + Comment + "</Comment>" +
                                "<Recommend>" + Recommend + "</Recommend>" +
                                "<RecordList>" + questions + "</RecordList>" +
                                "</Response>";
                    }
                }
                rs.close();
                statement.close();
                conn.close();
            } else {
                System.out.println("获取连接失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        LOG.error("resultXml:="+resultXml);
        return resultXml;
    }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值