I hava Spring Boot Application.I want to show html.
index.html location is following
templete/view/index.html
Controller.java
#Controller
public class Controller {
#RequestMapping(value = "/view", method = RequestMethod.GET)
public String index() {
return "/view/index.html";
}
}
return "view/index";
Should be what you need. In the future you might consider saying what you expect to happen and what actually happens, even better providing an error message which might give a hint like your file could not be found.
Related
I'm new to Spring Boot and I'm playing around with it trying to get a response from my backend.
The web server and application are running without errors but when I try to get "Hello World" by typing localhost:8080/api/hello I get a 404 not found.
This is the controller method
#RestController
#RequestMapping("/api")
public class EmployeeController {
#GetMapping("/hello")
public String greeting() {
return "Hello, World";
}
}
This is the response I get
the response
You've defined your api path as api/hello, but based on the screenshot of your request you are calling /hello.
use localhost:8080/api/hello ,
it will a solution for your problem
I am building a URL Shorter app (like Bitly). It is an SPA using Spring Boot & ReactJS. All web content is served off of index.html. All other routes are presumed to be shortLink redirect requests which should trigger a clickShortUrl() function to fetch the corresponding originalLink and redirect the user to that web address.
Therefore, I want the following routes to redirect to index.html:
#GetMapping(value = {"/", "/home", "/dashboard"})
public String redirect() {
return "forward:/index.html";
}
and all other/unknown routes to trigger a wildcard function:
#RequestMapping(value = "/{shortUrl}", method = RequestMethod.GET)
public Object clickShortUrl(#PathVariable("shortUrl") String shortUrl, #RequestBody ClickDTO request) {
// internalLogicHere
};
Individually, the mappings and functions are working. But combined, the /{shortUrl} wildcard route always takes precedence. I've googled around looking for ways to override this behavior. It seems to be possible a few ways, but all of my attempts have failed.
I read several posts like this suggesting to extend WebMvcConfigurerAdapter and override addViewControllers(ViewControllerRegistry registry) to define view controllers for specific routes. I don't really understand this. Is this the right path? If so, can someone help me understand what ViewControllerRegistry is all about and set me on the right path?
Thank you!
Answered my own question. Ended up using RegEx in the wildcard route to exclude the static paths used on the front end.
/** Redirect all '/' and '/dashboard/ requests to index.html. */
#GetMapping(value = {"path:/", "path:/dashboard"})
public String redirect() {
return "forward:/index.html";
}
and the fallback route:
/**
* Treat all routes as /{shortUrl} clicks except: '/', '/index.html, '/dashboard''
*/
#RequestMapping(value = "{_:^(?!index\\.html|dashboard).*$}")
public Object clickShortUrl(#PathVariable("shortUrl") String shortUrl, #RequestBody ClickDTO request) {
// internalLogicHere;
}
#RequestMapping("/modify")
public String modifyAd(AdDto adDto, Model model){
if (adService.modifyAd(adDto)){
adDto.setTitle("");
model.addAttribute(PageCodeEnum.KEY,PageCodeEnum.MODIFY_SUCCESS);
}else {
model.addAttribute(PageCodeEnum.KEY,PageCodeEnum.MODEFY_FAILED);
}
return "forward:search";
}
The adDto.title I got from jsp is '芒果冰淇淋11',
I have set Title to ""
#RequestMapping(value = "/search",method = RequestMethod.POST)
public String queryByTitle(AdDto adDto, Model model){
List<Ad>adList = adService.queryByTitle(adDto);
model.addAttribute("adList",adList);
model.addAttribute("searchParam",adDto);
return "/content/adList";
}
But after forward, adDto.title doesn's change. I don't know why.
You can see the 'title' change to "芒果冰淇淋11"
I think this response on another question may be helpful:
Spring forward with added parameters?
re-adding what you need to the request before it is forwarded.
Using FlashScope as well should be an option here an example:
https://www.javacodegeeks.com/2012/02/spring-mvc-flash-attributes.html
(in the example it uses "redirec:some/path" but using "forward" as you do should be possible as well)
A link to Spring (4.1) docs for Flash attributes:
https://docs.spring.io/spring/docs/4.1.x/spring-framework-reference/html/mvc.html#mvc-flash-attributes
I finally solved this problem in adding the following code:
Code
In my Spring MVC controller, I have a single method process():
#Controller
#RequestMapping("/Foo/dash")
public class MyController {
#RequestMapping( params = "action=loadDashboard")
public String process() {
return "/Moo/dash/bar";
}
}
It is my understanding that in this case, upon hitting the return statement, Spring MVC will perform a forward to the specified JSP file.
How can I change the return statement, and/or the method, so that instead of a forward, Spring MVC will perform an include of the specified JSP?
I want the client and server application to talk to each other using REST services. I have been trying to design this using Spring MVC. I am looking for something like this:
Client does a POST rest service call saveEmployee(employeeDTO, companyDTO)
Server has a similar POST method in its controller saveEmployee(employeeDTO, companyDTO)
Can this be done using Spring MVC?
Yes, this can be done. Here's a simple example (with Spring annotations) of a RESTful Controller:
#Controller
#RequestMapping("/someresource")
public class SomeController
{
#Autowired SomeService someService;
#RequestMapping(value="/{id}", method=RequestMethod.GET)
public String getResource(Model model, #PathVariable Integer id)
{
//get resource via someService and return to view
}
#RequestMapping(method=RequestMethod.POST)
public String saveResource(Model model, SomeResource someREsource)
{
//store resource via someService and return to view
}
#RequestMapping(value="/{id}", method=RequestMethod.PUT)
public String modifyResource(Model model, #PathVariable Integer id, SomeResource someResource)
{
//update resource with given identifier and given data via someService and return to view
}
#RequestMapping(value="/{id}", method=RequestMethod.DELETE)
public String deleteResource(Model model, #PathVariable Integer id)
{
//delete resource with given identifier via someService and return to view
}
}
Note that there are multiple ways of handling the incoming data from http-request (#RequestParam, #RequestBody, automatic mapping of post-parameters to a bean etc.). For longer and probably better explanations and tutorials, try googling for something like 'rest spring mvc' (without quotes).
Usually the clientside (browser) -stuff is done with JavaScript and AJAX, I'm a server-backend programmer and don't know lots about JavaScript, but there are lots of libraries available to help with it, for example see jQuery
See also:
REST in Spring 3 MVC
Yes, Rest is very easy to implement using spring MVC.
#RequestMapping(value="/saveEmploee.do",method = RequestMethod.POST)
#ResponseBody
public void saveEmployee(#RequestBody Class myclass){
//saving class.
//your class should be sent as JSON and will be deserialized by jackson
//bean which should be present in your Spring xml.
}