404 error when using spring to display jsp - java

the url that is being clicked in the home is
Edit
the method its accessing in the controller is
#GetMapping("/edit")
public ModelAndView editBrewery(#RequestParam("id") int id) {
return new ModelAndView("/editBrewery", "brewery", service.getBrewereriesID(id));
}
its not getting into the return in the #GetMapping method in the controller, i put a break point at the return and it goes into the 404 before it gets to the breakpoint
the utput that the tomcat console is giving is
WARNING: No mapping found for HTTP request with URI [/Assignment2/breweries/edit] in DispatcherServlet with name 'dispatcher'

Try just using Edit (without the previous url parts).

Related

Redirect to external URL from request mapping method in spring

Below is the request mapping method:
#GetMapping("/redirect")
public ResponseEntity<Void> redirect() {
String url = "http://yahoo.com";
return ResponseEntity.status(HttpStatus.FOUND)
.location(URI.create(url))
.build();
}
When I hit the URL http://somehost:8080/redirect in the browser I see that it takes me to yahoo.com, but when the /redirect is called from the UI(reactjs) the 302 Found httpstatus value is returned in the browser console but the page on the browser is blank. I was expecting to see the yahoo.com page. Seems it is not redirecting.
I referred this link: Redirect to an external URL from controller action in Spring MVC
reactjs code:
yield globalAxios.get(http://somehost:8080/redirect)
Below image when the http://somehost:8080/redirect gets called from the UI
Below image is when we the /redirect redirects to the link: yahoo.com
Is it because of the 405 method not allowed error as seen in the above image
Just in case if someone run into something like this in the future.
I end up using this code getting rid of 405 method not allowed while I am doing PUT-REDIRECT-GET pattern.
Notice it is #Controller and not #RestContorller. Otherwise it won't work.
If this is to be implemented in an existing rest controller you may want to add #ResponseBody over the other methods but not on these.
#Controller
#RequestMapping("/redirect")
public class RedirectController {
#PutMapping()
public String redirect() {
return "redirect:/redirect";
}
#GetMapping()
public String redirectPost() {
return "redirect:https://www.google.com";
}
}

Spring with Tomcat 7: PUT request returns 403 for strange resons

I have a web application written on Spring 3.1 (not boot) and running on Tomcat 7.
I have a #Controller implements method PUT on a certain URL.
In some cases When sending a PUT request from Postman, I get a 403 response instead of what is expected.
For example:
Sending the request to a non-implemented URL (on GET to the same URL I get a 404)
Sending an invalid JSON as the request body (Expected 400)
Sending a string instead of a numeric request parameter (Expected 400)
I also implement a filter that excepts all requests and just before the filter exists, I can verify I get the expected status from the rest of the chain.
This is an example of a controller code:
#RequestMapping(value = "/{book}", method = RequestMethod.PUT)
#ResponseStatus(HttpStatus.OK)
#ResponseBody
protected Book put(#PathVariable(value = "bookId") String id, #RequestBody #Valid Book book) {
return book; // just a stub
}
And this is the relevant part in the filter:
filterChain.doFilter(req, res);
// res.getStatus() is the expected status
return; // after this line I move to internal code of Tomcat which I cannot debug, but something happens there.
What do I miss?
Thanks
Check out CORS filter configuration first as Andreas said: https://tomcat.apache.org/tomcat-8.5-doc/config/filter.html
Check out this flowchart also https://tomcat.apache.org/tomcat-8.5-doc/images/cors-flowchart.png
Check out this stackoverflow post finally 403 on JSON PUT request to Tomcat with Spring 3.0.5 and Jackson
Your path variable value is bookId, but your url uses {book}; both should match. Try changing the url to "/{bookId}" or the path variable to #PathVariable(value = "book"). It might be useful to know the URL that you are calling to help analyse the issue.

Showing 404 in spring mvc

HTTP Status 404 - /SpringMVCHibernate/WEB-INF/views/user/userHome.jsp
shown in the webpage as error, even the page userHome.jsp is present in the correct package.
Thanking in Advance
You need to use the path given in #RequestMapping annotation on a controller method.Check for the controller method which returns userHome.jsp view.
Refer spring controller documentation
https://docs.spring.io/spring-framework/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-controller
The error message says for 404 that it's looking for userHome.jsp. And indeed, your controller returns the following view name: " userHome". But in your case shows that your file is naled userHome.jsp. The case matters.
Try returning the properly cased view name which matches the JSP userHome.jsp from the controller. Currently the controller returns Hello while the JSP is named userHome.jsp, causing the 404
#Controller
public class UserHomeController {
#RequestMapping(value = "/greeting")
public String sayHello (Model model) {
model.addAttribute("greeting", "Hello");
//return "Hello";
return "userhome";
}
}

RESTful error response Spring instead of tomcat page

I want to return a custom JSON as response body in case of errors. I tried doing this for 404 error code. I created a controller which looks like this.
#RequestMapping("/**")
#Controller
public class IOTExceptionController {
public void handleInvalidURL() throws ResourceNotFoundException {
throw new ResourceNotFoundException("Requested resource does not exist."
+ " Please check the URL.");
}
}
And the ControllerAdvice class I've setup will return the response as JSON.
So whatever URL which will not be found in other controllers will be handled here and 404 error is returned.
Unfortunately, the Tomcat error page for 404 is all I'm getting. Can anyone please let me know whether there is any flaw in this logic/code?

SpringMVC based Rest Service - StackOverflowError when path is not defined

I'm creating a project using the #RestController annotation of Spring 4. Everything is working fine, when I call a URL path that is configured I receive a well formatted answer or an error if something happened.
But, if I call a service in a path that is not defined, I'm getting the following exception:
java.lang.StackOverflowError
at javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:229)
at org.apache.catalina.core.ApplicationHttpRequest.getSession(ApplicationHttpRequest.java:592)
at javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:229)
at org.apache.catalina.core.ApplicationHttpRequest.getSession(ApplicationHttpRequest.java:592)
at javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:229)
at org.apache.catalina.core.ApplicationHttpRequest.getSession(ApplicationHttpRequest.java:592)
at javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:229)
I know this error is happening because my request could not be mapped to a resource, but I'm wondering why I am not getting a 404 - Not found error type?
My DispatcherServlet is configured like this:
ServletRegistration.Dynamic dispatcher =
servletContext.addServlet(DISPATCHER_SERVLET_NAME, new DispatcherServlet(rootContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
This is a sample of my Service classes:
#RestController
#RequestMapping("/Genotypes")
public class GenotypeService {
... #Autowired objects here ...
#RequestMapping(method=RequestMethod.GET)
public #ResponseBody List<Genotype> findGenotypesLike(#RequestParam(value="genName", required=false) String name, #RequestParam(value="like", defaultValue="false") Boolean like){
if(name == null){
return genotypeBO.findAllGenotypes();
}
return genotypeBO.findByName(name, like);
}
If I call http://localhost:8080/MyApp/Genotypes I get my response but if I call http://localhost:8080/MyApp/SomethingNotMapped the exception is thrown and the error code at the browser side is a 500 - Internal Server Error
By the way, my application is running on Apache Tomcat v7.0.
Thanks in advance for any suggestion.

Categories