首先看web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
         version="3.1"> 
    <servlet> 
        <servlet-name>solr_service</servlet-name> 
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> 
 
        <init-param> 
            <param-name>jersey.config.server.provider.packages</param-name> 
            <param-value>com.solr.service</param-value> 
        </init-param> 
        <load-on-startup>1</load-on-startup> 
    </servlet> 
 
    <servlet-mapping> 
        <servlet-name>solr_service</servlet-name> 
        <url-pattern>/api/*</url-pattern> 
    </servlet-mapping> 
</web-app>

  

  1、无参数

package com.solr.service; 
import javax.ws.rs.*; 
 
import javax.ws.rs.core.MediaType; 
import javax.xml.bind.JAXBElement; 
import javax.xml.namespace.QName; 
import java.util.List; 
 
 
@Path("/solr_Data") 
public class solrData { 
 
    @GET 
    @Path("/getMessage") 
    //api/solr_Data/getMessage 
    @Produces(MediaType.TEXT_PLAIN) 
    public String getMessage() { 
        return "Hello world!"; 
    } 
 
}

  运行结果:

  

  2、一个@PathParam参数

  
   @GET 
    @Path("/getMessage/{msg}") 
    //http://localhost:8080/api/solr_Data/getMessage/Restful-Webservice 
    @Produces(MediaType.TEXT_PLAIN) 
    public String getMessage(@PathParam("msg") String msg) { 
 
        return String.format("Hello %s !",msg); 
 
    }

  运行结果

  

  3、两个@PathParam参数

    @GET 
    @Path("/getMessage/{start}-{end}") 
    //http://localhost:8080/api/solr_Data/getMessage/0-10 
    @Produces(MediaType.TEXT_PLAIN) 
    public String getMessage(@PathParam("start") int start,@PathParam("end") int end) { 
        return String.format("From %d to %d",start,end); 
    }

  

  4、一个@QueryParam参数

  

 @GET 
    @Path("/getMessage") 
    //api/solr_Data/getMessage?msg=Restful-Webservice 
    @Produces(MediaType.TEXT_PLAIN) 
    public String getMessage(@QueryParam("msg") String msg) { 
 
        return String.format("Hello %s !",msg); 
 
    }

  http://localhost:8080/api/solr_Data/getMessage?msg=Restful-Webservice

  

  5、两个@QueryParam参数

 @GET 
    @Path("/getMessage") 
    //http://localhost:8080/api/solr_Data/getMessage?msg=Restful-Webservice&start=10 
    @Produces(MediaType.TEXT_PLAIN) 
    public String getMessage(@QueryParam("msg") String msg,@QueryParam("start") int start) { 
 
        return String.format("Hello %s: %d !",msg,start); 
 
    }

  

  6、一个PathParam参数,一个QueryParam参数

 @GET 
    @Path("/getMessage/{start}") 
    //http://localhost:8080/api/solr_Data/getMessage/0?end=10 
    @Produces(MediaType.TEXT_PLAIN) 
    public String getMessage(@PathParam("start") int start,@QueryParam("end") int end) { 
        return String.format("From %d to %d",start,end); 
    }

  

  7、多个QueryParam参数参,返回xml数据

    solrEntity实体类

package com.solr.service; 
 
/** 
 * @Author:sks 
 * @Description: 
 * @Date:Created in 16:04 2018/3/1 
 * @Modified by: 
 **/ 
import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 
 
@XmlRootElement(name = "solrEntity") 
public class solrEntity { 
 
    String id; 
    @XmlElement 
    public String getId() { 
        return id; 
    } 
 
    public void setid(String id) { 
        this.id = id; 
    } 
 
    //fileloadDate,keyword,category,fileType,summary 
    String fileDate; 
    @XmlElement 
    public String getFileDate() { 
        return fileDate; 
    } 
 
    public void setFileDate(String fileDate) { 
        this.fileDate = fileDate; 
    } 
 
    String category; 
    @XmlElement 
    public String getCategory() { 
        return category; 
    } 
 
    public void setCategory(String category) { 
        this.category = category; 
    } 
 
    String keyword; 
    @XmlElement 
    public String getKeyword() { 
        return keyword; 
    } 
 
    public void setKeyword(String keyword) { 
        this.keyword = keyword; 
    } 
 
    String fileType; 
    @XmlElement 
    public String getFileType() { 
        return fileType; 
    } 
 
    public void setFileType(String fileType) { 
        this.fileType = fileType; 
    } 
 
    String summary; 
    @XmlElement 
    public String getSummary() { 
        return summary; 
    } 
 
    public void setSummary(String summary) { 
        this.summary = summary; 
    } 
 
    String title; 
    @XmlElement 
    public String getTitle() { 
        return title; 
    } 
 
