I have a profile update page which is supposed to show errors on the fields.
However, when I remove the value from an input field instead of getting an error below, I get 405 - Request method 'PATCH' not supported.
I have no idea what could be the problem, any advice is appreciated.
#Controller
#RequestMapping("/users")
public class UserController {
#GetMapping("/profile/update/{id}")
public String showUpdateForm(#PathVariable("id") Long id, Model model) {
ProfileUpdateServiceModel profileServiceModel = this.userService.getProfileUpdateServiceModelById(id);
ProfileUpdateBindingModel profileUpdateBindingModel = this.mapper.map(profileServiceModel, ProfileUpdateBindingModel.class);
if (!model.containsAttribute("profileUpdateBindingModel")) {
model.addAttribute("profileUpdateBindingModel", profileUpdateBindingModel);
}
// model.addAttribute("profileUpdateBindingModel", profileUpdateBindingModel);
return "update-profile";
}
#PatchMapping("/profile/update/{id}")
public String update(#PathVariable("id") Long id,
#Valid ProfileUpdateBindingModel profileUpdateBindingModel,
BindingResult br,
RedirectAttributes rAtt) {
if (br.hasErrors()) {
rAtt
.addFlashAttribute("profileUpdateBindingModel", profileUpdateBindingModel)
.addFlashAttribute("org.springframework.validation.BindingResult.profileUpdateBindingModel", br);
return "redirect:/users/profile/update/" + id;
}
return "profile";
}
profile.html:
<form
th:method="GET"
th:action="#{/users/profile/update/{id}(id=${session.currentUserId})}">
update-profile.html:
<form
th:action="#{/users/profile/update/{id}(id=*{userId})}"
th:method="PATCH"
th:object="${profileUpdateBindingModel}"
enctype="multipart/form-data">
<div class="col-sm-6">
<label class="label-align">First Name<span
class="required">*</span></label>
<input
th:field="*{firstName}"
type="text"
class="form-control"
name="name"/>
<div class="col-auto">
<small th:if="${#fields.hasErrors('firstName')}"
th:errors="*{firstName}"
id="first-nameError"
class="text-light form-text bg-danger rounded">xxx</small>
</div>
</div>
LOGS:
2022-03-06 11:57:45.050 DEBUG 9492 --- [nio-8080-exec-6] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, application/signed-exchange;v=b3;q=0.9, */*;q=0.8]
2022-03-06 11:57:45.077 DEBUG 9492 --- [nio-8080-exec-6] o.s.web.servlet.DispatcherServlet : Completed 200 OK
2022-03-06 11:57:45.131 DEBUG 9492 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet : GET "/css/style.css", parameters={}
2022-03-06 11:57:45.131 DEBUG 9492 --- [nio-8080-exec-7] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [classpath [static/], ServletContext [/]]
2022-03-06 11:57:45.135 DEBUG 9492 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet : Completed 200 OK
2022-03-06 11:57:45.138 DEBUG 9492 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : GET "/images/undraw_profile_1.svg", parameters={}
2022-03-06 11:57:45.138 DEBUG 9492 --- [nio-8080-exec-3] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [classpath [static/], ServletContext [/]]
2022-03-06 11:57:45.138 DEBUG 9492 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : Completed 200 OK
2022-03-06 11:57:45.168 DEBUG 9492 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet : GET "/images/1915.jpg", parameters={}
2022-03-06 11:57:45.169 DEBUG 9492 --- [nio-8080-exec-9] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [classpath [static/], ServletContext [/]]
2022-03-06 11:57:45.178 DEBUG 9492 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet : Completed 200 OK
2022-03-06 11:57:59.981 DEBUG 9492 --- [nio-8080-exec-6] o.s.web.servlet.DispatcherServlet : PATCH "/users/profile/update/", parameters={multipart}
2022-03-06 11:57:59.984 WARN 9492 --- [nio-8080-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PATCH' not supported]
2022-03-06 11:57:59.984 DEBUG 9492 --- [nio-8080-exec-6] o.s.web.servlet.DispatcherServlet : Completed 405 METHOD_NOT_ALLOWED
2022-03-06 11:57:59.986 DEBUG 9492 --- [nio-8080-exec-6] o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for POST "/error", parameters={multipart}
2022-03-06 11:57:59.986 DEBUG 9492 --- [nio-8080-exec-6] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
2022-03-06 11:57:59.995 DEBUG 9492 --- [nio-8080-exec-6] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, text/html;q=0.8]
2022-03-06 11:58:00.022 DEBUG 9492 --- [nio-8080-exec-6] o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 405
2022-03-06 11:58:00.077 DEBUG 9492 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet : GET "/css/style.css", parameters={}
2022-03-06 11:58:00.078 DEBUG 9492 --- [nio-8080-exec-7] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [classpath [static/], ServletContext [/]]
2022-03-06 11:58:00.083 DEBUG 9492 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet : Completed 200 OK
2022-03-06 11:58:00.132 DEBUG 9492 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : GET "/images/undraw_profile_1.svg", parameters={}
2022-03-06 11:58:00.134 DEBUG 9492 --- [nio-8080-exec-3] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [classpath [static/], ServletContext [/]]
2022-03-06 11:58:00.137 DEBUG 9492 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet : Completed 200 OK
HTTP Code 405 means Method not Allowed. In your spring you are mentioning GET method but in your Form you are mentioning to submit a PATCH method.
Change your form to
<form
th:action="#{/users/profile/update/{id}(id=*{userId})}"
th:method="GET"
th:object="${profileUpdateBindingModel}"
enctype="multipart/form-data">
I'd use the pathvariable like this :
#PathVariable Long id
or like this ::
#PatchMapping(params = "/{id}", consumes = "application/json")
I am not confident about thymeleaf, but you can check this syntax[in second html page]:
th:action="#{/users/profile/update/{id}(id=*{userId})}"
As in your home.html form, you have assigned the value using $ sign but here it is in *.
Also, make sure your forms are enclosed properly with </form> tags.
Related
I created a microservice project that consist of a few services. In one of these services, I added an HTTP Filter to get user id in the request header that comes from API gateway. But after adding this filter, the application gives 404 not found error. But whenever I put a breakpoint to the target endpoint, I can see the request comes sucessfully but gives 404-not found.
For 2 days, I've tried to find a solution but nothing did work. Do you have any idea why this eror occures?
My web security config
#Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
#Autowired
private HttpFilter httpFilter;
#Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.antMatchers("/**")
.permitAll()
.anyRequest()
.authenticated();
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.addFilterBefore(httpFilter, UsernamePasswordAuthenticationFilter.class);
}
}
HttpFilter class
#Component
public class HttpFilter extends OncePerRequestFilter {
#Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
String userId = request.getHeader("x-auth-user-id");
if(userId !=null){
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(
userId,null,null
);
SecurityContextHolder.getContext().setAuthentication(authenticationToken);
}
filterChain.doFilter(request,response);
}
}
LOGS
2022-04-01 00:39:49.734 DEBUG 6384 --- [nio-8080-exec-2] o.a.t.util.http.Rfc6265CookieProcessor : Cookies: Parsing b[]: JSESSIONID=F04A8A4650623E59586BA5D0B3AFFB78
2022-04-01 00:39:49.734 DEBUG 6384 --- [nio-8080-exec-2] o.a.catalina.connector.CoyoteAdapter : Requested cookie session id is F04A8A4650623E59586BA5D0B3AFFB78
2022-04-01 00:39:49.735 DEBUG 6384 --- [nio-8080-exec-2] o.a.c.authenticator.AuthenticatorBase : Security checking request GET /api/message/getAll
2022-04-01 00:39:49.735 DEBUG 6384 --- [nio-8080-exec-2] org.apache.catalina.realm.RealmBase : No applicable constraints defined
2022-04-01 00:39:49.735 DEBUG 6384 --- [nio-8080-exec-2] o.a.c.authenticator.AuthenticatorBase : Not subject to any constraint
2022-04-01 00:39:49.735 DEBUG 6384 --- [nio-8080-exec-2] o.s.security.web.FilterChainProxy : Securing GET /api/message/getAll
2022-04-01 00:39:49.735 DEBUG 6384 --- [nio-8080-exec-2] w.c.HttpSessionSecurityContextRepository : Retrieved SecurityContextImpl [Authentication=UsernamePasswordAuthenticationToken [Principal=1, Credentials=[PROTECTED], Authenticated=true, Details=null, Granted Authorities=[]]]
2022-04-01 00:39:49.735 DEBUG 6384 --- [nio-8080-exec-2] s.s.w.c.SecurityContextPersistenceFilter : Set SecurityContextHolder to SecurityContextImpl [Authentication=UsernamePasswordAuthenticationToken [Principal=1, Credentials=[PROTECTED], Authenticated=true, Details=null, Granted Authorities=[]]]
2022-04-01 00:39:49.736 DEBUG 6384 --- [nio-8080-exec-2] o.s.s.w.a.i.FilterSecurityInterceptor : Authorized filter invocation [GET /api/message/getAll] with attributes [permitAll]
2022-04-01 00:39:49.736 DEBUG 6384 --- [nio-8080-exec-2] o.s.security.web.FilterChainProxy : Secured GET /api/message/getAll
2022-04-01 00:39:49.737 DEBUG 6384 --- [nio-8080-exec-2] org.apache.tomcat.util.http.Parameters : Set encoding to UTF-8
2022-04-01 00:39:49.737 DEBUG 6384 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : GET "/api/message/getAll", parameters={}
2022-04-01 00:39:49.737 DEBUG 6384 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.speakiv.socialservice.api.MessageApi#getAll()
2022-04-01 00:39:49.737 DEBUG 6384 --- [nio-8080-exec-2] o.j.s.OpenEntityManagerInViewInterceptor : Opening JPA EntityManager in OpenEntityManagerInViewInterceptor
2022-04-01 00:39:49.738 DEBUG 6384 --- [nio-8080-exec-2] o.s.orm.jpa.JpaTransactionManager : Found thread-bound EntityManager [SessionImpl(494638892<open>)] for JPA transaction
2022-04-01 00:39:49.739 DEBUG 6384 --- [nio-8080-exec-2] o.s.orm.jpa.JpaTransactionManager : Creating new transaction with name [org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly
2022-04-01 00:39:49.747 DEBUG 6384 --- [nio-8080-exec-2] o.s.jdbc.datasource.DataSourceUtils : Setting JDBC Connection [HikariProxyConnection#1484327655 wrapping com.mysql.cj.jdbc.ConnectionImpl#47fa3671] read-only
2022-04-01 00:39:49.751 DEBUG 6384 --- [nio-8080-exec-2] o.h.e.t.internal.TransactionImpl : On TransactionImpl creation, JpaCompliance#isJpaTransactionComplianceEnabled == false
2022-04-01 00:39:49.751 DEBUG 6384 --- [nio-8080-exec-2] o.h.e.t.internal.TransactionImpl : begin
2022-04-01 00:39:49.753 DEBUG 6384 --- [nio-8080-exec-2] o.s.orm.jpa.JpaTransactionManager : Exposing JPA transaction as JDBC [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle#50106ca8]
2022-04-01 00:39:49.753 DEBUG 6384 --- [nio-8080-exec-2] o.h.q.c.internal.CriteriaQueryImpl : Rendered criteria query -> select generatedAlias0 from UserMessage as generatedAlias0
2022-04-01 00:39:49.754 DEBUG 6384 --- [nio-8080-exec-2] org.hibernate.SQL : select usermessag0_.id as id1_5_, usermessag0_.date as date2_5_, usermessag0_.message as message3_5_, usermessag0_.receiver_id as receiver4_5_, usermessag0_.sender_id as sender_i5_5_ from user_messages usermessag0_
2022-04-01 00:39:49.760 DEBUG 6384 --- [nio-8080-exec-2] org.hibernate.loader.Loader : Result set row: 0
2022-04-01 00:39:49.760 DEBUG 6384 --- [nio-8080-exec-2] org.hibernate.loader.Loader : Result row: EntityKey[com.speakiv.socialservice.model.entity.UserMessage#1]
2022-04-01 00:39:49.765 DEBUG 6384 --- [nio-8080-exec-2] o.h.engine.internal.TwoPhaseLoad : Resolving attributes for [com.speakiv.socialservice.model.entity.UserMessage#1]
2022-04-01 00:39:49.765 DEBUG 6384 --- [nio-8080-exec-2] o.h.engine.internal.TwoPhaseLoad : Processing attribute `date` : value = 2022-03-28 13:46:10.319
2022-04-01 00:39:49.765 DEBUG 6384 --- [nio-8080-exec-2] o.h.engine.internal.TwoPhaseLoad : Attribute (`date`) - enhanced for lazy-loading? - false
2022-04-01 00:39:49.765 DEBUG 6384 --- [nio-8080-exec-2] o.h.engine.internal.TwoPhaseLoad : Processing attribute `message` : value = Hello world
2022-04-01 00:39:49.765 DEBUG 6384 --- [nio-8080-exec-2] o.h.engine.internal.TwoPhaseLoad : Attribute (`message`) - enhanced for lazy-loading? - false
2022-04-01 00:39:49.765 DEBUG 6384 --- [nio-8080-exec-2] o.h.engine.internal.TwoPhaseLoad : Processing attribute `receiverId` : value = 1
2022-04-01 00:39:49.765 DEBUG 6384 --- [nio-8080-exec-2] o.h.engine.internal.TwoPhaseLoad : Attribute (`receiverId`) - enhanced for lazy-loading? - false
2022-04-01 00:39:49.765 DEBUG 6384 --- [nio-8080-exec-2] o.h.engine.internal.TwoPhaseLoad : Processing attribute `senderId` : value = 1
2022-04-01 00:39:49.765 DEBUG 6384 --- [nio-8080-exec-2] o.h.engine.internal.TwoPhaseLoad : Attribute (`senderId`) - enhanced for lazy-loading? - false
2022-04-01 00:39:49.766 DEBUG 6384 --- [nio-8080-exec-2] o.h.engine.internal.TwoPhaseLoad : Done materializing entity [com.speakiv.socialservice.model.entity.UserMessage#1]
2022-04-01 00:39:49.766 DEBUG 6384 --- [nio-8080-exec-2] o.h.engine.internal.TwoPhaseLoad : Resolving attributes for [com.speakiv.socialservice.model.entity.UserMessage#2]
2022-04-01 00:39:49.766 DEBUG 6384 --- [nio-8080-exec-2] o.h.engine.internal.TwoPhaseLoad : Processing attribute `date` : value = 2022-03-28 13:46:25.956
2022-04-01 00:39:49.769 DEBUG 6384 --- [nio-8080-exec-2] o.h.engine.internal.TwoPhaseLoad : Done materializing entity [com.speakiv.socialservice.model.entity.UserMessage#13]
2022-04-01 00:39:49.769 DEBUG 6384 --- [nio-8080-exec-2] o.s.orm.jpa.JpaTransactionManager : Initiating transaction commit
2022-04-01 00:39:49.769 DEBUG 6384 --- [nio-8080-exec-2] o.s.orm.jpa.JpaTransactionManager : Committing JPA transaction on EntityManager [SessionImpl(494638892<open>)]
2022-04-01 00:39:49.769 DEBUG 6384 --- [nio-8080-exec-2] o.h.e.t.internal.TransactionImpl : committing
2022-04-01 00:39:49.777 DEBUG 6384 --- [nio-8080-exec-2] o.s.jdbc.datasource.DataSourceUtils : Resetting read-only flag of JDBC Connection [HikariProxyConnection#1484327655 wrapping com.mysql.cj.jdbc.ConnectionImpl#47fa3671]
2022-04-01 00:39:49.780 DEBUG 6384 --- [nio-8080-exec-2] o.s.orm.jpa.JpaTransactionManager : Not closing pre-bound JPA EntityManager after transaction
2022-04-01 00:39:49.781 DEBUG 6384 --- [nio-8080-exec-2] o.s.w.s.v.ContentNegotiatingViewResolver : Selected '*/*' given [*/*]
2022-04-01 00:39:49.781 DEBUG 6384 --- [nio-8080-exec-2] o.s.w.servlet.view.InternalResourceView : View name 'api/message/getAll', model {dataResponse=com.speakiv.speakivcore.model.response.SuccessDataResponse#3d2c9c93, org.springframework.validation.BindingResult.dataResponse=org.springframework.validation.BeanPropertyBindingResult: 0 errors}
2022-04-01 00:39:49.781 DEBUG 6384 --- [nio-8080-exec-2] o.s.w.servlet.view.InternalResourceView : Forwarding to [api/message/getAll]
2022-04-01 00:39:49.782 DEBUG 6384 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : "FORWARD" dispatch for GET "/api/message/api/message/getAll", parameters={}
2022-04-01 00:39:49.782 DEBUG 6384 --- [nio-8080-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [Classpath [META-INF/resources/], Classpath [resources/], Classpath [static/], Classpath [public/], ServletContext [/]]
2022-04-01 00:39:49.787 DEBUG 6384 --- [nio-8080-exec-2] o.s.w.s.r.ResourceHttpRequestHandler : Resource not found
2022-04-01 00:39:49.787 DEBUG 6384 --- [nio-8080-exec-2] w.c.HttpSessionSecurityContextRepository : Stored SecurityContextImpl [Authentication=UsernamePasswordAuthenticationToken [Principal=1, Credentials=[PROTECTED], Authenticated=true, Details=null, Granted Authorities=[]]] to HttpSession [org.apache.catalina.session.StandardSessionFacade#11a677b0]
2022-04-01 00:39:49.787 DEBUG 6384 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Exiting from "FORWARD" dispatch, status 404
2022-04-01 00:39:49.788 DEBUG 6384 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Disabling the response for further output
2022-04-01 00:39:49.788 DEBUG 6384 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : The Response is vehiculed using a wrapper: org.springframework.security.web.header.HeaderWriterFilter$HeaderWriterResponse
2022-04-01 00:39:49.789 DEBUG 6384 --- [nio-8080-exec-2] o.j.s.OpenEntityManagerInViewInterceptor : Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
2022-04-01 00:39:49.789 DEBUG 6384 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Completed 404 NOT_FOUND
2022-04-01 00:39:49.790 DEBUG 6384 --- [nio-8080-exec-2] w.c.HttpSessionSecurityContextRepository : Stored SecurityContextImpl [Authentication=UsernamePasswordAuthenticationToken [Principal=1, Credentials=[PROTECTED], Authenticated=true, Details=null, Granted Authorities=[]]] to HttpSession [org.apache.catalina.session.StandardSessionFacade#11a677b0]
2022-04-01 00:39:49.790 DEBUG 6384 --- [nio-8080-exec-2] s.s.w.c.SecurityContextPersistenceFilter : Cleared SecurityContextHolder to complete request
2022-04-01 00:39:49.790 DEBUG 6384 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost] : Processing ErrorPage[errorCode=0, location=/error]
2022-04-01 00:39:49.791 DEBUG 6384 --- [nio-8080-exec-2] o.s.security.web.FilterChainProxy : Securing GET /error
2022-04-01 00:39:49.792 DEBUG 6384 --- [nio-8080-exec-2] w.c.HttpSessionSecurityContextRepository : Retrieved SecurityContextImpl [Authentication=UsernamePasswordAuthenticationToken [Principal=1, Credentials=[PROTECTED], Authenticated=true, Details=null, Granted Authorities=[]]]
2022-04-01 00:39:49.792 DEBUG 6384 --- [nio-8080-exec-2] s.s.w.c.SecurityContextPersistenceFilter : Set SecurityContextHolder to SecurityContextImpl [Authentication=UsernamePasswordAuthenticationToken [Principal=1, Credentials=[PROTECTED], Authenticated=true, Details=null, Granted Authorities=[]]]
2022-04-01 00:39:49.792 DEBUG 6384 --- [nio-8080-exec-2] o.s.security.web.FilterChainProxy : Secured GET /error
2022-04-01 00:39:49.793 DEBUG 6384 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for GET "/error", parameters={}
2022-04-01 00:39:49.793 DEBUG 6384 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)
2022-04-01 00:39:49.793 DEBUG 6384 --- [nio-8080-exec-2] o.j.s.OpenEntityManagerInViewInterceptor : Opening JPA EntityManager in OpenEntityManagerInViewInterceptor
2022-04-01 00:39:49.795 DEBUG 6384 --- [nio-8080-exec-2] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Using 'application/json', given [*/*] and supported [application/json, application/*+json, application/json, application/*+json]
2022-04-01 00:39:49.795 DEBUG 6384 --- [nio-8080-exec-2] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Writing [{timestamp=Fri Apr 01 00:39:49 EET 2022, status=404, error=Not Found, path=/api/message/getAll}]
2022-04-01 00:39:49.796 DEBUG 6384 --- [nio-8080-exec-2] o.j.s.OpenEntityManagerInViewInterceptor : Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
2022-04-01 00:39:49.797 DEBUG 6384 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 404
2022-04-01 00:39:49.798 DEBUG 6384 --- [nio-8080-exec-2] s.s.w.c.SecurityContextPersistenceFilter : Cleared SecurityContextHolder to complete request
2022-04-01 00:39:49.798 DEBUG 6384 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Disabling the response for further output
2022-04-01 00:39:49.799 DEBUG 6384 --- [nio-8080-exec-2] o.a.coyote.http11.Http11InputBuffer : Before fill(): parsingHeader: [true], parsingRequestLine: [true], parsingRequestLinePhase: [0], parsingRequestLineStart: [0], byteBuffer.position(): [0], byteBuffer.limit(): [0], end: [678]
2022-04-01 00:39:49.799 DEBUG 6384 --- [nio-8080-exec-2] o.a.tomcat.util.net.SocketWrapperBase : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper#2efc81a4:org.apache.tomcat.util.net.NioChannel#4fd730ea:java.nio.channels.SocketChannel[connected local=/192.168.1.110:8080 remote=/192.168.1.110:52685]], Read from buffer: [0]
2022-04-01 00:39:49.799 DEBUG 6384 --- [nio-8080-exec-2] org.apache.tomcat.util.net.NioEndpoint : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper#2efc81a4:org.apache.tomcat.util.net.NioChannel#4fd730ea:java.nio.channels.SocketChannel[connected local=/192.168.1.110:8080 remote=/192.168.1.110:52685]], Read direct from socket: [0]
2022-04-01 00:39:49.799 DEBUG 6384 --- [nio-8080-exec-2] o.a.coyote.http11.Http11InputBuffer : Received []
2022-04-01 00:39:49.799 DEBUG 6384 --- [nio-8080-exec-2] o.apache.coyote.http11.Http11Processor : Socket: [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper#2efc81a4:org.apache.tomcat.util.net.NioChannel#4fd730ea:java.nio.channels.SocketChannel[connected local=/192.168.1.110:8080 remote=/192.168.1.110:52685]], Status in: [OPEN_READ], State out: [OPEN]
2022-04-01 00:39:49.800 DEBUG 6384 --- [nio-8080-exec-2] org.apache.tomcat.util.net.NioEndpoint : Registered read interest for [org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper#2efc81a4:org.apache.tomcat.util.net.NioChannel#4fd730ea:java.nio.channels.SocketChannel[connected local=/192.168.1.110:8080 remote=/192.168.1.110:52685]]
After implementing WebSecurityConfigurerAdapter class, I've faced this issue. But I tried to get current user id from request header and adding this user id in to SecurityContextHolder to access current user's id. Where am I wrong? do you have any idea?
I solved the issue. That is my mistake. I forgot to return ResponseEntity as the response. After returning ResponseEntity, the problem is solved...
I am running my application which is based java spring boot framework. basically i have more then one microservices and I want them to communicate using http rest .
but getting this error. Can you help me to resolve this error??
2022-01-10 10:31:52.834 INFO 13740 --- [ main] com.accounts.account.AccountApplication : Started AccountApplication in 53.129 seconds (JVM running for 57.978)
2022-01-10 10:31:53.259 DEBUG 13740 --- [ main] o.s.b.a.ApplicationAvailabilityBean : Application availability state LivenessState changed to CORRECT
2022-01-10 10:31:53.274 DEBUG 13740 --- [ main] o.s.b.a.ApplicationAvailabilityBean : Application availability state ReadinessState changed to ACCEPTING_TRAFFIC
2022-01-10 10:32:42.838 INFO 13740 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-01-10 10:32:42.838 INFO 13740 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2022-01-10 10:32:42.839 DEBUG 13740 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Detected StandardServletMultipartResolver
2022-01-10 10:32:42.840 DEBUG 13740 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Detected AcceptHeaderLocaleResolver
2022-01-10 10:32:42.841 DEBUG 13740 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Detected FixedThemeResolver
2022-01-10 10:32:42.848 DEBUG 13740 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Detected org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator#72a61e61
2022-01-10 10:32:42.850 DEBUG 13740 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Detected org.springframework.web.servlet.support.SessionFlashMapManager#742dbac8
2022-01-10 10:32:42.851 DEBUG 13740 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : enableLoggingRequestDetails='false': request parameters and headers will be masked to prevent unsafe logging of potentially sensitive data
2022-01-10 10:32:42.852 INFO 13740 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 13 ms
2022-01-10 10:32:42.942 DEBUG 13740 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : GET "/", parameters={}
2022-01-10 10:32:43.030 DEBUG 13740 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
2022-01-10 10:32:43.046 DEBUG 13740 --- [nio-8080-exec-1] o.j.s.OpenEntityManagerInViewInterceptor : Opening JPA EntityManager in OpenEntityManagerInViewInterceptor
2022-01-10 10:32:43.069 DEBUG 13740 --- [nio-8080-exec-1] o.s.w.s.r.ResourceHttpRequestHandler : Resource not found
2022-01-10 10:32:43.071 DEBUG 13740 --- [nio-8080-exec-1] o.j.s.OpenEntityManagerInViewInterceptor : Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
2022-01-10 10:32:43.072 DEBUG 13740 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed 404 NOT_FOUND
2022-01-10 10:32:43.144 DEBUG 13740 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for GET "/error", parameters={}
2022-01-10 10:32:43.229 DEBUG 13740 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
2022-01-10 10:32:43.231 DEBUG 13740 --- [nio-8080-exec-1] o.j.s.OpenEntityManagerInViewInterceptor : Opening JPA EntityManager in OpenEntityManagerInViewInterceptor
2022-01-10 10:32:43.750 DEBUG 13740 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, text/html;q=0.8]
2022-01-10 10:32:43.790 DEBUG 13740 --- [nio-8080-exec-1] o.j.s.OpenEntityManagerInViewInterceptor : Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
2022-01-10 10:32:43.791 DEBUG 13740 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 404
From the code, it seems you are trying to retrieve an account based on the customer id. In that case, it will be #GetMapping and not #PostMapping ( at line 12). Also , in case of get, we pass the variables in the url and not in the request body.
As a coding practice, the className should start with a capital letter ( the return type of your method should be Account and not accounts )
#GetMapping("/myAccount/{id}
public Account AccountsGetAccountDetails(#PathVariable String id ) {}
When I hit the login form after entering credentials, it shows
White label Error Page.
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Mar 05 20:30:41 NPT 2020
There was an unexpected error (type=Not Found, status=404).
No message available
After entering URL /login it becomes http://localhost:8080/#%7B/login%7D automatically.
Here is my controller code:
#RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(#RequestParam("email") String email, #RequestParam("password") String password, ModelMap modelMap) {
User user = userRepository.findByEmail(email);
if (user.getPassword().equals(password)) {
return "findFlight";
} else {
modelMap.addAttribute("msg", "nvalid username or password .please tryagain");
}
return "login/login";
}
here is login.html file
<form action="#{/login}" method="post" >
<div class="imgcontainer">
<img th:src="#{/imgs/img_avatar2.png}" alt="Avatar" class="avatar">
</div>
<div class="container">
<label for="email"><b>email</b></label>
<input type="text" placeholder="Enter Username" name="email" required>
<label for="psw"><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="password" required>
<button type="submit">Login</button>
<label>
<input type="checkbox" checked="checked" name="remember"> Remember me
</label>
</div>
<div class="container" style="background-color:#f1f1f1">
<button type="button" class="cancelbtn">Cancel</button>
<span class="psw">Forgot password?</span>
</div>
</form>
my console is
2020-03-05 20:58:13.147 INFO 5304 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-03-05 20:58:13.147 INFO 5304 --- [ main] c.r.f.FlightreservationApplication : Started FlightreservationApplication in 2.841 seconds (JVM running for 3.45)
2020-03-05 20:58:17.359 INFO 5304 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-03-05 20:58:17.359 INFO 5304 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-03-05 20:58:17.359 DEBUG 5304 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Detected StandardServletMultipartResolver
2020-03-05 20:58:17.363 DEBUG 5304 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : enableLoggingRequestDetails='false': request parameters and headers will be masked to prevent unsafe logging of potentially sensitive data
2020-03-05 20:58:17.363 INFO 5304 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 4 ms
2020-03-05 20:58:17.368 DEBUG 5304 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : POST "/#%7B/login%7D", parameters={masked}
2020-03-05 20:58:17.373 DEBUG 5304 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler ["classpath:/templates/", "classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "/"]
2020-03-05 20:58:17.378 DEBUG 5304 --- [nio-8080-exec-1] o.s.w.s.r.ResourceHttpRequestHandler : Resource not found
2020-03-05 20:58:17.379 DEBUG 5304 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed 404 NOT_FOUND
2020-03-05 20:58:17.382 DEBUG 5304 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for POST "/error", parameters={masked}
2020-03-05 20:58:17.383 DEBUG 5304 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
2020-03-05 20:58:17.398 DEBUG 5304 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, text/html;q=0.8]
2020-03-05 20:58:17.400 DEBUG 5304 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 404
Helow
Change the line:
<form action="#{/login}" method="post" >
to:
<form th:action="#{/login}" method="post" >
Best regards
I am triying to understand why my SpringBoot webApp doesn't render in the browser. This is what my debug log says:
2017-08-04 14:54:24.760 DEBUG 23863 --- [tp1027569178-15] o.s.w.servlet.view.InternalResourceView : Forwarding to resource [/views/tictactoe.html] in InternalResourceView 'tictactoe'
2017-08-04 14:54:24.762 DEBUG 23863 --- [tp1027569178-15] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/views/tictactoe.html]
2017-08-04 14:54:24.766 DEBUG 23863 --- [tp1027569178-15] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /views/tictactoe.html
2017-08-04 14:54:24.768 DEBUG 23863 --- [tp1027569178-15] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/views/tictactoe.html]
2017-08-04 14:54:24.768 DEBUG 23863 --- [tp1027569178-15] o.s.w.s.handler.SimpleUrlHandlerMapping : Matching patterns for request [/views/tictactoe.html] are [/**]
2017-08-04 14:54:24.770 DEBUG 23863 --- [tp1027569178-15] o.s.w.s.handler.SimpleUrlHandlerMapping : URI Template variables for request [/views/tictactoe.html] are {}
2017-08-04 14:54:24.771 DEBUG 23863 --- [tp1027569178-15] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapping [/views/tictactoe.html] to HandlerExecutionChain with handler [org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#47b52f9] and 1 interceptor
2017-08-04 14:54:24.771 DEBUG 23863 --- [tp1027569178-15] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/views/tictactoe.html] is: -1
2017-08-04 14:54:24.772 DEBUG 23863 --- [tp1027569178-15] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/error]
2017-08-04 14:54:24.777 DEBUG 23863 --- [tp1027569178-15] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /error
2017-08-04 14:54:24.778 DEBUG 23863 --- [tp1027569178-15] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)]
2017-08-04 14:54:24.778 DEBUG 23863 --- [tp1027569178-15] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/error] is: -1
2017-08-04 14:54:24.784 DEBUG 23863 --- [tp1027569178-15] o.s.web.servlet.DispatcherServlet : Rendering view [org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$SpelView#41ee3720] in DispatcherServlet with name 'dispatcherServlet'
2017-08-04 14:54:24.795 DEBUG 23863 --- [tp1027569178-15] o.s.web.servlet.DispatcherServlet : Successfully completed request
2017-08-04 14:54:24.802 DEBUG 23863 --- [tp1027569178-15] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2017-08-04 14:54:24.802 DEBUG 23863 --- [tp1027569178-15] o.s.web.servlet.DispatcherServlet : Successfully completed request
2017-08-04 14:54:24.804 DEBUG 23863 --- [tp1027569178-15] o.s.web.servlet.DispatcherServlet : Successfully completed request
I am not sure if my directory structure its correct. This is how it looks like:
enter code here
Maybe something wrong in my web page/angular?
<!DOCTYPE html>
<html ng-app="myApp" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>
<title>TicTacToe</title>
<link type="text/css" href="css/design.css" rel="stylesheet" />
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.js"></script>
<script src="js/tictactoe.js"></script>
</head>
<body ng-controller="displayPageController">
<div id="player-1">PLAYER 1<br/>
{{gameBoardDto.player1}}<span style="display:inline-block"/>
</div>
<div id="game">
<div id="top-left" ng-click='playPage("topleft")'>{{gameBoardDto.topleft}}<span style="display:inline-block"/></div>
<div id="top" ng-click='playPage("top")'>{{gameBoardDto.top}}<span style="display:inline-block"/></div>
<div id="top-right" ng-click='playPage("topright")'>{{gameBoardDto.topright}}<span style="display:inline-block"/></div>
<div id="left" ng-click='playPage("left")'>{{gameBoardDto.left}}<span style="display:inline-block"/></div>
<div id="middle" ng-click='playPage("middle")'>{{gameBoardDto.middle}}<span style="display:inline-block"/></div>
<div id="right" ng-click='playPage("right")'>{{gameBoardDto.right}}<span style="display:inline-block"/></div>
<div id="bottom-left" ng-click='playPage("bottomleft")'>{{gameBoardDto.bottomleft}}<span style="display:inline-block"/></div>
<div id="bottom" ng-click='playPage("bottom")'>{{gameBoardDto.bottom}}<span style="display:inline-block"/></div>
<div id="bottom-right" ng-click='playPage("bottomright")'>{{gameBoardDto.bottomright}}<span style="display:inline-block"/></div>
</div>
<div id="player-2">PLAYER 2<br/>
{{gameBoardDto.player2}}<span style="display:inline-block"/>
</div>
</body>
</html>
This is my Spring MVC conf:
package application;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
#Configuration
#EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter {
//This allows html pages to be resolved
#Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/views/");
resolver.setSuffix(".html");
return resolver;
}
/*
This allows resources such as .js .css etc to be resolved
Here an example of how the views are linked to the resources
<link rel="stylesheet" type="text/css" href="resources/css/design.css"/>
*/
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
#Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
Any idea how can I fix this issue? My browser just renders an error when I try to access:
update
I did as Andy Wilkinson suggested but didn't really work but I did some modifications and now I have the page being served but the javascript not working.
This is what I did:
- I deleted the #EnableWebMvc from MvcConfiguration
- Removed addResourceHandlers and configureDefaultServletHandling from MvcConfiguration
- Removed ViewResolver #Bean method from MvcConfiguration
- I added a dependency to compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf'
- I created a directory called templates and I moved the .html files there
Now the browser shows me this:
[![enter code here][3]][3]
This is what the log outputs:
017-08-06 13:58:54.397 DEBUG 17581 --- [qtp758944736-18] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/error]
2017-08-06 13:58:54.402 DEBUG 17581 --- [qtp758944736-19] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/error] is: -1
2017-08-06 13:58:54.402 DEBUG 17581 --- [qtp758944736-18] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /error
2017-08-06 13:58:54.403 DEBUG 17581 --- [qtp758944736-18] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)]
2017-08-06 13:58:54.403 DEBUG 17581 --- [qtp758944736-18] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/error] is: -1
2017-08-06 13:58:54.508 DEBUG 17581 --- [qtp758944736-18] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Written [{timestamp=Sun Aug 06 13:58:54 BST 2017, status=404, error=Not Found, message=Not Found, path=/resources/css/design.css}] as "application/json;charset=UTF-8" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter#15adc6be]
2017-08-06 13:58:54.508 DEBUG 17581 --- [qtp758944736-18] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2017-08-06 13:58:54.508 DEBUG 17581 --- [qtp758944736-18] o.s.web.servlet.DispatcherServlet : Successfully completed request
2017-08-06 13:58:54.509 DEBUG 17581 --- [qtp758944736-18] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2017-08-06 13:58:54.509 DEBUG 17581 --- [qtp758944736-18] o.s.web.servlet.DispatcherServlet : Successfully completed request
2017-08-06 13:58:54.510 DEBUG 17581 --- [qtp758944736-19] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Written [{timestamp=Sun Aug 06 13:58:54 BST 2017, status=404, error=Not Found, message=Not Found, path=/resources/javascript/tictactoe.js}] as "application/json;charset=UTF-8" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter#15adc6be]
2017-08-06 13:58:54.511 DEBUG 17581 --- [qtp758944736-19] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2017-08-06 13:58:54.511 DEBUG 17581 --- [qtp758944736-19] o.s.web.servlet.DispatcherServlet : Successfully completed request
2017-08-06 13:58:54.511 DEBUG 17581 --- [qtp758944736-19] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2017-08-06 13:58:54.511 DEBUG 17581 --- [qtp758944736-19] o.s.web.servlet.DispatcherServlet : Successfully completed request
2017-08-06 13:58:54.700 DEBUG 17581 --- [qtp758944736-21] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/resources/javascript/tictactoe.js]
2017-08-06 13:58:54.701 DEBUG 17581 --- [qtp758944736-21] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /resources/javascript/tictactoe.js
2017-08-06 13:58:54.701 DEBUG 17581 --- [qtp758944736-21] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/resources/javascript/tictactoe.js]
2017-08-06 13:58:54.701 DEBUG 17581 --- [qtp758944736-21] o.s.w.s.handler.SimpleUrlHandlerMapping : Matching patterns for request [/resources/javascript/tictactoe.js] are [/**]
2017-08-06 13:58:54.701 DEBUG 17581 --- [qtp758944736-21] o.s.w.s.handler.SimpleUrlHandlerMapping : URI Template variables for request [/resources/javascript/tictactoe.js] are {}
2017-08-06 13:58:54.701 DEBUG 17581 --- [qtp758944736-21] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapping [/resources/javascript/tictactoe.js] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver#453002f1]]] and 1 interceptor
2017-08-06 13:58:54.701 DEBUG 17581 --- [qtp758944736-21] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/resources/javascript/tictactoe.js] is: -1
2017-08-06 13:58:54.702 DEBUG 17581 --- [qtp758944736-21] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/error]
2017-08-06 13:58:54.703 DEBUG 17581 --- [qtp758944736-21] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /error
2017-08-06 13:58:54.704 DEBUG 17581 --- [qtp758944736-21] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)]
2017-08-06 13:58:54.704 DEBUG 17581 --- [qtp758944736-21] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/error] is: -1
2017-08-06 13:58:54.711 DEBUG 17581 --- [qtp758944736-21] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Written [{timestamp=Sun Aug 06 13:58:54 BST 2017, status=404, error=Not Found, message=Not Found, path=/resources/javascript/tictactoe.js}] as "application/json;charset=UTF-8" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter#15adc6be]
2017-08-06 13:58:54.711 DEBUG 17581 --- [qtp758944736-21] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2017-08-06 13:58:54.711 DEBUG 17581 --- [qtp758944736-21] o.s.web.servlet.DispatcherServlet : Successfully completed request
2017-08-06 13:58:54.711 DEBUG 17581 --- [qtp758944736-21] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2017-08-06 13:58:54.711 DEBUG 17581 --- [qtp758944736-21] o.s.web.servlet.DispatcherServlet : Successfully completed request
This is the project if somebody wants to have a look:
https://github.com/SFRJ/tictactoe(Note: The version in github works when running with gradlew but not running from the .jar)
#EnableWebMvc switches off Spring Boot's auto-configuration of Spring MVC. That includes configuring it to serve static resources from classpath:static/ (which is where you resources in src/main/resources/static will end up. You also have a few other bits of configuration that Spring Boot will configure for you.
This should get you up an running:
Remove #EnableWebMvc from MvcConfiguration
Remove your overrides of addResourceHandlers and configureDefaultServletHandling from MvcConfiguration
Remove your ViewResolver #Bean method from MvcConfiguration
Assuming that its contents are the HTML in your question, move tictactoe.html into src/main/resources/static
Try moving the static folder to the project root, where it will be a sibling of src. Tomcat will then serve the contents using the default Servlet.
I'm using Spring Boot (v1.3.5.RELEASE). And in the docs, there's this short section regarding error-handling:
Spring Boot provides an /error mapping by default that handles all errors in a sensible way, and it is registered as a ‘global’ error page in the servlet container. For machine clients it will produce a JSON response with details of the error, the HTTP status and the exception message. For browser clients there is a ‘whitelabel’ error view that renders the same data in HTML format (to customize it just add a View that resolves to ‘error’).
So, to set my own custom page, I'd just need a view that resolves to error. I've added Freemarker to my classpath:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
And sure enough, making a file named error.ftl in src/main/resources/templates did the job of getting me my own custom page.
However, instead of having error.ftl in the resources/templates folder, I'd like to place it inside WEB-INF/templates.
So, I moved the file (now in WEB-INF/templates/error.ftl) and made the following spring configuration-related beans:
#Bean(name = "freeMarkerViewResolver")
public FreeMarkerViewResolver getFreeMarkerViewResolver() {
FreeMarkerViewResolver freeMarkerViewResolver = new FreeMarkerViewResolver();
freeMarkerViewResolver.setSuffix(".ftl");
return freeMarkerViewResolver;
}
#Bean(name = "freemarkerConfigurer")
public FreeMarkerConfigurer getFreeMarkerConfigurer() {
FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
freeMarkerConfigurer.setTemplateLoaderPaths(
"classpath:/templates/",
"/WEB-INF/templates/");
return freeMarkerConfigurer;
}
But now, I only get the whitelabel page instead of /WEB-INF/templates/error.ftl (when, let's say for example, I access an invalid path such as localhost:8080/invalid). Is there anything I missed?
I'm pretty sure BasicErrorController handles the job:
#RequestMapping(produces = "text/html")
public ModelAndView errorHtml(HttpServletRequest request,
HttpServletResponse response) {
response.setStatus(getStatus(request).value());
Map<String, Object> model = getErrorAttributes(request,
isIncludeStackTrace(request, MediaType.TEXT_HTML));
return new ModelAndView("error", model);
}
And here are my logs:
2016-09-12 08:46:00.241 TRACE 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Bound request context to thread: org.apache.catalina.connector.RequestFacade#11e1fd30
2016-09-12 08:46:00.241 DEBUG 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/invalid]
2016-09-12 08:46:00.242 TRACE 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Testing handler map [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#75739a6e] in DispatcherServlet with name 'dispatcherServlet'
2016-09-12 08:46:00.242 TRACE 5888 --- [nio-8080-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping : No handler mapping found for [/invalid]
2016-09-12 08:46:00.242 TRACE 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Testing handler map [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#67eec602] in DispatcherServlet with name 'dispatcherServlet'
2016-09-12 08:46:00.242 DEBUG 5888 --- [nio-8080-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /invalid
2016-09-12 08:46:00.243 DEBUG 5888 --- [nio-8080-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/invalid]
2016-09-12 08:46:00.244 TRACE 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Testing handler map [org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping#605d8da4] in DispatcherServlet with name 'dispatcherServlet'
2016-09-12 08:46:00.244 TRACE 5888 --- [nio-8080-exec-4] o.s.w.s.h.BeanNameUrlHandlerMapping : No handler mapping found for [/invalid]
2016-09-12 08:46:00.244 TRACE 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Testing handler map [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#3fa90efa] in DispatcherServlet with name 'dispatcherServlet'
2016-09-12 08:46:00.244 DEBUG 5888 --- [nio-8080-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping : Matching patterns for request [/invalid] are [/**]
2016-09-12 08:46:00.244 DEBUG 5888 --- [nio-8080-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping : URI Template variables for request [/invalid] are {}
2016-09-12 08:46:00.244 DEBUG 5888 --- [nio-8080-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapping [/invalid] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver#16545fd3]]] and 1 interceptor
2016-09-12 08:46:00.244 TRACE 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Testing handler adapter [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#5ef1fb8a]
2016-09-12 08:46:00.244 TRACE 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Testing handler adapter [org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter#781974d0]
2016-09-12 08:46:00.244 DEBUG 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/invalid] is: -1
2016-09-12 08:46:00.244 TRACE 5888 --- [nio-8080-exec-4] o.s.w.s.r.ResourceHttpRequestHandler : Applying "invalid path" checks to path: invalid
2016-09-12 08:46:00.244 TRACE 5888 --- [nio-8080-exec-4] o.s.w.s.resource.PathResourceResolver : Resolving resource for request path "invalid"
2016-09-12 08:46:00.244 TRACE 5888 --- [nio-8080-exec-4] o.s.w.s.resource.PathResourceResolver : Checking location: ServletContext resource [/]
2016-09-12 08:46:00.244 TRACE 5888 --- [nio-8080-exec-4] o.s.w.s.resource.PathResourceResolver : No match for location: ServletContext resource [/]
2016-09-12 08:46:00.244 TRACE 5888 --- [nio-8080-exec-4] o.s.w.s.resource.PathResourceResolver : Checking location: class path resource [META-INF/resources/]
2016-09-12 08:46:00.245 TRACE 5888 --- [nio-8080-exec-4] o.s.w.s.resource.PathResourceResolver : No match for location: class path resource [META-INF/resources/]
2016-09-12 08:46:00.245 TRACE 5888 --- [nio-8080-exec-4] o.s.w.s.resource.PathResourceResolver : Checking location: class path resource [resources/]
2016-09-12 08:46:00.245 TRACE 5888 --- [nio-8080-exec-4] o.s.w.s.resource.PathResourceResolver : No match for location: class path resource [resources/]
2016-09-12 08:46:00.245 TRACE 5888 --- [nio-8080-exec-4] o.s.w.s.resource.PathResourceResolver : Checking location: class path resource [static/]
2016-09-12 08:46:00.245 TRACE 5888 --- [nio-8080-exec-4] o.s.w.s.resource.PathResourceResolver : No match for location: class path resource [static/]
2016-09-12 08:46:00.245 TRACE 5888 --- [nio-8080-exec-4] o.s.w.s.resource.PathResourceResolver : Checking location: class path resource [public/]
2016-09-12 08:46:00.245 TRACE 5888 --- [nio-8080-exec-4] o.s.w.s.resource.PathResourceResolver : No match for location: class path resource [public/]
2016-09-12 08:46:00.245 TRACE 5888 --- [nio-8080-exec-4] o.s.w.s.r.ResourceHttpRequestHandler : No matching resource found - returning 404
2016-09-12 08:46:00.245 DEBUG 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2016-09-12 08:46:00.246 TRACE 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade#11e1fd30
2016-09-12 08:46:00.246 DEBUG 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Successfully completed request
2016-09-12 08:46:00.246 TRACE 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Bound request context to thread: org.apache.catalina.core.ApplicationHttpRequest#292356a6
2016-09-12 08:46:00.246 DEBUG 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/error]
2016-09-12 08:46:00.246 TRACE 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Testing handler map [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#75739a6e] in DispatcherServlet with name 'dispatcherServlet'
2016-09-12 08:46:00.246 TRACE 5888 --- [nio-8080-exec-4] o.s.w.s.handler.SimpleUrlHandlerMapping : No handler mapping found for [/error]
2016-09-12 08:46:00.246 TRACE 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Testing handler map [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#67eec602] in DispatcherServlet with name 'dispatcherServlet'
2016-09-12 08:46:00.246 DEBUG 5888 --- [nio-8080-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /error
2016-09-12 08:46:00.247 TRACE 5888 --- [nio-8080-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Found 2 matching mapping(s) for [/error] : [{[/error],produces=[text/html]}, {[/error]}]
2016-09-12 08:46:00.248 DEBUG 5888 --- [nio-8080-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)]
2016-09-12 08:46:00.248 TRACE 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Testing handler adapter [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#5ef1fb8a]
2016-09-12 08:46:00.248 DEBUG 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/error] is: -1
2016-09-12 08:46:00.248 TRACE 5888 --- [nio-8080-exec-4] .w.s.m.m.a.ServletInvocableHandlerMethod : Invoking [BasicErrorController.errorHtml] method with arguments [org.apache.catalina.core.ApplicationHttpRequest#292356a6, org.apache.catalina.connector.ResponseFacade#4fa94295]
2016-09-12 08:46:00.248 TRACE 5888 --- [nio-8080-exec-4] .w.s.m.m.a.ServletInvocableHandlerMethod : Method [errorHtml] returned [ModelAndView: reference to view with name 'error'; model is {timestamp=Mon Sep 12 08:46:00 SGT 2016, status=404, error=Not Found, message=No message available, path=/invalid}]
2016-09-12 08:46:00.249 DEBUG 5888 --- [nio-8080-exec-4] o.s.w.s.v.ContentNegotiatingViewResolver : Requested media types are [text/html, text/html;q=0.8] based on Accept header types and producible media types [text/html])
2016-09-12 08:46:00.249 DEBUG 5888 --- [nio-8080-exec-4] o.s.w.s.v.ContentNegotiatingViewResolver : Returning [org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$SpelView#58edc30] based on requested media type 'text/html'
2016-09-12 08:46:00.249 DEBUG 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Rendering view [org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$SpelView#58edc30] in DispatcherServlet with name 'dispatcherServlet'
2016-09-12 08:46:00.249 TRACE 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Cleared thread-bound request context: org.apache.catalina.core.ApplicationHttpRequest#292356a6
2016-09-12 08:46:00.249 DEBUG 5888 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet : Successfully completed request
Instead of redefining the freemarker beans (which may be the cause of your error), you could use spring-boot's property spring.freemarker.template-loader-path, as described here:
If you use FreeMarker you will also have a FreeMarkerViewResolver with
id ‘freeMarkerViewResolver’. It looks for resources in a loader path
(externalized to spring.freemarker.templateLoaderPath, default
‘classpath:/templates/’) by surrounding the view name with a prefix
and suffix (externalized to spring.freemarker.prefix and
spring.freemarker.suffix, with empty and ‘.ftl’ defaults
respectively). It can be overridden by providing a bean of the same
name.
The default is this:
spring.freemarker.template-loader-path=classpath:/templates/ # Comma-separated list of template paths.
You could add this to your application.properties:
spring.freemarker.template-loader-path=classpath:/WEB-INF/templates/