IntelliJ IDEA java开发 WebService
测试的功能是通过访问solr服务器,获取相关信息,然后以webservice的形式供客户端访问来获取数据。通过查询资料,现在把过程记录如下:
1、环境
IntelliJ IDEA 2017.2.6
JAVA 1.8
Tomcat 9.0.0.M26
2、新建web服务项目
在src下面建立web_test包,在下面建立类SolrData
修改配置:
增加SolrData类的配置
完善SolrData类里面的代码:
import java.io.IOException; import java.util.List; import org.apache.solr.client.solrj.SolrClient; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.impl.HttpSolrClient; import org.apache.solr.client.solrj.response.Cluster; import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.client.solrj.response.ClusteringResponse; import org.apache.solr.common.SolrDocumentList; /** * @Author:sks * @Description: * @Date:Created in 14:37 2018/2/1 * @Modified by: **/ public class SolrData { public String getData(String id){ System.out.println("hello:"+id); // getTxtFromTextFile(id); String urlString = "http://localhost:8983/solr/test"; SolrClient solr = new HttpSolrClient.Builder(urlString).build(); System.out.println("solr"); //使用这个对象做查询 SolrQuery params = new SolrQuery(); //查询所有数据 params.set("qt", "/clustering"); params.setQuery("*:*"); params.setStart(0); params.setRows((int)getDocTotalCount(solr)); try { QueryResponse queryResponse = solr.query(params); ClusteringResponse clr = queryResponse.getClusteringResponse(); List<Cluster> list = clr.getClusters(); //拿到聚类数据集合,返回查询结果 String txt = ""; for (Cluster c : list) { //类别标签 List<String> lblist = c.getLabels(); for (String lb : lblist) { System.out.println(lb); } //聚类文档ID List<String> doclist = c.getDocs(); for (String doc : doclist) { System.out.println(" " + doc); } } } catch (SolrServerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return "getdata"; } /** * @Author:sks * @Description:获取文档总数 * @Date: */ private long getDocTotalCount(SolrClient solr ) { long num = 0; try { SolrQuery params = new SolrQuery(); params.set("q", "*:*"); //params.setQuery("*:*"); QueryResponse rsp = solr.query(params); SolrDocumentList docs = rsp.getResults(); num = docs.getNumFound(); } catch (SolrServerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return num; } }
引入jar,提取准备好所有的jar包,放在项目的lib文件夹下
同时对libraries也引用上面的jar包
配置tomcat
配置Artifacts
运行服务
在浏览器输入:http://localhost:8080/services
生成wsdl,需要注意的是,在生成wsdl之前,一定要先配置好tomcat先运行以下,保证浏览器上能访问在生成wsdl
3、web服务客户端
删除example包,直接在client包下面建client_test代码如下:
package client; import client.SolrDataServiceLocator; import client.SolrData_PortType; import javax.xml.rpc.ServiceException; import java.rmi.RemoteException; /** * @Author:sks * @Description: * @Date:Created in 14:58 2018/2/1 * @Modified by: **/ public class client_test { public static void main(String[] argv){ try { SolrDataServiceLocator sdl = new SolrDataServiceLocator(); SolrData_PortType service = sdl.getSolrData(); String result = service.getData("1234"); System.out.println(result); }catch (ServiceException e) { e.printStackTrace(); } catch (RemoteException e) { e.printStackTrace(); } } }
4、测试
启动solr_web服务
启动solr_web_clent客户端
服务端输出结果: