분영 아래와 같이 정상적으로 잘 연결한것 같지만 null 발생한다.
java Controller -> Service -> ServiceImpl -> DAO -> DAO.xml
ERROR org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException: null
at com.example.demo.web.UserController.postLogin(UserController.java:28) ~[main/:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_201]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_201]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_201]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_201]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-5.3.12.jar:5.3.12]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) ~[spring-web-5.3.12.jar:5.3.12]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-5.3.12.jar:5.3.12]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.3.12.jar:5.3.12]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.12.jar:5.3.12]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.12.jar:5.3.12]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067) ~[spring-webmvc-5.3.12.jar:5.3.12]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963) ~[spring-webmvc-5.3.12.jar:5.3.12]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.12.jar:5.3.12]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.3.12.jar:5.3.12]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:681) ~[tomcat-embed-core-9.0.54.jar:4.0.FR]
해결
위 에러는 연결 부분을 확인을 해봐야 한다.
- Controller 에서 Service -> Service 사용하기 위해서는 @Autowired 사용.
- Service 에서 DAO
- DAO -> DAO.xml
@Autowired
private UserService userService;
UserController.java
package com.example.demo.web;
import com.example.demo.common.Common;
import com.example.demo.domain.User;
import com.example.demo.service.UserService;
import groovy.util.logging.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@RestController
public class UserController extends Common {
@Autowired
private UserService userService;
@PostMapping(value = "/login")
public Map<String, Object> postLogin(@RequestBody User user, HttpSession session) {
User signUser = userService.signIn(user.getUsername(), user.getPassword());
log.info(signUser.getUsername());
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("result", "ok");
resultMap.put("authInfo", signUser );
//session.setAttribute(">>>username", user.getUsername() );
return resultMap;
}
}
'프로그래밍 > Java' 카테고리의 다른 글
spring boot cron 같은 스케쥴러 사용하기 (0) | 2022.01.11 |
---|---|
spring boot application.properties 운영/개발 설정하기 (0) | 2022.01.10 |
Log4j2 취약점 해결 : 보안 업데이트 (0) | 2021.12.13 |
스프링 부트 + vue.js gradle build 과정에서 npm build 자동으로 수행되도록 (0) | 2021.11.23 |
intellij thymeleaf 재시작 없이 실시간 반영 (0) | 2021.11.18 |