I am running a web application using Jersey. In my controller, I am trying to get a file in src/main/resources
folder.
My folder structure is:
My gradle dependencies are:
dependencies { implementation group: 'org.glassfish.jersey.containers', name: 'jersey-container-servlet', version: '3.0.2' implementation group: 'org.glassfish.jersey.inject', name: 'jersey-hk2', version: '3.0.2' implementation group: 'org.glassfish.jersey.media', name: 'jersey-media-moxy', version: '3.0.2' }
My code is:
@GET @Path(value = "/log/details/{fileId}") @Produces(MediaType.APPLICATION_JSON) public List<IngestionLogs> getIngestionLogDetails(@PathParam("fileId") String fileId) { List<IngestionLogs> logs = new ArrayList<>(); try { InputStream stream; // how do I get this? List<IngestionLogs> errors = TsvFileValidators.parseObject(stream); logs.addAll(errors); } catch (FileNotFoundException e) { e.printStackTrace(); } return logs; }
How do I get an InputStream
for this file?
Advertisement
Answer
You can get it by servletContext.getResourceAsStream
InputStream is = servletContext.getResourceAsStream("/WEB-INF/test_model.js");
Add ServletContext
using @Context
@Context private ServletContext servletContext;
If you are missing import jakarta.servlet.ServletContext
add to dependency jakarta.servlet-api