This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

how to load a particular file of resources based on the request url using java spring

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm beginner to Java and I've a project developed using spring. In my project I've an interceptor which loads at first by DispatcherServlet, in that Interceptor I want to load a particular json which is in my resources folder based on the request url.
I have 100 urls and 100 related jsons(in the resource folder), so how can I load those jsons based on the coming requests. I know how to reach the resource folder using
ClassLoader classLoader = getClass().getClassLoader();
URL resource = classLoader.getResource(APP_CONFIG_FILE_PATH);

I don't know how to match the request and the resource file and there are 100 request urls and jsons.

The Interceptor uses HandlerInterceptor and it has preHandle().

Any help is appreciated.


Thanks.

 
Ranch Hand
Posts: 49
Eclipse IDE MySQL Database Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have each of those resources in some unique named files, I assume, then
You can use path variables in the url and then access that variable in your controller, url can be like this
http://yourproject.domain/resources/(someUniqueIdOfThatResource)
In your controller you can have a mapping like this


Those curly braces around resourceID in request mapping annotation are important.
This is just one way, you can also use requestParam or MatrixParam.
Thanks
 
Saloon Keeper
Posts: 24847
174
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You shouldn't be accessing resources by brute force in a webapp. Use the getResource() method to locate a resource. Or use getResourceAsStream, which should be able to feed straight into the JSON parser.

Resource locations are rooted at the head of the application WAR, so they always begin with a slash. Like so:

 
Lilly Rose
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, this worked well "InputStream istream = servletContext.getResourceAsStream("/resources/warehouse.json");
" .
I've another question, I've stored all the resource file locations as a constants and using this
String path = request.getRequestURI().substring(request.getContextPath().length());
String[] pathValue = path.split("/");
I could able to find the path parameters to match the resource file,
I've 100  urls and 100 matching resource files,
which way one is efficient to use, conditional IF or which one?
eg: if (pathValue[2].equals("abc")&&pathValue[3].equals("getAbc"))

 
 
Tim Holloway
Saloon Keeper
Posts: 24847
174
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No sure if I have good understanding here, but I think I'd probably use getPathTranslated to get the resource path of the URL and the simplest way to locate the resource would be to put the resource in a location whose path matched the URL. Like so:



Might need tweaking slightly.

If it isn't practical to directly map resources from URL to WAR location, then I'd suggest a table lookup. If you want better performance, sort the table by URL location and do a binary search of the table. 100 entries is easily small enough to keep in memory and you can make the table more maintainable by reading it in from a resource.
 
Lilly Rose
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, I think you are pointing me in correction direction. I would like to be more specific for you.

Here is what I did so far.



When I run this, I'm getting the result as a marched response. But need to figure it out for all the jsons.

AbcConstants.java has my resource constants.

public static final String TEST_DETAILS_RESPONSE = "response/payment/getDetails.json"; // do I need to create constants for all the json here?

My resource folder is in : src/main/resources.

My response folder has all the json in a particular folder
eg: payment has - response/payment/getDetails.json like this 12 othere json's.
and response/xyz/ ab1.json
like this I have 20 other folders under response.


My urls look different compare to the folder structure of the resource.

eg:



http://localhost:8080/abc/rest/payment/bank/delete
http://localhost:8080/abc/rest/payment/bank/update/67676
http://localhost:8080/abc/rest/payment/bank/add
http://localhost:8080/abc/rest/payment/card/delete
http://localhost:8080/abc/rest/payment/card/update/090909
http://localhost:8080/abc/rest/payment/card/add
http://localhost:8080/abc/rest/payment/card/generateToken
http://localhost:8080/abc/rest/payment/getDetails/AL3153595
http://localhost:8080/abc/rest/payment/somemore....




like this I have different urls  /rest/phone/...., /rest/someother/xyz/...,

Each time only one url will be coming to hit this interceptor, based on the coming url we need to pick the matching json.

I'm worried about this if block

if (pathValue[2].equals("payment")&&pathValue[3].equals("getDetails")) {
String filename = AbcConstants.TEST_DETAILS_RESPONSE;
response = getMatchedJsonResponse(filename);

}

How to match them, using conditional if is good or any other better way to match up.

Please help me out on this with a good solution.

I appreciate your help.

Thank you.





 
Tim Holloway
Saloon Keeper
Posts: 24847
174
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm. That's a bit much for me to make sense of this early in the morning. Let me approach it the way I'd expect to do it (based on similar projects).

First, it looks like you may be using Maven to build your project. If you want to build a WAR, the resource files for the WAR would normally be placed under src/main/webapp. not src/main/resources. Everything in src/main/webapp gets copied over to the WAR, so src/main/webapp/WEB-INF/classes/log4j.xml for example would end up in the WAR as /WEB-INF/classes/log4j.xml.

When I'm doing ReST, I have dedicated classes to major functions and annotated methods in those classes to minor functions. So, for example. I'd have a com.xyx.payment.Bank class with delete, update and add methods, each one being mapped to a ReST URL. So the easiest way to get a unique JSON resource for these methods would be to simple provide a shared method in the class, a base class or a static utility class that implemented a getJsonResource method and just hard-code the resource path as an argument. Like so:



I notice that you're doing stuff with general Spring web functions, but I think maybe you should be using Spring ReST. As you can see from the above example coding to use the JSR-standard ReST annotations is probably going to be simpler even before you add Spring-specific support to it.
 
Lilly Rose
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, my project uses Spring Rest and the code I posted and I'm  trying to do here is lil different, I have developed everything with the Spring Rest, I've 18 different controllers. The thing here I'm trying to do using this interceptor is when we want to launch our app we want to give some test data to the vendor, that is why I'm trying to give those test json responses when it hit a particular Url.
Like you said the above code we need to write another controller using @Path(), I don't want to do that.
I want to do everything in the interceptor.
If you could guide me how to match up the url with json's( any better way instead of If block) within the interceptor, that will be helpful for me.

Thank you.
 
Lilly Rose
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please give me some solutions.
 
Tim Holloway
Saloon Keeper
Posts: 24847
174
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems to me like you want a central control for all possible URL paths of interest. One thing I learned from Struts (and so did the people who designed Struts) was that it can be a real nuisance to separate strongly-related components.

So unless I'm missing something (which is very likely), you'd need your interceptor to either be able to translate a URL path fragment to a resource path using some simple mechanism like what I showed earlier, or you'd need to keep a dictionary that mapped the URL path fragments to their corresponding resource paths. This means doing something like keeping a mapping file that the Interceptor loads into a Map at startup. And it means that every time you add a new JSON-related URL or change the location of a resource for an existing URL, you'd have to change that map file. And properly, if you delete a URL from the list, delete the mapping, but that's less critical.

So the disadvantage of doing things like this in the Interceptor is that you'd have to potentially edit 2 files instead of one every time you did maintenance. One being the class file with the logic for handling the URL, one for the mapping.
 
Lilly Rose
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much for your quick responses, I got lot of knowledge through this and I appreciate your help. It makes sense for me the approach you've provided.
I figured it out a solution for it, I used properties file where I stored all the json response-path as a key/value pair. where I filtered the key from the request.getRequestURI(). I could able to go forward now and the interceptor is working well.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic