我正在尝试从jersey客户端执行sparql查询但是执行返回200状态并且结果为null的问题。但是,当我在POSTMAN上执行相同的查询时,状态为200但结果不为null。 / p>
java客户端的代码:
package org.jaba.messenger.messenger.client;
import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.net.URLCodec;
import java.net.URI;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import org.w3c.dom.Document;
public class restapiClient {
public static void main(String args[])
{
Client client=ClientBuilder.newClient();
//sb simple query
HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("admin", "123123");
client.register(feature);
String query=new String("SELECT * WHERE { ?a ?b ?c }") ;
URLCodec uc = new URLCodec();
String sparqlQueryUri = null;
try
{
sparqlQueryUri=uc.encode(query);
System.out.println("SimpleJerseyClient(): uri = " + sparqlQueryUri);
}catch (EncoderException e)
{
System.err.println(e.toString());
e.printStackTrace();
}
WebTarget target=client.target("http://localhost:8080/qodisco/api/sync-search")
.queryParam("query",sparqlQueryUri);
Invocation.Builder invocationBuilder =target.request();
Response response = invocationBuilder.get();
;
System.out.println(response.getStatus());
System.out.println(response.readEntity(String.class));
}
}
这是我连接到它的API的代码:
package br.ufrn.dimap.consiste.qodisco.api;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import br.ufrn.dimap.consiste.qodisco.model.entities.Domain;
import br.ufrn.dimap.consiste.qodisco.model.entities.User;
import br.ufrn.dimap.consiste.qodisco.model.enums.TopicType;
import br.ufrn.dimap.consiste.qodisco.services.APIService;
import br.ufrn.dimap.consiste.qodisco.services.FusekiService;
@RestController
@RequestMapping("/api")
public class QoDiscoAPI {
@Autowired
private APIService apiService;
@Autowired
private FusekiService fusekiService;
@RequestMapping(value="/user", method=RequestMethod.POST)
public ResponseEntity<String> addUser(@RequestBody User user){
if(!apiService.addUser(user)){
return new ResponseEntity<String>(HttpStatus.CONFLICT);
}
return new ResponseEntity<String>(HttpStatus.CREATED);
}
@RequestMapping(value="/sync-search", method=RequestMethod.GET, produces="text/json")
//@RequestParam("domain") String domain,
public String syncSearch( @RequestParam("query") String query){
return apiService.searchByQuery("Pollution", query);
}
@RequestMapping(value="/async-search", method=RequestMethod.GET, produces="text/json")
public @ResponseBody String asyncSearch(@RequestParam("query") String query, @RequestParam("domain") String domain, @RequestParam("type") int type){
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss.SSS");
Date date = new Date();
String topicName = "qodisco"+auth.getName()+dateFormat.format(date);
TopicType topicType;
switch(type){
case 1:
topicType = TopicType.TIPO1;
break;
case 2:
topicType = TopicType.TIPO2;
break;
case 3:
topicType = TopicType.TIPO3;
break;
default:
topicType = null;
break;
}
if(topicType!=null){
fusekiService.asyncSearch(query, topicName, domain, topicType);
return topicName;
} else{
return null;
}
}
@RequestMapping(value="/resource", method=RequestMethod.POST)
public ResponseEntity<String> addResource(@RequestParam("domain") String domain,
@RequestParam("data") String data){
apiService.createOrUpdateResource(domain, data);
return new ResponseEntity<String>(HttpStatus.CREATED);
}
@RequestMapping(value="/resource", method=RequestMethod.PUT)
public ResponseEntity<String> updateResource(@RequestParam("domain") String domain,
@RequestParam("data") String data){
apiService.createOrUpdateResource(domain, data);
return new ResponseEntity<String>(HttpStatus.OK);
}
@RequestMapping(value="/resource", method=RequestMethod.DELETE)
public ResponseEntity<String> removeResource(@RequestParam("domain") String domain,
@RequestParam("data") String data){
apiService.createOrUpdateResource(domain, data);
return new ResponseEntity<String>(HttpStatus.OK);
}
@RequestMapping(value="/repository", method=RequestMethod.POST)
public ResponseEntity<String> addRepository(@RequestParam("domains") List<String> domainNames,
@RequestParam("url") String repositoryUrl, @RequestParam("operations") List<String> operations){
if(apiService.addRepository(domainNames, repositoryUrl, operations)){
return new ResponseEntity<String>(HttpStatus.OK);
} else{
return new ResponseEntity<String>(HttpStatus.CONFLICT);
}
}
@RequestMapping(value="/repository", method=RequestMethod.DELETE)
public ResponseEntity<String> removeRepository(@RequestParam("url") String url){
if(apiService.removeRepository(url)){
return new ResponseEntity<String>(HttpStatus.OK);
} else{
return new ResponseEntity<String>(HttpStatus.CONFLICT);
}
}
@RequestMapping(value="/rdo", method=RequestMethod.GET, produces="text/json")
public List<Domain> getRdos(HttpServletResponse response){
return apiService.getDomains();
}
@RequestMapping(value="/rdo", method=RequestMethod.POST)
public ResponseEntity<String> addRdo(@RequestBody Domain domain){
System.out.println(domain);
if(apiService.addRdo(domain)){
return new ResponseEntity<String>(HttpStatus.OK);
}else{
return new ResponseEntity<String>(HttpStatus.CONFLICT);
}
}
@RequestMapping(value="/rdo", method=RequestMethod.DELETE)
public ResponseEntity<String> removeRdo(@RequestParam("name") String name){
if(apiService.removeDomain(name)){
return new ResponseEntity<String>(HttpStatus.OK);
} else{
return new ResponseEntity<String>(HttpStatus.CONFLICT);
}
}
@RequestMapping(value="/rdo", method=RequestMethod.PUT)
public ResponseEntity<String> updateRdo(@RequestBody Domain domain){
if(apiService.addRdo(domain)){
return new ResponseEntity<String>(HttpStatus.OK);
}else{
return new ResponseEntity<String>(HttpStatus.CONFLICT);
}
}
}
这是运行客户端的结果:
SimpleJerseyClient(): uri = SELECT+*+WHERE+%7B+%3Fa+%3Fb++%7D
200
org.glassfish.jersey.client.internal.HttpUrlConnector$2@cd3fee8
null
谢谢
答案 0 :(得分:0)
这可能是由端点侧的解析错误引起的。我发现Fuseki
(是你的底层三重存储?)要求空格编码为%20
而不是+
。但是,我设法从浏览器运行+
编码查询就好了。在编码的查询字符串中用+
替换%20
可能会解决问题。否则,希望有人觉得这很有用。
sparqlQueryUri = uc.encode(query).replaceAll("\\+", "%20");