Rest Web Service + Apache Solr: show XML in webapi

0

I have a problem and partly it is ignorance. I'm trying to get my Web Service to show me the XML corresponding to the data indexed in Apache Solr. The WS is connected, tested and gets the data, but at the time of doing the return to show the data in the resource it just gives me some meaningless tags in the source code.

This would be the "Product" model:

import javax.xml.bind.annotation.XmlRootElement;

import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;

@XmlRootElement
public class Producto {

private String id;
private String name;
private String manu;
private String manu_id_s;
private SolrDocumentList results;

public Producto() {

}

public Producto(String id, String name, String manu, String manu_id_s) {
    this.id = id;
    this.name = name;
    this.manu = manu;
    this.manu_id_s = manu_id_s;
}


public Producto(SolrDocumentList results) {
    this.results = results;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getManu() {
    return manu;
}

public void setManu(String manu) {
    this.manu = manu;
}

public String getManu_id_s() {
    return manu_id_s;
}

public void setManu_id_s(String manu_id_s) {
    this.manu_id_s = manu_id_s;
}



}

Your corresponding service "ProductService":

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;

import com.data.solr.PruebaMavenSolr3.conexion.Connection;
import com.data.solr.PruebaMavenSolr3.modelos.Producto;

public class ProductoService {

public List<Producto> getTodosProductos() throws MalformedURLException, SolrServerException {
    Connection.init();
    final SolrQuery query = new SolrQuery(); 
    query.setQuery("*:*");
    query.setIncludeScore(false);
    query.setFields("id,name,manu,manu_id_s");
    query.setParam("wt", "xml");
    QueryResponse response = Connection.server.query(query);
    Producto p1 = new Producto(response.getResults());
    List<Producto> list = new ArrayList<>();
    list.add(p1);
    return list;

}
}

And Your resource "MyResource"

import java.net.MalformedURLException;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.common.SolrDocumentList;

import com.data.solr.PruebaMavenSolr3.modelos.Producto;
import com.data.solr.PruebaMavenSolr3.servicios.ProductoService;

@Path("myresource")
public class MyResource {

ProductoService productoService = new ProductoService();

@GET
@Produces(MediaType.APPLICATION_XML)
public List<Producto> getProductos() throws MalformedURLException, SolrServerException {
    return productoService.getTodosProductos();
}
}

When running the webapi it shows me an empty page with this source code:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><productoes><producto/></productoes>

I already tried changing the configuration in many ways and I do not have a clear example of how to work Rest WS with Solr.

Thank you very much.

Update: as additional data I also leave the connection to Solr. The moment you connect, it parses XML

import java.net.MalformedURLException;

import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.impl.XMLResponseParser;

public class Connection {
public static HttpSolrServer server;
public static void init() throws MalformedURLException, SolrServerException{
    server = new HttpSolrServer("http://localhost:8983/solr/collection1/");
    server.setParser(new XMLResponseParser());
}

}
    
asked by Darius 08.02.2017 в 16:05
source

1 answer

0

Replace this code snippet

    @XmlRootElement
    public class Producto

for the following:

    @XmlRootElement(name = "Producto")
    public class Producto
    
answered by 08.02.2017 в 16:30