Can't start Spring project - java

I want to process the request with the controller without any logic inside only to return html page. Here the class with configurations and starter class:
#Configuration
#EnableWebMvc
public class MVCConfig extends WebMvcConfigurerAdapter {
#Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/webapp/");
resolver.setSuffix(".html");
return resolver;
}
#Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/home").setViewName("NewPage");
}
}
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
#Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { MVCConfig.class };
}
#Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
#Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
Apparently that's not enough to do it because when trying to get the page in browser I recieve 404 error.
What do I must to add or change to make it work?

Just to remind you that except the configuration must be complicated,u also need to publish your project as a war and deploy to Tomcat or whatever.
(how to package project as a war,such as maven,use the command "package")
Hope that will help.

Related

Resources are not loaded in Spring MVC with Java config

Firstly I was used WebMvcConfigurerAdapter in RootConfig, and the resources were connected, but I didn't deploy the Spring Security project.
#Configuration
#ComponentScan({"org.example"})
public class RootConfig extends WebMvcConfigurerAdapter {
}
Then I deleted WebMvcConfigurerAdapter from the class RootConfig, and the project became deployable, but the resources stopped working.
#Configuration
#ComponentScan({"org.example"})
public class RootConfig {
}
project structure
How I connect styles:
<link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/index.css">
My resource handler:
#Configuration
#EnableWebMvc
#ComponentScan({
"org.example",
"org.example.model",
"org.example.repo",
"org.example.service"
})
public class WebConfig implements WebMvcConfigurer {
#Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
}
SpringMvcDispatcherServletInitializer:
public class SpringMvcDispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
#Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{RootConfig.class};
}
#Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{WebConfig.class};
}
#Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
try to override config method with web-security on resources like below and also if you are using thymleaf use # to represent URI href="#{/**/css/index.css}" or else check the path properly.
#Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/resources/*");
}
Use this syntax to access your css file
<link href="<c:url value="/resources/css/index.css />" rel="stylesheet">

Tomcat error - Status code 404, resource not available

I am creating basic REST application. I am getting requested resource not available when trying to run on Tomcat server. I am using Java configuration (no xml) in the project. I have tried all possible solutions available on Stackoverflow, none of them worked for me. I have been trying for almost 2 hours. I need help to solve this error.
HTTP Status 404 - The requested resource (/spring-rest-demo/test/hello) is not available
DemoRestController
//imports
#RestController
#RequestMapping("/test")
public class DemoRestController {
// add code for the "/hello" endpoint
#GetMapping("/hello")
public String sayHello() {
return "Hello World!";
}
}
MySpringMvcDispatcherServletInitializer
//imports
public class MySpringMvcDispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
#Override
protected Class<?>[] getRootConfigClasses() {
// TODO Auto-generated method stub
return null;
}
#Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { DemoAppConfig.class };
}
#Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
DemoAppConfig
//imports
#Configuration
#EnableWebMvc
#ComponentScan("com.springdemo")
public class DemoAppConfig implements WebMvcConfigurer {
}

How to configure Spring mvc for static html files?

I have a spring MVC application with JSP pages. I understand that JSPs are outdated technology and want to change them to HTML files. But all the guides and stacktrace questions I'm seeing are from 4 to 8+ years ago.
Here is my config:
public class InitWebApp extends AbstractAnnotationConfigDispatcherServletInitializer {
#Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] {ConfigWeb.class ,DataConfig.class, ConfigSecurity.class};
}
#Override
protected Class<?>[] getServletConfigClasses() {
return new Class[0];
}
#Override
protected String[] getServletMappings() {
return new String[] {"/"};
}
}
#Configuration
#EnableWebMvc
#ComponentScan({"Controller","Config","Services"})
public class ConfigWeb implements WebMvcConfigurer {
#Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("view/LoginPage");
registry.addViewController("/").setViewName("index");
registry.addViewController("/curator").setViewName("view/Curator/curatorHome");
registry.addViewController("/student").setViewName("view/Student/studentHome");
registry.addViewController("/change").setViewName("view/ProfileChange");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
#Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
registry.addResourceHandler("/profilePictures/**").addResourceLocations("file:///${fullPath}/resources/profilePictures/");
registry.addResourceHandler("/profilePictures/**").addResourceLocations("file:///${fullPath}/web-resources/profilePictures/");
}
#Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
I've tried to experiment with changing view resolvers and resourceHandlers. As soon as I managed to make my Tomcat server find my index.html and open it on the start, my controllers stopped working.
Could you please help me with the direction in which I should move on?

How to set the tracking-modes to none in Spring Boot [duplicate]

