我正在用Netbeans编写一个相当简单的restful web服务项目(使用Maven web Application模板)。我正试图在Glassfish 4.1服务器上运行它。我过去使用过Tomcat,但在这里并不是一个真正的选择。基本上,我的问题是我运行项目,服务器启动,但当我试图在浏览器中访问服务时,我只是得到一个404错误。
我的源代码:
package jp.go.aist.limrs;
import com.hp.hpl.jena.rdf.model.Model;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.websocket.server.PathParam;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.apache.commons.io.IOUtils;
@Path("/target")
public class ParserService
{
public static final String SERVER_LOC = "http://localhost:8080/LiMRS/";
public static final String MAPPINGS_LOC = "export.txt";
private String targetUrl;
private String microData;
private Model uDataModel;
private Model mappingsModel;
public ParserService() {}
public ParserService( String url )
{
this.targetUrl = url;
try {
parseMicro(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
@GET
@Path("/{url:.+}")
@Produces(MediaType.TEXT_PLAIN)
public String getMicro(@PathParam("url") String target)
{
this.targetUrl = target;
String domain = "_";
try {
URI uri = new URI(this.targetUrl);
domain = uri.getHost();
System.out.println("Domain is " + domain + "nnn");
} catch (URISyntaxException ex) {
Logger.getLogger(jp.go.aist.LiMRS.LiMRService.class.getName()).log(Level.SEVERE, null, ex);
}
this.microData = "<?xml version="1.0" encoding="utf-8"?>" +
"<rdf:RDF xml:base="http://dbpedia.org/ontology/" " +
"xmlns:_="" + domain + "">nn";
try
{
parseMicro(URLEncoder.encode(this.targetUrl, "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
return "";
}
return this.microData;
}
private void parseMicro(String target) throws MalformedURLException
{
try {
URL url = new URL("http://getschema.org/microdataextractor?url=" + target + "&out=rdf");
HttpURLConnection conn;
try {
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
InputStream ins = conn.getInputStream();
StringWriter writer = new StringWriter();
IOUtils.copy(ins, writer, null);
this.microData += writer.toString() + ".";
} catch (IOException ex) {
Logger.getLogger(jp.go.aist.LiMRS.LiMRService.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (MalformedURLException ex) {
Logger.getLogger(jp.go.aist.LiMRS.LiMRService.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
我用来测试服务的URL是:http://localhost:8080/LiMRS/target/http://www.rottentomatoes.com/m/jurassic_park/
(我知道URL是未编码的。在URL的"资源"部分,在"/target/"之后,有正斜杠,但这是由代码中的正则表达式处理的,不是问题的根源。
这是可能的问题是与服务器本身,我不知道是否有任何特殊的配置需要做Glassfish或者如果你可以直接运行项目。我没有web.xml文件。除非我搞错了,我觉得我不需要。我遗漏了什么?
您将需要一个web.xml或Application/ResourceConfig
子类来配置应用程序。如果你不想使用web.xml,你可以做的最简单的事情就是用@ApplicationPath
注释一个空的Application
类。这将导致注册您拥有的所有@Path
类
@ApplicationPath("/")
public class JaxRsApplication extends Application {}
你可以在这里看到更多选项