My computer restarted and my app no longer works today. It was working yesterday. Nothing changed except Maven downloading new libraries (mostly Amazon). Now I can't even access the /admin/login page. How do I debug the request?
2019-09-13 17:26:16.924 INFO 2056 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/admin/login],methods=[GET]}" onto public java.lang.String com.s.s.controller.Controller.login()
2019-09-13 17:26:21.465 ERROR 2056 --- [nio-8080-exec-1] c.s.s.c.Controller : invalidRequest(): invalid request: /admin/login
#RequestMapping(value="/admin/login", method=RequestMethod.GET)
public String login() {
return "login";
}
#RequestMapping("/error")
public void invalidRequest(HttpServletRequest request, HttpServletResponse response){
Logger.error(TAG, "invalidRequest()", "invalid request: " + request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI));
try {
response.setStatus(HttpStatus.OK.value());
response.getWriter().write("This is not a valid request");
} catch (IOException e) {
Logger.error(TAG, "invalidRequest: ", "Exception in invalidRequest() method "+e.getMessage());
}
}
#Override
public String getErrorPath() {
return "/error";
}
I set this line in application.properties but it didn't print anything extra.
logging.level.com.s.s.controller.Controller=DEBUG
I ran mvn clean and restarted server, but it didn't help.
I set this line in application.properties but it didn't print anything useful.
debug=true
Log:
2019-09-13 17:40:25.459 DEBUG 2056 --- [nio-8080-exec-1] o.s.b.w.f.OrderedRequestContextFilter : Bound request context to thread: org.apache.catalina.connector.RequestFacade#4d3e8c3a
2019-09-13 17:40:25.460 DEBUG 2056 --- [nio-8080-exec-1] o.s.b.w.f.OrderedRequestContextFilter : Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade#4d3e8c3a
2019-09-13 17:40:25.463 ERROR 2056 --- [nio-8080-exec-1] c.s.s.c.Controller : invalidRequest(): invalid request: /admin/login
2019-09-13 17:40:25.477 DEBUG 2056 --- [nio-8080-exec-2] o.s.b.w.f.OrderedRequestContextFilter : Bound request context to thread: org.apache.catalina.connector.RequestFacade#4d3e8c3a
2019-09-13 17:40:25.484 DEBUG 2056 --- [nio-8080-exec-2] o.s.b.w.f.OrderedRequestContextFilter : Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade#4d3e8c3a
$ git status
modified: src/main/resources/application.properties
modified: src/main/resources/static/js/home.js
I compared libraries to what was last deployed and the only changes were
aws-java-sdk-core-1.11.631.jar
aws-java-sdk-kms-1.11.631.jar
aws-java-sdk-s3-1.11.631.jar
aws-java-sdk-ses-1.11.631.jar
jmespath-java-1.11.631.jar
I commented public String getErrorPath() and public void invalidRequest() and now it gives me
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Sep 13 18:59:03 UTC 2019
There was an unexpected error (type=Forbidden, status=403).
No message available
But with curl it gives
$ curl localhost:8080/admin/login
{"timestamp":1568401131164,"status":403,"error":"Forbidden","message":"No message available","path":"/admin/login"}
I found the following searching for spring-sleuth in Makgigrahul's answer and added it to application.properties producing this log.
logging.level.org.springframework.web.servlet.DispatcherServlet=DEBUG
Log:
2019-09-13 16:10:11.329 DEBUG 15356 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing servlet 'dispatcherServlet'
2019-09-13 16:10:11.330 INFO 15356 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2019-09-13 16:10:11.331 INFO 15356 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2019-09-13 16:10:11.332 DEBUG 15356 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Using MultipartResolver [org.springframework.web.multipart.support.StandardServletMultipartResolver#6481dcd8]
2019-09-13 16:10:11.332 DEBUG 15356 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Using LocaleResolver [org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver#50cc90f2]
2019-09-13 16:10:11.339 DEBUG 15356 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Unable to locate ThemeResolver with name 'themeResolver': using default [org.springframework.web.servlet.theme.FixedThemeResolver#135fd762]
2019-09-13 16:10:11.345 DEBUG 15356 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Unable to locate RequestToViewNameTranslator with name 'viewNameTranslator': using default [org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator#61cc4a6b]
2019-09-13 16:10:11.352 DEBUG 15356 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Unable to locate FlashMapManager with name 'flashMapManager': using default [org.springframework.web.servlet.support.SessionFlashMapManager#766c1f26]
2019-09-13 16:10:11.353 DEBUG 15356 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Published WebApplicationContext of servlet 'dispatcherServlet' as ServletContext attribute with name [org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcherServlet]
2019-09-13 16:10:11.355 INFO 15356 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 23 ms
2019-09-13 16:10:11.356 DEBUG 15356 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Servlet 'dispatcherServlet' configured successfully
2019-09-13 16:10:11.376 DEBUG 15356 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/error]
2019-09-13 16:10:11.383 DEBUG 15356 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/error] is: -1
2019-09-13 16:10:11.408 DEBUG 15356 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Rendering view [org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$SpelView#53191939] in DispatcherServlet with name 'dispatcherServlet'
2019-09-13 16:10:11.415 DEBUG 15356 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Successfully completed request
You can try to set a breakpoint in org.apache.catalina.core.ApplicationFilterChain#doFilter. Thats pretty at the beginning of the request processing. Or you can have a look in org.springframework.web.servlet.DispatcherServlet#doDispatch
I was able to debug using Andreas' answer and saw the values for request.request.filterChain.filters[] and saw my filter remoteAddressFilter() and saw remoteAddress = 0:0:0:0:0:0:0:1. I have an IP filter (RemoteAddrFilter) and one of the allowed values is 127.0.0.1. I was accessing the server with http://localhost:8080/admin/home, which apparently uses the IP6 address. Probably a Windows update which restarted the computer changed the networking behavior. When I use http://127.0.0.1:8080/admin/home it works. I would like to add logging to the filter to notify when blocked, but there is no apparent way. Amazing Spring doesn't log the filters in debug mode.
I'm trying to write a simple SpringBoot Web Application to get started with the framework.
This is my structure:
src
`-main
`-java
`-resources
`-static
`-templates
`-webapp
`-WEB-INF
`-jsp
`-login.jsp
I have a Controller like
#Controller
public class LoginController {
#RequestMapping("/login")
public String loginMessage(){
return "login";
}
}
I've configurated the path where views has to be looked for in the application.properties like:
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
logging.level.org.springframework.web=DEBUG
I've also included the next dependency in pom.xml:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
But when I go to http://localhost:8080/login I'm still having a 404 error.
What am I missing?
EDIT:
Here is the log from the IntelliJ Idea console. It seems like it finds the login.jsp, but for some reason return an error page.
2018-07-06 11:17:20.488 DEBUG 53340 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/login]
2018-07-06 11:17:20.488 DEBUG 53340 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /login
2018-07-06 11:17:20.488 DEBUG 53340 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public java.lang.String com.albertocastano.apringboot.web.springbootfirstwebapplication.controller.LoginController.loginMessage()]
2018-07-06 11:17:20.488 DEBUG 53340 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/login] is: -1
2018-07-06 11:17:20.489 DEBUG 53340 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Requested media types are [text/html, application/xhtml+xml, image/webp, image/apng, application/xml;q=0.9, */*;q=0.8] based on Accept header types and producible media types [*/*])
2018-07-06 11:17:20.489 DEBUG 53340 --- [nio-8080-exec-1] o.s.w.servlet.view.BeanNameViewResolver : No matching bean found for view name 'login'
2018-07-06 11:17:20.489 DEBUG 53340 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Returning [org.springframework.web.servlet.view.InternalResourceView: name 'login'; URL [/WEB-INF/jsp/login.jsp]] based on requested media type 'text/html'
2018-07-06 11:17:20.489 DEBUG 53340 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Rendering view [org.springframework.web.servlet.view.InternalResourceView: name 'login'; URL [/WEB-INF/jsp/login.jsp]] in DispatcherServlet with name 'dispatcherServlet'
2018-07-06 11:17:20.492 DEBUG 53340 --- [nio-8080-exec-1] o.s.w.servlet.view.InternalResourceView : Forwarding to resource [/WEB-INF/jsp/login.jsp] in InternalResourceView 'login'
2018-07-06 11:17:20.492 DEBUG 53340 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/WEB-INF/jsp/login.jsp]
2018-07-06 11:17:20.492 DEBUG 53340 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /WEB-INF/jsp/login.jsp
2018-07-06 11:17:20.493 DEBUG 53340 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/WEB-INF/jsp/login.jsp]
2018-07-06 11:17:20.495 DEBUG 53340 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping : Matching patterns for request [/WEB-INF/jsp/login.jsp] are [/**]
2018-07-06 11:17:20.496 DEBUG 53340 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping : URI Template variables for request [/WEB-INF/jsp/login.jsp] are {}
2018-07-06 11:17:20.497 DEBUG 53340 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapping [/WEB-INF/jsp/login.jsp] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/], ServletContext resource [/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver#3f0408]]] and 1 interceptor
2018-07-06 11:17:20.498 DEBUG 53340 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/WEB-INF/jsp/login.jsp] is: -1
2018-07-06 11:17:20.499 DEBUG 53340 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2018-07-06 11:17:20.499 DEBUG 53340 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Successfully completed request
2018-07-06 11:17:20.500 DEBUG 53340 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Successfully completed request
2018-07-06 11:17:20.501 DEBUG 53340 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing GET request for [/error]
2018-07-06 11:17:20.502 DEBUG 53340 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /error
2018-07-06 11:17:20.502 DEBUG 53340 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)]
2018-07-06 11:17:20.502 DEBUG 53340 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Last-Modified value for [/error] is: -1
2018-07-06 11:17:20.522 DEBUG 53340 --- [nio-8080-exec-1] 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])
2018-07-06 11:17:20.522 DEBUG 53340 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Returning [org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$SpelView#bd7588] based on requested media type 'text/html'
2018-07-06 11:17:20.522 DEBUG 53340 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Rendering view [org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$SpelView#bd7588] in DispatcherServlet with name 'dispatcherServlet'
2018-07-06 11:17:20.522 DEBUG 53340 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Successfully completed request
You're missing dependancy in your pom.xml file.
Add jstl :
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
And remove the scope provided of tomcat-embed-jasper, because it's not provided by default by any spring-boot-starter.
I just can not. For 4 days I have been struggling to send data ajax using a different method than GET and I can not advise. I create a project in Spring Boot. I have created a mapped address controller
#PutMapping(value = "/changeEmail", consumes = MediaType.APPLICATION_JSON_VALUE)
public boolean changeEmail(
#RequestBody ChangeEmailDTO changeEmailDTO
) {
System.out.println("email: " + changeEmailDTO.getEmail());
return true;
}
This controller is supposed to accept the email address sent by ajaxa
function changeEmail() {
console.log("Event");
$.ajax({
type: 'PUT',
url: '/changeEmail',
data: {
email: JSON.strgify($('#email').val())
},
success: function (result) {
console.log('function');
}
});
}
However, the only effect is that in the console crashes me
PUT http://localhost:8080/signIn net::ERR_TOO_MANY_REDIRECTS
send # jquery-3.2.1.min.js:4
ajax # jquery-3.2.1.min.js:4
changeEmail # settings.js:58
submitHandler # settings.js:52
d # jquery.validate.min.js:4
(anonymous) # jquery.validate.min.js:4
dispatch # jquery-3.2.1.min.js:3
q.handle # jquery-3.2.1.min.js:3
The DTO has only one field
public class ChangeEmailDTO {
#IsValidEmail
#ExistsEmail(ifExistsReturn = false)
#Getter #Setter private String email;
}
After trying to send data via ajax, it sends me to the error controller
#Controller
public class PageNotFoundController implements ErrorController{
#RequestMapping("/error")
public ModelAndView showPageError() {
return new ModelAndView("redirect:/signIn");
}
#Override
public String getErrorPath() {
return "/error";
}
}
Screen with browser tool what to send and receive: https://zapodaj.net/f1b8ed0b2a16b.png.html
As I mentioned, the controller can correctly pick up a GET query from ajax only. Every time it flips on the error controller.
Additionally, they say that if I wanted to send data directly from the form, then there is no problem with any query.
What this ajax divert, I do not know.
In addition, when the csrf() is disabled in Spring Security receives such logs
2017-07-26 21:07:15.465 DEBUG 3760 --- [nio-8080-exec-6] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2017-07-26 21:07:15.465 DEBUG 3760 --- [nio-8080-exec-6] o.s.web.servlet.DispatcherServlet : Successfully completed request
2017-07-26 21:07:15.499 DEBUG 3760 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing PUT request for [/changeEmail]
2017-07-26 21:07:15.501 DEBUG 3760 --- [nio-8080-exec-7] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /changeEmail
2017-07-26 21:07:15.505 DEBUG 3760 --- [nio-8080-exec-7] .m.m.a.ExceptionHandlerExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
2017-07-26 21:07:15.506 DEBUG 3760 --- [nio-8080-exec-7] .w.s.m.a.ResponseStatusExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
2017-07-26 21:07:15.506 DEBUG 3760 --- [nio-8080-exec-7] .w.s.m.s.DefaultHandlerExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
2017-07-26 21:07:15.508 DEBUG 3760 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2017-07-26 21:07:15.508 DEBUG 3760 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet : Successfully completed request
2017-07-26 21:07:15.509 DEBUG 3760 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing PUT request for [/error]
2017-07-26 21:07:15.510 DEBUG 3760 --- [nio-8080-exec-7] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /error
2017-07-26 21:07:15.511 DEBUG 3760 --- [nio-8080-exec-7] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public org.springframework.web.servlet.ModelAndView com.movie.database.app.controller.error.PageNotFoundController.showPageError()]
2017-07-26 21:07:15.526 DEBUG 3760 --- [nio-8080-exec-7] o.s.web.cors.DefaultCorsProcessor : Skip CORS processing: request is from same origin
here
2017-07-26 21:07:15.527 DEBUG 3760 --- [nio-8080-exec-7] o.s.w.s.v.ContentNegotiatingViewResolver : Requested media types are [*/*] based on Accept header types and producible media types [*/*])
2017-07-26 21:07:15.528 DEBUG 3760 --- [nio-8080-exec-7] o.s.w.servlet.view.BeanNameViewResolver : No matching bean found for view name 'redirect:/signIn'
2017-07-26 21:07:15.528 DEBUG 3760 --- [nio-8080-exec-7] o.s.w.s.v.ContentNegotiatingViewResolver : Returning redirect view [org.springframework.web.servlet.view.RedirectView: unnamed; URL [/signIn]]
2017-07-26 21:07:15.528 DEBUG 3760 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet : Rendering view [org.springframework.web.servlet.view.RedirectView: unnamed; URL [/signIn]] in DispatcherServlet with name 'dispatcherServlet'
2017-07-26 21:07:15.530 DEBUG 3760 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet : Successfully completed request
2017-07-26 21:07:15.534 DEBUG 3760 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing PUT request for [/signIn]
2017-07-26 21:07:15.535 DEBUG 3760 --- [nio-8080-exec-9] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /signIn
2017-07-26 21:07:15.536 DEBUG 3760 --- [nio-8080-exec-9] .m.m.a.ExceptionHandlerExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported
2017-07-26 21:07:15.536 DEBUG 3760 --- [nio-8080-exec-9] .w.s.m.a.ResponseStatusExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported
2017-07-26 21:07:15.536 DEBUG 3760 --- [nio-8080-exec-9] .w.s.m.s.DefaultHandlerExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported
2017-07-26 21:07:15.536 WARN 3760 --- [nio-8080-exec-9] o.s.web.servlet.PageNotFound : Request method 'PUT' not supported
2017-07-26 21:07:15.536 DEBUG 3760 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2017-07-26 21:07:15.536 DEBUG 3760 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet : Successfully completed request
2017-07-26 21:07:15.537 DEBUG 3760 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing PUT request for [/error]
2017-07-26 21:07:15.538 DEBUG 3760 --- [nio-8080-exec-9] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /error
2017-07-26 21:07:15.539 DEBUG 3760 --- [nio-8080-exec-9] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public org.springframework.web.servlet.ModelAndView com.movie.database.app.controller.error.PageNotFoundController.showPageError()]
2017-07-26 21:07:15.539 DEBUG 3760 --- [nio-8080-exec-9] o.s.web.cors.DefaultCorsProcessor : Skip CORS processing: request is from same origin
The most important mistakes are
2017-07-26 21:07:15.506 DEBUG 3760 --- [nio-8080-exec-7] .w.s.m.s.DefaultHandlerExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
2017-07-26 21:07:15.536 DEBUG 3760 --- [nio-8080-exec-9] .m.m.a.ExceptionHandlerExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported
2017-07-26 21:07:15.536 WARN 3760 --- [nio-8080-exec-9] o.s.web.servlet.PageNotFound : Request method 'PUT' not supported
Your error message tells you your issue: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
Add contentType: 'application/json; charset=utf-8', to your ajax call. You'll also want to call JSON.stringify on the entire JSON string. Check out the w3schools page on stringify for a good example, also copied/pasted below.
var obj = { "name":"John", "age":30, "city":"New York"};
var myJSON = JSON.stringify(obj);
I have a test like this:
#Test
#Transactional
public void when_EnteredRegisteredProductionOrder_and_RegisteredStaffSignature_then_201_and_ProductionOrderRegistered() {
//language=JSON
String startProductionOrder = "{\n" +
" \"productionOrder\": \"1700281\",\n" +
" \"staffCodeSignature\": \"00000425\"\n" +
"}";
given()
.body(startProductionOrder)
.accept(ContentType.JSON)
.contentType(ContentType.JSON)
.when()
.post(commandPath)
.then().statusCode(201);
ProductionOrder po = productionOrderRepo.findOneByCode("1700281");
assertThat(po.getCode(), is("1700281"));
}
My intention is to test the REST API from start to end using restassured. As you can imagine each command, starting from the controller is annotated with #Transactional. The problem is that test does not rollback automatically. The debug log shows than (I removed many parts because it was too long to post):
DEBUG 14120 --- [ main] tractDirtiesContextTestExecutionListener : Before test method: context [DefaultTestContext#1dd02175 testClass = ProductionOrderStartLT, testInstance = com.demo.logbook.commandValidationLT.ProductionOrderStartLT#25359ed8, testMethod = when_EnteredRegisteredProductionOrder_and_RegisteredStaffSignature_then_201_and_ProductionOrderRegistered#ProductionOrderStartLT, testExcep
DEBUG 14120 --- [ main] t.a.AnnotationTransactionAttributeSource : Adding transactional method 'com.demo.logbook.commandValidationLT.ProductionOrderStartLT.when_EnteredRegisteredProductionOrder_and_RegisteredStaffSignature_then_201_and_ProductionOrderRegistered' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
DEBUG 14120 --- [ main] t.c.t.TransactionalTestExecutionListener : Explicit transaction definition [PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''] found for test context [DefaultTestContext#1dd02175 testClass = ProductionOrderStartLT, testInstance = com.demo.logbook.commandValidationLT.ProductionOrderStartLT#25359ed8, testMethod = when_EnteredRegisteredProductionOrder_and_RegisteredStaffSignature_
DEBUG 14120 --- [ main] t.c.t.TransactionalTestExecutionListener : Retrieved #TransactionConfiguration [null] for test class [com.demo.logbook.commandValidationLT.ProductionOrderStartLT].
DEBUG 14120 --- [ main] t.c.t.TransactionalTestExecutionListener : Using TransactionConfigurationAttributes [TransactionConfigurationAttributes#4a5fab78 transactionManagerName = '', defaultRollback = true] for test class [com.demo.logbook.commandValidationLT.ProductionOrderStartLT].
DEBUG 14120 --- [ main] c.DefaultCacheAwareContextLoaderDelegate : Retrieved ApplicationContext from cache with key [[WebMergedContextConfiguration#31206beb testClass = ProductionOrderStartLT, locations = '{}', classes = '{class com.demo.logbook.ElogbookApplication}', contextInitializerClasses = '[]', activeProfiles = '{test}', propertySourceLocations = '{}', propertySourceProperties = '{org.sp
DEBUG 14120 --- [ main] org.springframework.test.context.cache : Spring test ApplicationContext cache statistics: [DefaultContextCache#c677d7e size = 1, maxSize = 32, parentContextCount = 0, hitCount = 1, missCount = 1]
DEBUG 14120 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'transactionManager'
DEBUG 14120 --- [ main] t.c.t.TransactionalTestExecutionListener : No method-level #Rollback override: using default rollback [true] for test context [DefaultTestContext#1dd02175 testClass = ProductionOrderStartLT, testInstance = com.demo.logbook.commandValidationLT.ProductionOrderStartLT#25359ed8, testMethod = when_EnteredRegisteredProductionOrder_and_RegisteredStaffSignature_then_201_and_Prod
DEBUG 14120 --- [ main] o.s.orm.jpa.JpaTransactionManager : Creating new transaction with name [com.demo.logbook.commandValidationLT.ProductionOrderStartLT.when_EnteredRegisteredProductionOrder_and_RegisteredStaffSignature_then_201_and_ProductionOrderRegistered]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
DEBUG 14120 --- [ main] o.s.orm.jpa.JpaTransactionManager : Opened new EntityManager [org.hibernate.jpa.internal.EntityManagerImpl#505a8582] for JPA transaction
DEBUG 14120 --- [ main] o.s.orm.jpa.JpaTransactionManager : Exposing JPA transaction as JDBC transaction [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle#465d1345]
INFO 14120 --- [ main] o.s.t.c.transaction.TransactionContext : Began transaction (1) for test context [DefaultTestContext#1dd02175 testClass = ProductionOrderStartLT, testInstance = com.demo.logbook.commandValidationLT.ProductionOrderStartLT#25359ed8, testMethod = when_EnteredRegisteredProductionOrder_and_RegisteredStaffSignature_then_201_and_ProductionOrderRegistered#ProductionOrderStartLT
DEBUG 14120 --- [ main] c.DefaultCacheAwareContextLoaderDelegate : Retrieved ApplicationContext from cache with key [[WebMergedContextConfiguration#31206beb testClass = ProductionOrderStartLT, locations = '{}', classes = '{class com.demo.logbook.ElogbookApplication}', contextInitializerClasses = '[]', activeProfiles = '{test}', propertySourceLocations = '{}', propertySourceProperties = '{org.sp
DEBUG 14120 --- [ main] org.springframework.test.context.cache : Spring test ApplicationContext cache statistics: [DefaultContextCache#c677d7e size = 1, maxSize = 32, parentContextCount = 0, hitCount = 2, missCount = 1]
DEBUG 14120 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'org.springframework.boot.test.mock.mockito.MockitoBeans'
DEBUG 14120 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet : Initializing servlet 'dispatcherServlet'
INFO 14120 --- [nio-9090-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
INFO 14120 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'multipartResolver'
DEBUG 14120 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet : Using MultipartResolver [org.springframework.web.multipart.support.StandardServletMultipartResolver#63880be9]
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Creating instance of bean 'org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Finished creating instance of bean 'org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver'
DEBUG 14120 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet : Unable to locate LocaleResolver with name 'localeResolver': using default [org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver#560b484d]
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Creating instance of bean 'org.springframework.web.servlet.theme.FixedThemeResolver'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Finished creating instance of bean 'org.springframework.web.servlet.theme.FixedThemeResolver'
DEBUG 14120 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet : Unable to locate ThemeResolver with name 'themeResolver': using default [org.springframework.web.servlet.theme.FixedThemeResolver#5c57255c]
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'requestMappingHandlerMapping'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'viewControllerHandlerMapping'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'beanNameHandlerMapping'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'resourceHandlerMapping'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'defaultServletHandlerMapping'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'faviconHandlerMapping'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'welcomePageHandlerMapping'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'requestMappingHandlerAdapter'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'httpRequestHandlerAdapter'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'simpleControllerHandlerAdapter'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'errorAttributes'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'handlerExceptionResolver'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Creating instance of bean 'org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Finished creating instance of bean 'org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator'
DEBUG 14120 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet : Unable to locate RequestToViewNameTranslator with name 'viewNameTranslator': using default [org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator#56aa150f]
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'beanNameViewResolver'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'mvcViewResolver'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'defaultViewResolver'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'viewResolver'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Creating instance of bean 'org.springframework.web.servlet.support.SessionFlashMapManager'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Finished creating instance of bean 'org.springframework.web.servlet.support.SessionFlashMapManager'
DEBUG 14120 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet : Unable to locate FlashMapManager with name 'flashMapManager': using default [org.springframework.web.servlet.support.SessionFlashMapManager#2c40b216]
DEBUG 14120 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet : Published WebApplicationContext of servlet 'dispatcherServlet' as ServletContext attribute with name [org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcherServlet]
INFO 14120 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 19 ms
DEBUG 14120 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet : Servlet 'dispatcherServlet' configured successfully
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.w.f.OrderedRequestContextFilter : Bound request context to thread: org.apache.catalina.connector.RequestFacade#17b5f8f0
DEBUG 14120 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet : DispatcherServlet with name 'dispatcherServlet' processing POST request for [/commands/production/production-orders]
DEBUG 14120 --- [nio-9090-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /commands/production/production-orders
DEBUG 14120 --- [nio-9090-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public void com.demo.logbook.commands.web.controllers.ProductionController.startProductionOrder(com.demo.logbook.commands.commands.productionOrder.StartProductionOrder)]
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'productionController'
DEBUG 14120 --- [nio-9090-exec-1] o.j.s.OpenEntityManagerInViewInterceptor : Opening JPA EntityManager in OpenEntityManagerInViewInterceptor
DEBUG 14120 --- [nio-9090-exec-1] m.m.a.RequestResponseBodyMethodProcessor : Read [class com.demo.logbook.commands.commands.productionOrder.StartProductionOrder] as "application/json;charset=UTF-8" with [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter#ff21443]
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Creating instance of bean 'org.hibernate.validator.internal.constraintvalidators.bv.NotNullValidator'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Finished creating instance of bean 'org.hibernate.validator.internal.constraintvalidators.bv.NotNullValidator'
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'transactionManager'
DEBUG 14120 --- [nio-9090-exec-1] o.s.orm.jpa.JpaTransactionManager : Found thread-bound EntityManager [org.hibernate.jpa.internal.EntityManagerImpl#2aa65d86] for JPA transaction
DEBUG 14120 --- [nio-9090-exec-1] o.s.orm.jpa.JpaTransactionManager : Creating new transaction with name [com.demo.logbook.commands.web.controllers.ProductionController.startProductionOrder]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; '',-java.lang.RuntimeException
DEBUG 14120 --- [nio-9090-exec-1] o.s.orm.jpa.JpaTransactionManager : Exposing JPA transaction as JDBC transaction [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle#63fa0a64]
DEBUG 14120 --- [nio-9090-exec-1] c.d.l.c.w.c.ProductionController : Command received : StartProductionOrder{productionOrder='1700281'} SignedCommand{staffCodeSignature='00000425'}
INFO 14120 --- [nio-9090-exec-1] o.h.h.i.QueryTranslatorFactoryInitiator : HHH000397: Using ASTQueryTranslatorFactory
Hibernate: select eventtype0_.id as id1_17_, eventtype0_.name as name2_17_, eventtype0_.type as type3_17_ from events.event_type eventtype0_ where eventtype0_.name=?
Hibernate: select aggregate0_.aggregate_id as aggregat1_15_, aggregate0_.name as name2_15_ from events.aggregate aggregate0_ where aggregate0_.name=?
DEBUG 14120 --- [nio-9090-exec-1] c.d.l.event_store.services.EventStore : Flushing event : Event
DEBUG 14120 --- [nio-9090-exec-1] stomAnnotationTransactionAttributeSource : Adding transactional method 'save' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'transactionManager'
DEBUG 14120 --- [nio-9090-exec-1] o.s.orm.jpa.JpaTransactionManager : Found thread-bound EntityManager [org.hibernate.jpa.internal.EntityManagerImpl#2aa65d86] for JPA transaction
DEBUG 14120 --- [nio-9090-exec-1] o.s.orm.jpa.JpaTransactionManager : Participating in existing transaction
Hibernate: select nextval ('hibernate_sequence')
DEBUG 14120 --- [nio-9090-exec-1] c.d.l.event_store.services.EventStore : Event flushed : Event{event_id=31, correlation_id=e5901e3d-9cbf-4c53-bb8c-1fb1006328f8, data='{"transaction_id":"0c4cb269-32fc-422d-93a8-28d506911861","staffCodeSignature":"00000425","productionOrderCode":"1700281"}', version=1, eventType=com.demo.logbook.event_store.persistance.entities.EventType#34ef5c1b, aggregate=Aggregate{a
DEBUG 14120 --- [nio-9090-exec-1] c.d.l.c.w.c.ProductionController : Publishing eventData : ProductionOrderStarted
DEBUG 14120 --- [nio-9090-exec-1] c.d.l.c.w.c.ProductionController : Command success : StartProductionOrder{productionOrder='1700281'} SignedCommand{staffCodeSignature='00000425'}
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'adminProjection'
DEBUG 14120 --- [nio-9090-exec-1] c.d.l.q.a.projection.AdminProjection : Event received in admin module: ProductionOrderStarted
DEBUG 14120 --- [nio-9090-exec-1] c.d.l.q.a.s.AdminValidationService : Validating event in admin module: ProductionOrderStarted
Hibernate: select count(production0_.product_order_id) as col_0_0_ from admin.production_order production0_ where production0_.code=?
Hibernate: select count(staff0_.reason_type_id) as col_0_0_ from admin.staff staff0_ where staff0_.code=?
DEBUG 14120 --- [nio-9090-exec-1] c.d.l.q.a.s.AdminValidationService : Event validation successful in admin module: ProductionOrderStarted
DEBUG 14120 --- [nio-9090-exec-1] c.d.l.q.a.projection.AdminProjection : Event processed successfully by admin module: ProductionOrderStarted
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'productionProjection'
DEBUG 14120 --- [nio-9090-exec-1] c.d.l.q.p.p.ProductionProjection : Event received in prod module: ProductionOrderStarted
DEBUG 14120 --- [nio-9090-exec-1] c.d.l.q.p.s.ProductionValidationService : Validating event in prod module: ProductionOrderStarted
Hibernate: select production0_.transaction_id as transact1_20_, production0_.last_action_timestamp as last_act2_20_, production0_.code as code3_20_, production0_.product_code as product_4_20_, production0_.product_name as product_5_20_, production0_.work_center_code as work_cen6_20_, production0_.work_center_name as work_cen7_20_ from prod.production_order production0_ where production0_.code=?
2017-01-20 13:50:10.399 DEBUG 14120 --- [nio-9090-exec-1] c.d.l.q.p.s.ProductionValidationService : Event validation successful in prod module: ProductionOrderStarted
Hibernate: select production0_.product_order_id as product_1_5_, production0_.code as code2_5_, production0_.product_id as product_3_5_, production0_.work_center_id as work_cen4_5_ from admin.production_order production0_ where production0_.code=?
Hibernate: select product0_.product_id as product_1_3_0_, product0_.box_size as box_size2_3_0_, product0_.code as code3_3_0_, product0_.container_size as containe4_3_0_, product0_.container_type as containe5_3_0_, product0_.description as descript6_3_0_, product0_.markezini_capacity as markezin7_3_0_, product0_.partena_capacity as partena_8_3_0_, product0_.product_category_id as product_9_3_0_, productca
Hibernate: select workcenter0_.work_center_id as work_cen1_13_0_, workcenter0_.code as code2_13_0_, workcenter0_.is_active as is_activ3_13_0_, workcenter0_.name as name4_13_0_, workcenter0_.wing_id as wing_id5_13_0_, wing1_.wing_id as wing_id1_12_1_, wing1_.name as name2_12_1_ from admin.work_center workcenter0_ left outer join admin.wing wing1_ on workcenter0_.wing_id=wing1_.wing_id where workcenter0_.w
DEBUG 14120 --- [nio-9090-exec-1] c.d.l.q.p.s.ProductionEventsService : Flushing production order: ProductionOrder(super=TransactionalEntity(transaction_id=0c4cb269-32fc-422d-93a8-28d506911861, lastActionTimestamp=2017-01-20T13:50:10.425), code=1700281, workCenterCode=0032, workCenterName=SYFPAC A, productCode=0100100, productName=AMINOPHYLLINE 250MG PPAMP 10ML, reasonCodesList=null, inProcessContro
DEBUG 14120 --- [nio-9090-exec-1] stomAnnotationTransactionAttributeSource : Adding transactional method 'save' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'transactionManager'
DEBUG 14120 --- [nio-9090-exec-1] o.s.orm.jpa.JpaTransactionManager : Found thread-bound EntityManager [org.hibernate.jpa.internal.EntityManagerImpl#2aa65d86] for JPA transaction
DEBUG 14120 --- [nio-9090-exec-1] o.s.orm.jpa.JpaTransactionManager : Participating in existing transaction
Hibernate: select production0_.transaction_id as transact1_20_1_, production0_.last_action_timestamp as last_act2_20_1_, production0_.code as code3_20_1_, production0_.product_code as product_4_20_1_, production0_.product_name as product_5_20_1_, production0_.work_center_code as work_cen6_20_1_, production0_.work_center_name as work_cen7_20_1_, inprocessc1_.production_order_transaction_id as producti5_18
DEBUG 14120 --- [nio-9090-exec-1] c.d.l.q.p.s.ProductionEventsService : Flushed production order: ProductionOrder(super=TransactionalEntity(transaction_id=0c4cb269-32fc-422d-93a8-28d506911861, lastActionTimestamp=2017-01-20T13:50:10.425), code=1700281, workCenterCode=0032, workCenterName=SYFPAC A, productCode=0100100, productName=AMINOPHYLLINE 250MG PPAMP 10ML, reasonCodesList=null, inProcessControl
DEBUG 14120 --- [nio-9090-exec-1] c.d.l.q.p.p.ProductionProjection : Event processed successfully by prod module: ProductionOrderStarted
DEBUG 14120 --- [nio-9090-exec-1] o.s.orm.jpa.JpaTransactionManager : Initiating transaction commit
DEBUG 14120 --- [nio-9090-exec-1] o.s.orm.jpa.JpaTransactionManager : Committing JPA transaction on EntityManager [org.hibernate.jpa.internal.EntityManagerImpl#2aa65d86]
Hibernate: insert into events.event (aggregate_id, correlation_id, data, event_type_id, time_created, version, event_id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into prod.production_order (last_action_timestamp, code, product_code, product_name, work_center_code, work_center_name, transaction_id) values (?, ?, ?, ?, ?, ?, ?)
DEBUG 14120 --- [nio-9090-exec-1] o.s.orm.jpa.JpaTransactionManager : Not closing pre-bound JPA EntityManager after transaction
DEBUG 14120 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
DEBUG 14120 --- [nio-9090-exec-1] o.j.s.OpenEntityManagerInViewInterceptor : Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
DEBUG 14120 --- [nio-9090-exec-1] o.s.orm.jpa.EntityManagerFactoryUtils : Closing JPA EntityManager
DEBUG 14120 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet : Successfully completed request
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.w.f.OrderedRequestContextFilter : Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade#17b5f8f0
Hibernate: select production0_.transaction_id as transact1_20_, production0_.last_action_timestamp as last_act2_20_, production0_.code as code3_20_, production0_.product_code as product_4_20_, production0_.product_name as product_5_20_, production0_.work_center_code as work_cen6_20_, production0_.work_center_name as work_cen7_20_ from prod.production_order production0_ where production0_.code=?
DEBUG 14120 --- [ main] c.DefaultCacheAwareContextLoaderDelegate : Retrieved ApplicationContext from cache with key [[WebMergedContextConfiguration#31206beb testClass = ProductionOrderStartLT, locations = '{}', classes = '{class com.demo.logbook.ElogbookApplication}', contextInitializerClasses = '[]', activeProfiles = '{test}', propertySourceLocations = '{}', propertySourceProperties = '{org.sp
DEBUG 14120 --- [ main] org.springframework.test.context.cache : Spring test ApplicationContext cache statistics: [DefaultContextCache#c677d7e size = 1, maxSize = 32, parentContextCount = 0, hitCount = 3, missCount = 1]
DEBUG 14120 --- [ main] c.DefaultCacheAwareContextLoaderDelegate : Retrieved ApplicationContext from cache with key [[WebMergedContextConfiguration#31206beb testClass = ProductionOrderStartLT, locations = '{}', classes = '{class com.demo.logbook.ElogbookApplication}', contextInitializerClasses = '[]', activeProfiles = '{test}', propertySourceLocations = '{}', propertySourceProperties = '{org.sp
DEBUG 14120 --- [ main] org.springframework.test.context.cache : Spring test ApplicationContext cache statistics: [DefaultContextCache#c677d7e size = 1, maxSize = 32, parentContextCount = 0, hitCount = 4, missCount = 1]
DEBUG 14120 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'org.springframework.boot.test.mock.mockito.MockitoBeans'
DEBUG 14120 --- [ main] c.DefaultCacheAwareContextLoaderDelegate : Retrieved ApplicationContext from cache with key [[WebMergedContextConfiguration#31206beb testClass = ProductionOrderStartLT, locations = '{}', classes = '{class com.demo.logbook.ElogbookApplication}', contextInitializerClasses = '[]', activeProfiles = '{test}', propertySourceLocations = '{}', propertySourceProperties = '{org.sp
DEBUG 14120 --- [ main] org.springframework.test.context.cache : Spring test ApplicationContext cache statistics: [DefaultContextCache#c677d7e size = 1, maxSize = 32, parentContextCount = 0, hitCount = 5, missCount = 1]
DEBUG 14120 --- [ main] o.s.orm.jpa.JpaTransactionManager : Initiating transaction rollback
DEBUG 14120 --- [ main] o.s.orm.jpa.JpaTransactionManager : Rolling back JPA transaction on EntityManager [org.hibernate.jpa.internal.EntityManagerImpl#505a8582]
DEBUG 14120 --- [ main] o.s.orm.jpa.JpaTransactionManager : Closing JPA EntityManager [org.hibernate.jpa.internal.EntityManagerImpl#505a8582] after transaction
DEBUG 14120 --- [ main] o.s.orm.jpa.EntityManagerFactoryUtils : Closing JPA EntityManager
INFO 14120 --- [ main] o.s.t.c.transaction.TransactionContext : Rolled back transaction for test context [DefaultTestContext#1dd02175 testClass = ProductionOrderStartLT, testInstance = com.demo.logbook.commandValidationLT.ProductionOrderStartLT#25359ed8, testMethod = when_EnteredRegisteredProductionOrder_and_RegisteredStaffSignature_then_201_and_ProductionOrderRegistered#ProductionOrderStart
DEBUG 14120 --- [ main] tractDirtiesContextTestExecutionListener : After test method: context [DefaultTestContext#1dd02175 testClass = ProductionOrderStartLT, testInstance = com.demo.logbook.commandValidationLT.ProductionOrderStartLT#25359ed8, testMethod = when_EnteredRegisteredProductionOrder_and_RegisteredStaffSignature_then_201_and_ProductionOrderRegistered#ProductionOrderStartLT, testExcept
Which seems that a new appplication context (and tread is used). How can I accomplish the above task? Is it the only way to use #AfterTranscation and clean my database for each test?
#Transactional only works within the same thread, and as such the rollback provided by Spring will only work on the objects that were persisted on the thread where the transaction was started.
Your rest-assured test will perform an integration test and will hit your Spring context on another thread. So although your rest-assured test would be able to see the objects persisted in your test setup, Spring will never be able to cleanup resources persisted in your rest-assured test automatically.
The only way to tackle this is to delete the entities yourself in your test (Using an Junit Rule, or cleaning up any state in your test setup).
I've been banging my head against this problem, and have managed to find a workaround for it. The problem is indeed caused by the fact that the JUnit test runs in a different thread as the embedded servlet container. This means that two transactions and database connections are opened, unaware of each other. The fix is to propagate the transaction that was opened in the JUnit test to the servlet container (using a Filter):
#Transactional
public class MyTest {
private static final String[] FIELD_NAMES = {"resources", "synchronizations", "currentTransactionName", "currentTransactionReadOnly", "currentTransactionIsolationLevel", "actualTransactionActive"};
private static final Field[] FIELDS = new Field[FIELD_NAMES.length];
private static final Object[] FIELD_VALUES = new Object[FIELD_NAMES.length];
#Before
public void copyTransactionThreadLocals() {
for (int i=0; i<FIELD_NAMES.length; i++) {
Field field = ReflectionUtils.findField(TransactionSynchronizationManager.class,FIELD_NAMES[i]);
field.setAccessible(true);
FIELD_VALUES[i] = ((ThreadLocal)ReflectionUtils.getField(field,null)).get();
FIELDS[i] = field;
}
}
#Configuration
static class Config {
#Bean
public Filter transactionFilter() {
return new OncePerRequestFilter() {
#Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
for (int i=0; i<FIELD_NAMES.length; i++) {
((ThreadLocal)ReflectionUtils.getField(FIELDS[i],null)).set(FIELD_VALUES[i]);
}
filterChain.doFilter(request,response);
}
};
}
}
}
Please be aware that the above code is a hack that uses reflection to copy inaccessible static fields.
With these integration tests, Spring is reusing its test context and the old transaction can stick around and cause tests to fail in weird ways due to state from previous tests. If you see tests that pass when run by themselves fail when run together, then annotate the tests with #DirtiesContext. This lets Spring know it should discard the test context instead of reusing it.
I want to add rss source for my site in spring and the final step is configure the Bean of CustomRssView.
but tutorial used xml to configure it. I hope to use #Bean annotation. However, It's failed to resolve the view name.
xml in tutorial
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.mkyong.common.controller" />
<!-- Map returned view name "rssViewer" to bean id "rssViewer" -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
<bean id="rssViewer" class="com.mkyong.common.rss.CustomRssViewer" />
my bean configuration
#SpringBootApplication
public class Application {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
System.out.println("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
}
#Bean
public RssView rssView() {
return new RssView();
}
}
It is successful to register, but the view name is not resolved by BeanNameViewResolver.
some debug info
2015-09-08 16:02:13.103 TRACE 21367 --- [nio-8080-exec-1] .w.s.m.m.a.ServletInvocableHandlerMethod : Method [getRssFeed] returned [ModelAndView: reference to view with name 'rssView'; model is {rss=[Post(id=0, menuId=0, submenuId=0, date=null, author=admin, title=Spring MVC Tutorial 1, content=Tutorial 1 Content ...), Post(id=0, menuId=0, submenuId=0, date=null, author=system, title=Spring MVC Tutorial 2, content=Tutorial 2 Content ...)]}]
2015-09-08 16:02:13.103 TRACE 21367 --- [nio-8080-exec-1] HandlerMethodReturnValueHandlerComposite : Testing if return value handler [org.springframework.web.servlet.mvc.method.annotation.ModelAndViewMethodReturnValueHandler#46a550ec] supports [class org.springframework.web.servlet.ModelAndView]
2015-09-08 16:02:13.111 DEBUG 21367 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Requested media types are [text/html, application/xhtml+xml, image/webp, application/xml;q=0.9, */*;q=0.8] based on Accept header types and producible media types [*/*])
2015-09-08 16:02:13.111 DEBUG 21367 --- [nio-8080-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'rssView'
2015-09-08 16:02:13.113 DEBUG 21367 --- [nio-8080-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Invoking afterPropertiesSet() on bean with name 'rssView'
2015-09-08 16:02:13.113 DEBUG 21367 --- [nio-8080-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'org.springframework.
2015-09-08 16:02:13.113 DEBUG 21367 --- [nio-8080-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Invoking afterPropertiesSet() on bean with name 'rssView'
2015-09-08 16:02:13.113 DEBUG 21367 --- [nio-8080-exec-1] o.s.b.f.s.DefaultListableBeanFactory : Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
2015-09-08 16:02:13.113 TRACE 21367 --- [nio-8080-exec-1] o.s.w.s.v.InternalResourceViewResolver : Cached view [rssView]
2015-09-08 16:02:13.114 DEBUG 21367 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Returning [org.springframework.web.servlet.view.InternalResourceView: name 'rssView'; URL [rssView]] based on requested media type 'text/html'
2015-09-08 16:02:13.114 DEBUG 21367 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Rendering view [org.springframework.web.servlet.view.InternalResourceView: name 'rssView'; URL [rssView]] in DispatcherServlet with name 'dispatcherServlet'
2015-09-08 16:02:13.114 TRACE 21367 --- [nio-8080-exec-1] o.s.w.servlet.view.InternalResourceView : Rendering view with name 'rssView' with model {rss=[Post(id=0, menuId=0, submenuId=0, date=null, author=admin, title=Spring MVC Tutorial 1, content=Tutorial 1 Content ...), Post(id=0, menuId=0, submenuId=0, date=null, author=system, title=Spring MVC Tutorial 2, content=Tutorial 2 Content ...)]} and static attributes {}
2015-09-08 16:02:13.114 DEBUG 21367 --- [nio-8080-exec-1] o.s.w.servlet.view.InternalResourceView : Added model object 'rss' of type [java.util.ArrayList] to request in view with name 'rssView'
2015-09-08 16:02:13.116 DEBUG 21367 --- [nio-8080-exec-1] o.s.w.servlet.view.InternalResourceView : Forwarding to resource [rssView] in InternalResourceView 'rssView'
2015-09-08 16:02:13.125 TRACE 21367 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Bound request context to thread: org.apache.catalina.core.ApplicationHttpRequest#41e5804f
2015-09-08 16:02:13.125 DEBUG 21367 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet
how to change my code.