I am setting up a completely java based spring app with no xml config :
public class WebApp extends AbstractAnnotationConfigDispatcherServletInitializer {
#Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
#Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[]{WebMvcConfigurer.class};
}
#Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
and
#Configuration
#EnableWebMvc
#ComponentScan(basePackages = { mypackages })
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/static-assets/");
}
#Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
where do I put this, which used to be in my web.xml ?
<session-config>
<!-- Disables URL-based sessions (no more 'jsessionid' in the URL using Tomcat) -->
<tracking-mode>COOKIE</tracking-mode>
</session-config>
you can do it as in below
public class WebConfig implements WebApplicationInitializer {
#Override
public void onStartup(ServletContext servletContext)
throws ServletException {
HashSet<SessionTrackingMode> set = new HashSet<SessionTrackingMode>();
set.add(SessionTrackingMode.COOKIE);
servletContext.setSessionTrackingModes(set);
}
}
In a Spring Boot app, you can configure the mode using the application property server.session.tracking-modes.
In your application.properties add:
server.session.tracking-modes=cookie
Or if you use application.yml:
server:
session:
tracking-modes: 'cookie'
The Spring Boot autoconfiguration internally uses the same call to servletContext.setSessionTrackingModes which Bassem recommended in his answer.
UPDATE
In newer versions of Springboot, use
server.servlet.session.tracking-modes=cookie
Since 3.2.0.RC1 this is available in the AbstractSecurityWebApplicationInitializer like so:
public class WebSecutityInit extends AbstractSecurityWebApplicationInitializer {
#Override
protected Set<SessionTrackingMode> getSessionTrackingModes() {
return EnumSet.of(SessionTrackingMode.SSL);
}
}
As of spring boot 2.4.0 and later, the application property is renamed to server.servlet.session.tracking-modes.
So, in application.properties, need to add
server.servlet.session.tracking-modes="cookie"
If using application.yml, need to have
server:
servlet:
session:
tracking-modes: cookie
Another solution, that works for me, has been the code below inside the SecurityConfig class.
#Override
protected void configure(HttpSecurity http) throws Exception {
http.httpBasic()
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS) //No sessionId eppended
...
}

JavaConfig Spring Web Flow returns 404 not found (JSP)

I have been trying out the Java Configuration feature of Spring Web Flow 2.4 by modifying an existing project from xml configuration to JavaConfig. The XML version works, but JavaConfig doesn't. Every time I try to start the flow with URL http://localhost:8080/sia_p219_ch08_spring_web_flow_order_pizza_customer_flow_complete/pizza , it returns 404. There are no exceptions. The console show no "no request mapping found for..." message. The webpage shows The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
The project is hosted on github, the working XML version is here.
I think the problem is the request URL doesn't call the pizza flow (/WEB-INF/flows/pizza/pizza-flow.xml).
Here are some code snippets:
WebAppInitializer:
#Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { RootConfig.class };
}
#Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { WebConfig.class };
}
#Override
// map DispatcherServlet to /
protected String[] getServletMappings() {
return new String[] { "/" };
}
RootConfig:
#Configuration
#Import({WebFlowConfig.class})
public class RootConfig {}
WebFlowConfig:
#Configuration
#ComponentScan({"pizza"})
public class WebFlowConfig extends AbstractFlowConfiguration {
static{
System.out.println("WebFlowConfig loaded");
}
#Autowired
private WebConfig webMvcConfig;
#Bean
public FlowDefinitionRegistry flowRegistry() {
return
getFlowDefinitionRegistryBuilder(flowBuilderServices())
.setBasePath("/WEB-INF/flows")
.addFlowLocationPattern("/**/*-flow.xml")
.build();
}
#Bean
public FlowExecutor flowExecutor(){
return getFlowExecutorBuilder(flowRegistry()).build();
}
#Bean
public FlowBuilderServices flowBuilderServices() {
return getFlowBuilderServicesBuilder()
.setViewFactoryCreator(mvcViewFactoryCreator())
.setDevelopmentMode(true)
.build();
}
#Bean
public MvcViewFactoryCreator mvcViewFactoryCreator() {
MvcViewFactoryCreator factoryCreator = new MvcViewFactoryCreator();
factoryCreator.setViewResolvers(Collections.singletonList(this.webMvcConfig.viewResolver()));
factoryCreator.setUseSpringBeanBinding(true);
return factoryCreator;
}
}
WebConfig
#Configuration
#EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
static{
System.out.println("WebConfig loaded");
}
#Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/flows/");
resolver.setSuffix(".jsp");
return resolver;
}
// configure static content handling
#Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
The flow definition files and JSPs are fine and you can see them on github if you want.
Thanks a lot, any help is greatly appreciated.
What I've found so far that the configuration definitely lacks this part of configuration in WebFlowConfig (take a look at the documentation page for integration with Spring MVC for details):
#Bean
#Autowired
public FlowHandlerAdapter flowHandlerAdapter(FlowExecutor flowExecutor) {
FlowHandlerAdapter flowHandlerAdapter = new FlowHandlerAdapter();
flowHandlerAdapter.setFlowExecutor(flowExecutor);
return flowHandlerAdapter;
}
#Bean
#Autowired
public FlowHandlerMapping flowHandlerMapping(FlowDefinitionRegistry flowDefinitionRegistry) {
FlowHandlerMapping flowHandlerMapping = new FlowHandlerMapping();
flowHandlerMapping.setFlowRegistry(flowDefinitionRegistry);
flowHandlerMapping.setOrder(0);
return flowHandlerMapping;
}
Also remove mvcViewFactoryCreator definition and setViewFactoryCreator call from the flowBuilderServices bean definition as well. It works for me.

Categories

Resources