    public void setTitle(String title) { 
        this.title = title; 
    } 
 
    String text; 
    @XmlElement 
    public String getText() { 
        return text; 
    } 
 
    public void setText(String text) { 
        this.text = text; 
    } 
 
}
View Code

    getQueryData 方法

package com.solr.service; 
 
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.SolrDocument; 
import org.apache.solr.common.SolrDocumentList; 
 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 
/** 
 * @Author:sks 
 * @Description: 
 * @Date:Created in 9:56 2018/2/26 
 * @Modified by: 
 **/ 
public class SolrService { 
 
    private SolrClient solr; 
 
    public SolrService() { 
        String urlString = "http://localhost:8983/solr/test"; 
        solr = new HttpSolrClient.Builder(urlString).build(); 
    } 
 
    /** 
     * @Author:sks 
     * @Description:获取文档总数 
     * @Date: 
     */ 
    private long getDocTotalCount() { 
        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; 
    } 
 
 
    /** 
     * @Author:sks 
     * @Description:获取普通查询数据 
     * @Date: 
     * @queryWhere:查询条件 
     * @showField:显示的字段 
     * @sortField: 排序字段 
     */ 
    public List<solrEntity> getQueryData(String queryWhere, String showField, String sortField) { 
        SolrQuery params = new SolrQuery(); 
 
        //例如:fileloadDate:[2018-02-25 TO 2018-03-1]  AND category:广告营销 
        //查询日期在2018-02-25 TO 2018-03-1之间,这里TO 必须是大写,并且category:广告营销的所有数据, 
        //多个与条件用AND连接,多个或条件用OR连接,必须是大写 
        params.setQuery(queryWhere); 
 
        //id,fileloadDate,keyword,category,fileType,summary 多个字段中间用逗号隔开 
        params.setFields(showField); 
        //从0页开始 
        params.setStart(0); 
        params.setRows((int) getDocTotalCount()); 
 
        //排序字段  fileloadDate:desc; category:asc 
        //字段和排序类型中间用冒号隔开,多个字段中间用分号隔开 
        if (sortField != null && sortField.length() != 0) { 
            String[] sortFields = sortField.split(";"); 
            List<SolrQuery.SortClause> sortlist = new ArrayList<SolrQuery.SortClause>(); 
            SolrQuery.ORDER order; 
            for (String sf : sortFields) { 
                order = sf.split(":")[1] == "asc" ? SolrQuery.ORDER.asc : SolrQuery.ORDER.desc; 
                SolrQuery.SortClause sc = new SolrQuery.SortClause(sf.split(":")[0], order); 
                sortlist.add(sc); 
            } 
            params.setSorts(sortlist); 
        } 
        List<solrEntity> list = new ArrayList<solrEntity>(); 
        try { 
            QueryResponse queryResponse = solr.query(params); 
            SolrDocumentList clr = queryResponse.getResults(); 
            System.out.println(clr.getNumFound()); 
            String[] fields = showField.split(","); 
            for (SolrDocument sd : clr) { 
                solrEntity se = new solrEntity(); 
                se.setid(sd.get("id").toString()); 
                se.setCategory(sd.get("category").toString()); 
                se.setKeyword(sd.get("keyword").toString()); 
                se.setFileType(sd.get("fileType").toString()); 
                list.add(se); 
            } 
 
        } catch (SolrServerException e) { 
            e.printStackTrace(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
        return list; 
    } 
 
}
View Code

    调用方法

@GET 
    @Path("/getSolrData") 
    //@Produces(MediaType.APPLICATION_JSON) 
    @Produces(MediaType.APPLICATION_XML) 
    //api/solr_Data/getSolrData?queryWhere=fileloadDate:[2018-02-25 TO 2018-03-1]  AND category:广告营销&showField=id,fileloadDate,keyword,category,fileType&sortField=fileloadDate:desc; category:asc 
    public List<solrEntity> getSolrData(@QueryParam("queryWhere") String queryWhere, 
                                        @QueryParam("showField") String showField, 
                                        @QueryParam("sortField") String sortField) { 
        SolrService service = new SolrService(); 
        //"fileloadDate:[2018-02-25 TO 2018-03-1]  AND category:广告营销" 
        //id,fileloadDate,keyword,category,fileType 
        //fileloadDate:desc; category:asc 
        List<solrEntity> list  = service.getQueryData(queryWhere,showField,sortField); 
        return list; 
    }
View Code

  浏览器查询:

http://localhost:8080/api/solr_Data/getSolrData?queryWhere=fileloadDate:[2018-02-25 TO 2018-03-1]  AND category:广告营销&showField=id,fileloadDate,keyword,category,fileType&sortField=fileloadDate:desc; category:asc
查询结果:


发布评论
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

jersey rest webservice知识解答
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。