public BlogResponseDTO findBlogByPageId(String pageId) {
BlogEntity blogEntity = blogRepository.findByPageId(pageId);
if (blogEntity == null) {
throw new NoSuchElementException("Blog not found for pageId: " + pageId);
}
return buildBlogResponseDTO(blogEntity);
}
My application doesn't throw runtime errors from even though I try throwing it explicitly. Can someone help me with what am I missing? If you need any more details, please drop a comment.
My springboot application (for blog) is using JWT method of authentication. When user tries to find a blog by the [code]pageId[/code] which doesn't exist in the repository, I am trying to throw a [code]NoSuchElementException[/code] In BlogService: [code] public BlogResponseDTO findBlogByPageId(String pageId) { BlogEntity blogEntity = blogRepository.findByPageId(pageId); if (blogEntity == null) { throw new NoSuchElementException("Blog not found for pageId: " + pageId); } return buildBlogResponseDTO(blogEntity); } [/code] But it throws [code]401 Unauthorized[/code] error as a response. [b]WebSecurityConfig[/b]: [code] //Imports import jakarta.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; import org.springframework.security.config.http.SessionCreationPolicy; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.web.SecurityFilterChain; import org.springframework.security.web.authentication.Http403ForbiddenEntryPoint; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; import org.springframework.web.multipart.support.MultipartFilter;
[/code] My application doesn't throw runtime errors from even though I try throwing it explicitly. Can someone help me with what am I missing? If you need any more details, please drop a comment.