Tomcat+Spring boot redirect ignoring contextPath - spring

I'am using Spring boot and Tomcat7 to build a vehicle management system.
The base path is localhost:8080/vehicle
My server setting:
server.contextPath=/vehicle
My IndexController:
#RequestMapping("/")
public class IndexController extends BaseController {
#RequestMapping(value = "/", method = RequestMethod.GET)
public String index(Model model) {
return "redirect:/login";
}
}
But when I go straight to get this view, my path is ...../login instead of ..../vehicle/login And so it returns 404 Error.
Also when I tried to use return "redirect:/vehicle/login"; it still goes to ...../login.
So what is wrong with my code. Why the server can't recognize the contextPath.

In application.properties use : server.servlet.context-path=/contextPath.
Example:
server.servlet.context-path=/vehicle
Tomcat Server:
In the webapps folder, the context path(vehicle) must be the same as the folder name(vehicle).

Related

Two end points work without configuration - showing same view

I am using Spring Boot (2.5.6)'s Controller like this:
#Controller
public class WebController {
#GetMapping("/index")
public String indexPage () {
return "index";
}
}
And when I hit these two URLs:
http://localhost:8080/index
http://localhost:8080
Same Thymeleaf view index.html is served. To my understanding on hitting http://localhost:8080 I should get - Whitelabel Error Page.
In the past I have used something like this #RequestMapping(value = { "/", "/index" }, method = RequestMethod.GET) extensively. What I am missing/overlooking here?
Spring Boot, through auto-configuration, will add a WelcomePageHandlerMapping. The WelcomePageHandlerMapping will mimic the behavior of the welcome-page support in Servlet containers like Tomcat.
By default it will try to locate an index.html in either the static or template directory and, if needed, use the available templating framework to render this page.

Spring Boot redirect to another url

In my spring boot project i wanted to do a redirection from http://localhost:8080 to http://localhost:8080/birdspotting. This is the code of the Home controller:
#RestController
public class HomeController {
#GetMapping("/")
public String showHomePage() {
return "redirect:/birdspotting";
}
}
The result of going to http://localhost:8080 is a print of redirect:/birdspotting
Basically RestController = Controller + RequestBody
Which will send json response but we are expecting view resolver to return the page or redirect url.
So use #Controller instead of #RestController to fix the issue.
Update:
If you want to use both in Same controller then use #Controller on class level and then wherever you want to return API call response put #ResponseBody on method and wherever you want to return web browser page don't put #ResponseBody.
You have to create the other endpoint like:
#GetMapping("/")
public String showHomePage() {
return "redirect:/birdspotting";
}
#GetMapping("/birdspotting")
public String birdspottingPage() {
return "birdspotting";
}
It's expect you have birdspotting.html in your templates.

Mappings in RestController not found

I have a RestController that defines a default path and some endpoints like this:
#RestController
#EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
#RequestMapping(path = "/somePath", produces = "application/hal+json")
public class SomeRestController {
#GetMapping (path = "/otherPath")
public String someEndpoint(){
return "hello";
}
...other endpoints...
}
I get a 404 for the mapped endpoints. However, if I delete the default RequestMapping the endpoints suddenly get picked up! I also tried RequestMapping ( path =..., method=RequestMethod.GET) for the endpoints, but same result...
If I delete the #GetMapping from one endpoint, the default path is mapped successfully.
What is going on here? Why do the endpoints don't get mapped if I have the default RequestMapping?
You have to concat both pathes:
localhost:8080/somePath/otherPath
because the mapping on top of the class is for all methods in this controller, and than the method specific path will be added

Code is not update in Maven war

I am working on a Spring MVC and Hibernate Project.When i build war clean install and deploy in tomact.In console old code is running Means like this is my index controller
#Controller
#RequestMapping({ "/index" })
public class IndexController {
private final Logger logger =
LogManager.getLogger(this.getClass().getSimpleName());
#RequestMapping(method = RequestMethod.GET)
public String index(ModelMap model, final Principal principal) {
//logger.debug("Enter in Get method IndexController");
return "index";
}
}
you can see i comment the logger but it print in console.So i think it comes from another controller then i delete all controllers from my code and after deploying it still print logger and i also remove logger from this controller and after deploying it still print logger.I dont know why my code is not update in war.Can anyone help me
Please clear local repository and then do clean install
For windows you can find .m2 repository in your C:\Users\{yourUsername}\.m2

How to call Spring Boot (using tomcat) url redirect no template engine like localhost:8080/sample/someparameter

I am using spring boot+restful WS+ angular2. Mow my url is like http://localhost:8080/accountSummary/someparameter
When I run this I am getting error below in the browser
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Jun 30 16:24:03 EDT 2016
There was an unexpected error (type=Internal Server Error, status=500).
Circular view path [index.html]: would dispatch back to the current handler URL [/accountSummary/index.html] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
I have used below configurations in spring boot conf file under app folder:
#Controller
#Configuration
public class WebController extends WebMvcConfigurerAdapter{
// first option I have tried
/*public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/accountSummary/**").setViewName("index.html");
}*/
// second option I have tried
/* #RequestMapping(value = "/{[accountSummary:[^\\.]*}")
public String redirect() {
return "forward:/index.html";
}*/
Third option I have tried
#RequestMapping(value={"/accountSummary/{*}"}, produces="text/html")
public String getIndex(Model model, HttpServletRequest request){
return "index.html";
}
}
Presently this is show stopper to me. I am using pure spring boot structure like I am moving all the web application content into static folder under target folder.
You have been on the right path but used the wrong prefix. If you want to send a redirect you should use redirect: as a prefix instead of forward:.
It got fixed using Angular2. I have kept {provide:LocationStrategy,useClass:HashLocationStrategy}. if you give localhost:8080/#/ -- it will redirect to pagename with parameter if you provide.
Thanks to all for helping me.
#RestController
Add this annotation in your controller
#RequestMapping(value = "/accountSummary/someparameter", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
Add this on your method level and change according to yours method.

Resources