Java 从REST资源访问Servlet初始化参数
我有一个JAX-RSRESTWeb应用程序,用于为桌面客户端存储和检索文件。我将在两个不同的服务器上的两个不同的环境中部署它,因此我希望在代码之外配置文件的存储路径 我知道如何从Servlet读取初始化参数(在web.xml中)。我可以为REST资源类执行类似的操作吗?如果我可以从WEB-INF目录中的其他文件中读取,那么也应该可以 以下是我正在使用的代码:Java 从REST资源访问Servlet初始化参数,java,rest,servlets,Java,Rest,Servlets,我有一个JAX-RSRESTWeb应用程序,用于为桌面客户端存储和检索文件。我将在两个不同的服务器上的两个不同的环境中部署它,因此我希望在代码之外配置文件的存储路径 我知道如何从Servlet读取初始化参数(在web.xml中)。我可以为REST资源类执行类似的操作吗?如果我可以从WEB-INF目录中的其他文件中读取,那么也应该可以 以下是我正在使用的代码: import javax.ws.rs.*; import java.io.*; @Path("/upload") public clas
import javax.ws.rs.*;
import java.io.*;
@Path("/upload")
public class UploadSchedule {
static String path = "/home/proctor/data/schoolData/";
//I would like to store the path value in web.xml
@PUT
@Path("/pxml/{id}/")
@Consumes("text/xml") @Produces("text/plain")
public String receiveSchedule(@PathParam("id") final Integer schoolID, String content) {
if (saveFile(schoolID, "schedule.pxml", content))
return schoolID + " saved assignment schedule."
else
return "Error writing schedule. ("+content.length()+" Bytes)";
}
/**
* Receives and stores the CSV file faculty list. The location on the server
* is not directly associated with the request URI.
* @param schoolID
* @param content
* @return a String confirmation message.
*/
@POST
@Path("/faculty/{id}/")
@Consumes("text/plain") @Produces("text/plain")
public String receiveFaculty(@PathParam("id") final Integer schoolID, String content) {
if (saveFile(schoolID, "faculty.csv", content))
return schoolID + " saved faculty.";
else
return "Error writing faculty file.(" +content.length()+ " Bytes)";
}
//more methods like these
/**
* Saves content sent from the user to the specified filename.
* The directory is determined by the static field in this class and
* by the school id.
* @param id SchoolID
* @param filename location to save content
*/
private boolean saveFile(int id, String filename, String content) {
File saveDirectory = (new File(path + id));
if (!saveDirectory.exists()) {
//create the directory since it isn't there yet.
if (!saveDirectory.mkdir())
return false;
}
File saveFile = new File(saveDirectory, filename);
try(FileWriter writer = new FileWriter(saveFile)) {
writer.write(content);
return true;
} catch (IOException ioe) {
return false;
}
}
}
首先,您必须使用
@Context
ServletContext context;
然后在您的rest资源中
context.getInitParameter("praram-name")
首先,您必须使用
@Context
ServletContext context;
然后在您的rest资源中
context.getInitParameter("praram-name")
虽然从web.xml获取init参数似乎是一项常见的任务,但我花了相当长的时间才弄清这一点并找到一个有效的解决方案。为了让别人免于我的挫折,让我发布我的解决方案。我正在使用Jersey实现,即
com.sun.jersey.spi.container.servlet.ServletContainer
也许其他REST实现可以使用ServletContext
访问web.xml init参数,但尽管文档让我相信这是可行的,但它没有
我需要改为使用以下内容:@Context-ResourceConfig-Context代码>
这是作为我的资源类中的一个字段列出的。然后,在我的一个资源方法中,我能够通过以下方式访问web.xml init参数:
String uploadDirectory = (String) context.getProperty("dataStoragePath");
其中属性引用web.xml文件:
<init-param>
<param-name>dataStoragePath</param-name>
<param-value>C:/ztestServer</param-value>
</init-param>
输出:
com.sun.faces.forceLoadConfiguration
com.sun.faces.validateXml
虽然从web.xml获取init参数似乎是一项常见的任务,但我花了相当长的时间才弄清这一点并找到一个有效的解决方案。为了让别人免于我的挫折,让我发布我的解决方案。我正在使用Jersey实现,即
com.sun.jersey.spi.container.servlet.ServletContainer
也许其他REST实现可以使用ServletContext
访问web.xml init参数,但尽管文档让我相信这是可行的,但它没有
我需要改为使用以下内容:@Context-ResourceConfig-Context代码>
这是作为我的资源类中的一个字段列出的。然后,在我的一个资源方法中,我能够通过以下方式访问web.xml init参数:
String uploadDirectory = (String) context.getProperty("dataStoragePath");
其中属性引用web.xml文件:
<init-param>
<param-name>dataStoragePath</param-name>
<param-value>C:/ztestServer</param-value>
</init-param>
输出:
com.sun.faces.forceLoadConfiguration
com.sun.faces.validateXml
唯一的问题是,我后来发现它不起作用。这不起作用。本例中的上下文是应用程序Facade。唯一的问题是,我后来发现它不起作用。这不起作用。本例中的上下文是应用程序外观