Categories: java, spring, spring-boot

Spring-boot location variables in urls

1 answer

I'm trying to set up my website to allow location additions to the urls. EG: mysite.com/us/ca/sanfrancisco/home While also still allowing mysite.com/home and everything in between.

Spring boot parent so you know what version of spring I'm using:

<parent>     <groupId>org.springframework.boot</groupId>     <artifactId>spring-boot-starter-parent</artifactId>     <version>1.5.6.RELEASE</version> </parent> 

If there's another piece of versioning you need, let me know.

I get that I can add in regex variables to the request mapping, but how would I go about persisting those urls across more requests?

So right now for an example, the testing error page I have:

    @RequestMapping({"/error/501", "/{state:[a-z]{2}}/error/501", "/{state:[a-z]{2}}/{city:[a-z]+}/error/501"})     public ModelAndView testingLocations(ModelMap model, @PathVariable(value="state", required = false) String state,                                          @PathVariable(value="city", required=false) String city){         getLogger().info("State: {}", state);         model.addAttribute("stateTest",state+":"+city);         model.addAttribute("view", "error");         return new ModelAndView("error/501", model);     } 

But when I'm on my testing page, and I click the home button, it takes me back to mysite.com/home

So My Questions

Is there a way for me to persist it so that if they're currently on a location based url, it will apply that base to the future navigations? (unless they manually enter the url to not include them)

Then as a follow-up, is there a way for me to globally apply these request variables without requiring me to add the @PathVariable to every request mapping method? I get that I can just add the request mapping variable strings themselves to the controller class, so that I don't need those on every method. But is there a way for me to utilize those without needing the @PathVariable annotations?

Finally, is there a way for me to make this not as hardcoded, like a way for me to say /{*location}/error to cover as deep as the locations will allow? While still having the verification on the location formatting, so verifying that 1 we support the locations given, 2 the format is correct (/ca/sanfrancisco vs /anything/anything

The last one I can live with, if I need to have the /state/city/municipality/actualtarget As far as verifying that we support the locations given, I understand that's on my end, which I'll probably just have a small database to keep track of where we do and do not support for the given variables.

Is there a best practice for building this system? I tried to find something on this, but googling "spring boot location url" is not the best at giving me what I need, since "location" can apply to a pretty wide range of topics. I've gotten to where I am from searching, but I can't seem to pin down these last few steps.

Any help/advice/suggestions is appreciated. If upgrading versions is required, I'm not sure how viable that is at the moment, I'd have to look into it. Preferably I'd like the solution to be able to be done on the current spring version I'm running.

All answers to this question, which has the identifier 61231444

The best answer:

The best way here is:

@RequestMapping("/some/{foo}/{baz}") public String hi(CompositeObject compositeObject) {    return "hi"; }  @Data public class CompositeObject {     private String foo;     private String baz; } 

Spring provides functionality for request path and request parameters to collect it into a composite object. It doesn' work either with body or headers.

If you have something optional like state, then just keep it null at the controller and handle later

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type com.google.firebase.iid.zzav is referenced as an interface from com.google.firebase.messaging.zzd”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data