Spring MVC. No mapping found for HTTP request with URI - java

What is wrong with my views?
#EnableWebMvc
#Configuration
class WebMvcConfig extends WebMvcConfigurerAdapter {
#Bean
public ViewResolver getViewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/view/");
resolver.setSuffix(".jsp");
return resolver;
}
}
#Configuration
#ComponentScan(basePackages = "com.wordparty")
class AppConfig {
}
#Controller
class IndexController {
#RequestMapping(value = "/", method = RequestMethod.GET)
public String showIndex() {
return "index";
}
}
Here is my package structure
main
--java
----com.wordparty
...
--webapp
----WEB-INF
------view
--------index.jsp
I recieve 404 response. And in console I see message that my page wasn't found. If I write #ResponseBody then it returns text from controller without problems...
Where is a mistake?
P.S. I am using Spring 4 + Java 8

I solve the problem.. my dispatcher servlet has /* mapping instead of /

Related

415 Unsupported Media Type AngularJS to SpringMVC Controller

I'm getting this error when trying to post JSON data from angularjs controller to SpringMVC controller. I've tried a lot of solutions posted here and some other stuff available on the net as well. I already have the jackson library in my classpath. And also I'm not using maven because of internet issues.
SpringMVC Controller
#Controller
public class MainController {
#RequestMapping("/")
public String index() {
return "index";
}
#RequestMapping(value = "/employee", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public #ResponseBody
String saveEmployee(#RequestBody Employee employee) {
//Will do some stuff here.
System.out.println("INSIDE CONTROLLER");
StringBuilder json = new StringBuilder();
return json.toString();
}
}
AngularJS Controller
app.controller('saveEmployeeCtrl', function ($scope, $http) {
$scope.employee = {};
$scope.saveEmployee = function () {
$http({
method: 'POST',
url: 'employee',
data: $scope.employee,
headers:{'Accept':'application/json', 'Content': 'application/json'}
}).success(function(data){
console.log('something nice');
});
};
});
WebConfig
#EnableWebMvc
#Configuration
#ComponentScan("springmvc.com.")
public class WebConfig extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/webapp/resources/static/app/**")
.addResourceLocations("/webapp/resources/static/app/");
registry.addResourceHandler("/webapp/resources/static/lib/**")
.addResourceLocations("/webapp/resources/static/lib/");
registry.addResourceHandler("/webapp/resources/static/js/**")
.addResourceLocations("/webapp/resources/static/js/");
registry.addResourceHandler("/webapp/resources/static/css/**")
.addResourceLocations("/webapp/resources/static/css/");
registry.addResourceHandler("/webapp/webapp/resources/static/views/**")
.addResourceLocations("/webapp/webapp/resources/static/views/");
registry.addResourceHandler("/webapp/resources/static/**")
.addResourceLocations("/webapp/resources/static/");
}
#Override
public void configureContentNegotiation(
ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(false).favorParameter(true)
.parameterName("mediaType").ignoreAcceptHeader(true)
.useJaf(false).defaultContentType(MediaType.APPLICATION_JSON)
.mediaType("xml", MediaType.APPLICATION_XML)
.mediaType("json", MediaType.APPLICATION_JSON);
}
#Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}
#Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
WebAppInitializer
public class WebAppInitializer implements WebApplicationInitializer {
private static final String CONFIG_LOCATION = "springmvc.com.config";
#Override
public void onStartup(ServletContext servletContext) throws ServletException {
System.out.println("***** Initializing Application for " + servletContext.getServerInfo() + " *****");
// Create ApplicationContext
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.setConfigLocation(CONFIG_LOCATION);
// Add the servlet mapping manually and make it initialize automatically
DispatcherServlet dispatcherServlet = new DispatcherServlet(applicationContext);
ServletRegistration.Dynamic servlet = servletContext.addServlet("mvc-dispatcher", dispatcherServlet);
servlet.addMapping("/");
servlet.setAsyncSupported(true);
servlet.setLoadOnStartup(1);
}
}
You are sending header "Content" but you should send "Content-Type"
Do you send exactly the same fields in JSON as there are in Employee class, check if there are no additional fields, because Jackson has setting that it fail if unrecognized field is set. And there are some resolutions for this issue (like annotation on your class or change this setting)
Most important is what appear in the log file of your server application. What exception is raised as a cause of this http status. So i the solutions above not helping you, please check logs (maybe increase log level for spring) and post it here.
UPDATE:
I have few additional questions:
has your Employee class got default (non args) constructor or maybe you create only constructor with arguments? Could you post your Employee class.
Do you have any logger attached to your project, is there anything in log file, (if there is, please post it)?

spring boot /WEB-INF doesn't work

#SpringBootApplication
#Controller
public class App {
#RequestMapping("/aaa")
String home(HttpServletRequest request) {
return "nihao";
}
#Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/res/");
resolver.setSuffix(".html");
return resolver;
}
}
This code works fine. The path of 'nihao.html' is in '/static/res/nihao.html'. But when I change the prefix to "/WEB-INF/", and create /WEB-INF/nihao.html file, I've got a 404 no message available error. and I've got the same error when I change the prefix to to /static/WEB-INF/" and create requisite file in that directory. I need someone to help me figure out. All the path is relative to the context path.

Problems with controller mapping in Spring MVC

There are similar topics, but they all use xml configuration files. The reason why I'm writing this question is that I'm using annotations.
I experience problems running my app:
getting “WARN org.springframework.web.servlet.PageNotFound - No
mapping found for HTTP request with URI …” when trying to setup
Spring servlet
getting error 404 when trying to run it on server
Here is my code (package and imports are skipped):
1) initializer
public class WebInitializer implements WebApplicationInitializer{
#Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx =
new AnnotationConfigWebApplicationContext();
ctx.register(AppConfig.class);
ctx.setServletContext(servletContext);
ServletRegistration.Dynamic servlet =
servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
}
}
2) app config
#Configuration
#ComponentScan("ua.kiev.prog")
#EnableWebMvc
public class AppConfig {
#Bean
public EntityManager entityManager() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("AdvJPA");
return emf.createEntityManager();
}
#Bean
public AdvDAO advDAO() {
return new AdvDAOImpl();
}
#Bean
public UrlBasedViewResolver setupViewResolver() {
UrlBasedViewResolver resolver = new UrlBasedViewResolver();
resolver.setPrefix("/WEB-INF/pages/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
resolver.setOrder(1);
return resolver;
}
#Bean
public CommonsMultipartResolver multipartResolver() {
return new CommonsMultipartResolver();
}
}
3) controller
#Controller
#RequestMapping("/Advertisement")
public class MainController {
#Autowired
private AdvDAO advDAO;
#RequestMapping("/")
public ModelAndView listAdvs() {
return new ModelAndView("index", "advs", advDAO.list());
}
#RequestMapping(value = "/add_page", method = RequestMethod.POST)
public String addPage(Model model) {
return "add_page";
}
#RequestMapping(value = "/search", method = RequestMethod.POST)
public ModelAndView search(#RequestParam(value="pattern") String pattern) {
return new ModelAndView("index", "advs", advDAO.list(pattern));
}
// more code goes here
}
The controller is mapped to /Advertisement, so app should be available at URL localhost:8080/Advertisement/ but it isn't. When I change mapping in annotation to "/" - it becomes available at localhost:8080/Advertisement/. How can it be?
And when I change it back to "/Advertisement" - the same probleb accurs (error 404 and exception "No mapping found for HTTP request with URI …")
So, where I've made a mistake in my code?
Or maybe the problem is in Eclipse/TomCat/Maven?
Source - https://github.com/KostyantynPanchenko/prog.kiev.ua.lesson09.adv
You should change mapping
#Controller
#RequestMapping("/")
public class MainController {
#Autowired
private AdvDAO advDAO;
#RequestMapping("/Advertisement")
public ModelAndView listAdvs() {
return new ModelAndView("index", "advs", advDAO.list());
}
The mistake that a mapper used the value from the annotation to match the request URL, and it can't match the last slash. Note, it should not happen in the above code.
How are you running the application? Atleast in tomcat each deployed application is served from specific context path. Context path is determined from the base file name, more on that here.
So if you're deploying Advertisement.war all requests to the app will be served from localhost:8080/Advertisement/ even though you're declaring the DispatcherServlet and Controller to /

JspViewResolver doesn't work in java configuration - based spring applciation

I have only Java-based configuration in Spring Application. I have created user entity and Dao for finding / creating users.
The controller has two GET methods - one for user and one for users list and one POST method - for creating / editing a user.
After running at Tomcat 7 server it gives 404 error and message that 'The requested resource is not available.
Dispatcher servlet is configured in SpringWebAppInitializer.class:
public class SpringWebAppInitializer implements WebApplicationInitializer {
#Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
appContext.register(ApplicationContextConfig.class);
servletContext.addListener(new ContextLoaderListener(appContext));
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
dispatcherContext.register(DispatcherServlet.class);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("SpringDispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
View resolver is defined in ApplicationContextConfig.class:
#Configuration
#ComponentScan("pl.wybornie.entity")
#EnableTransactionManagement
public class ApplicationContextConfig extends WebMvcConfigurerAdapter {
#Bean(name = "viewResolver")
public InternalResourceViewResolver getViewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/pages/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
//other configuration beans...
}
Jsp files for userList and userForm are in WebContent project's folder under: /WEB-INF/pages/user/*.jsp
Controllers responsible for geting views are pasted below:
#Controller
#SessionAttributes({"user"})
public class UserController {
#Autowired
private UserDao userDao;
#RequestMapping(value = "/user_list.html", method = RequestMethod.GET)
public String list(Model model, HttpServletRequest request) {
model.addAttribute("userList", userDao.usersList());
return "user/userList";
}
#RequestMapping(value = "/create.html", method = RequestMethod.GET)
public String edit(#RequestParam(value="id", required=false) Long id, Model model) {
User user = userDao.findOrCreate(id);
model.addAttribute("user", user);
return "user/userForm";
}
#RequestMapping(method = RequestMethod.POST)
public String submitForm(#ModelAttribute("user") User user, BindingResult bindingResult, HttpServletRequest request, Model model) {
userDao.saveOrUpdate(user);
return "redirect:user_list.html";
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
Your class should extend WebMvcConfigurerAdapter class.
#Configuration
#ComponentScan("pl.wybornie.entity")
#EnableWebMvc
public class ApplicationContextConfig extends WebMvcConfigurerAdapter{
}

Spring - Thymeleaf - 404 Error

I'm worked with Spring in the past on a big project, but I've never started a Spring MVC web app from scratch.
Well that's what I'm currently doing now for practice as I have a project coming up that will require it.
I was successfully able to make a simple Spring MVC Web App that used .JSP pages (using annotations, no XML).
I wanted to use Thymeleaf though and started my conversion process to that.
Well now I'm getting a 404 Error and my HomeController class isn't even being hit it seems like.
I get no errors in the Console output.
I've Google search, read through tutorials, and code samples. Second pair of eyes would be nice. Thanks! :)
Note: going from .JSP to Thymeleaf the only changes made was the addition of the ThymeleafConfig class. I don't see how it went from working to not working.
Here's my code:
WebInit.java
public class WebInit implements WebApplicationInitializer {
#Override
public void onStartup(ServletContext servletContext) throws ServletException {
// Creates the root application context
AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
appContext.register(ServletConfig.class);
appContext.setDisplayName("REPLACE ME");
appContext.setConfigLocation("com.demo.config");
// Creates the Spring Container shared by all Servlets and Filters
servletContext.addListener(new ContextLoaderListener(appContext));
// Further configures the servlet context
ServletRegistration.Dynamic dispatcher = servletContext.addServlet(
"dispatcher", new DispatcherServlet(appContext));
dispatcher.setLoadOnStartup(1);
dispatcher.setAsyncSupported(true);
dispatcher.addMapping("/");
}
}
ServetConfig.java
#Configuration
#Import(WebConfig.class)
#ImportResource({/*"classpath:META-INF/spring/persistence-context.xml"*/})
public class ServletConfig {
}
WebConfig.java
#Configuration
#ComponentScan("com.illinois.dnr")
#EnableAspectJAutoProxy
#EnableWebMvc
#Import({ThymeleafConfig.class})
public class WebConfig extends WebMvcConfigurerAdapter {
// Maps resources path to webapp/resources
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations(
"/resources/");
}
// Provides internationalization of messages
#Bean
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource source = new ResourceBundleMessageSource();
source.setBasename("messages");
return source;
}
}
Thymeleaf.java
#Configuration
public class ThymeleafConfig {
#Bean
public ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".html");
resolver.setTemplateMode("HTML5");
resolver.setOrder(1);
return resolver;
}
#Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setTemplateResolver(templateResolver());
return engine;
}
#Bean
public ThymeleafViewResolver thymeleafViewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
return resolver;
}
}
HomeController.java
#Controller
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
#RequestMapping(value = "/dnr", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
logger.info("Welcome home! The client locale is {}.", locale);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG,
DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate);
return "home";
}
#RequestMapping(value = "/login", method = RequestMethod.GET)
public String loginPage(Locale locale, Model model) {
logger.info("Login");
return "login";
}
#RequestMapping(value = "/home", method = RequestMethod.POST)
public String login(#Validated User user, Model model) {
model.addAttribute("userName", user.getUserName());
logger.info("User");
return "user";
}
#RequestMapping(value = { "/", "/welcome**" }, method = RequestMethod.GET)
public ModelAndView welcomePage() {
ModelAndView model = new ModelAndView();
model.addObject("title", "Spring Security Hello World");
model.addObject("message", "This is welcome page!");
model.setViewName("hello");
logger.info("Hello");
return model;
}
#RequestMapping(value = "/admin**", method = RequestMethod.GET)
public ModelAndView adminPage() {
ModelAndView model = new ModelAndView();
model.addObject("title", "Spring Security Hello World");
model.addObject("message", "This is protected page - Admin Page!");
model.setViewName("admin");
logger.info("admin");
return model;
}
#RequestMapping(value = "/dba**", method = RequestMethod.GET)
public ModelAndView dbaPage() {
ModelAndView model = new ModelAndView();
model.addObject("title", "Spring Security Hello World");
model.addObject("message", "This is protected page - Database Page!");
model.setViewName("admin");
logger.info("dba");
return model;
}
}
I'm not sure if it helps but you can simplified WebInit by extendsAbstractAnnotationConfigDispatcherServletInitializer instead of implements WebApplicationInitializer. Otherwise Thymeleaf configuration seems ok to me (suppose it's called). I'm giving here working skeleton of config so you can experiment with it.
WebApplicationInitializer
public class WebInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
#Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
#Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { WebConfig.class };
}
#Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
WebConfig
#Configuration
#EnableWebMvc
#ComponentScan("com.kreuzman")
public class WebConfig {
#Bean
public ITemplateResolver templateResolver() {
TemplateResolver resolver = new ServletContextTemplateResolver();
resolver.setPrefix("/templates/");
resolver.setSuffix(".html");
resolver.setTemplateMode("HTML5");
resolver.setCacheTTLMs(0l);
return resolver;
}
#Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setTemplateResolver(templateResolver());
return engine;
}
#Bean
public ViewResolver thymeleafViewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setCharacterEncoding("UTF-8");
return viewResolver;
}
}
What page are you trying to hit? Does that html page exist in /WEB-INF/views/?
Is it a JSP page? you need to exclude all pages that are not going to be resolved by ThymeleafViewResolver. I also noticed you didn't set the ThymeleafViewResolver to first in the order.
Like so
#Bean
public ThymeleafViewResolver thymeleafViewResolver()
{
String[] excludedViews = new String[]{
"login", "logout"};
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
/*
* This is how we get around Thymeleaf view resolvers throwing an error instead of returning
* of null and allowing the next view resolver in the {#see
* DispatcherServlet#resolveViewName(String, Map<String, Object>, Locale,
* HttpServletRequest)} to resolve the view.
*/
resolver.setExcludedViewNames(excludedViews);
resolver.setOrder(1);
return resolver;
}

Categories