0
votes

I am new to Spring Boot and somewhat new to Java / Maven as well, so I am pretty sure I have some basic misconfiguration here in terms of using a DAL project and a Web Service project, but it is difficult for me to figure out.

I can attach .POM files or add more code if my explanation here is insufficient - I am hoping perhaps there is something obviously wrong that a Spring Boot advanced user could help out with. Even suggestions for turning on more logging so I could see why Spring Boot thinks it has to log to stderr in this case would be great.

My basic set up is - I have 2 Spring Boot projects:

  1. DAL project. This uses Postgres / JPA to set up my database and allows a routing component of our project to internally use the DAL. This sets up all of the repositories for the DAL as not being externally exposed - for instance:

    @RepositoryRestResource(collectionResourceRel = "user", path = "user", exported=false) public interface UserRepository extends PagingAndSortingRepository<User, Long> { }

  2. A Web Service project that will take care of any business logic and expose end points to our end users. This logic is not needed by our routing component. This also exposes REST endpoints for instance like:

    @RestController @RequestMapping(value = "/auditentries") @Api(value = "Audit Entries", description = "Audit Entries API") public class AuditEntryController extends BaseController<AuditEntry, AuditEntryService> { }

When I run the Web Service as a Spring Boot app, the endpoints are exposed correctly and I can use a REST client to get to /auditentries or other external endpoints that I have configured. The DAL is also basically working as it sets up tables using Hibernate and the internal PagingAndSortingRepository classes used by the Web Service. So - this is great - the main use case for the project setup seems to work without issue.

However, I have noticed 2 problems so far - and perhaps I am missing something but I can't even figure out how to debug them or to tell why Spring Boot is getting confused in these cases:

  1. If I configure logging for the Web Service project, the spring boot logging from the Web Service project is immediately directed to stderr in red text, while the logging from the DAL project is appropriately directed to the log file. Manual logging statements I have inserted using logger.info in the Web Service also correctly go to the log file.

By default Spring Boot logs to the console, so logging such as that at the end of this post always goes to the console and stderr even though a log file is configured.

  1. If I attempt to enable security in the Web Service project, Spring Boot is enabling security on all the endpoints, but it does not recognize my custom class which is supposed to define my security:

    @Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter{ }

I believe both of these issues are related to the inclusion of the DAL project in the POM file from the Web Service project, because if I simplify the project back down to essentially a spring boot project that no longer relies on DAL (or does anything else interesting anymore), my security configuration class is called, and the logging appears to work as expected.

One potential problem currently is both POM files for DAL and Web Service include most of the dependencies explicitly for things like spring-boot-starter-data-rest, postgres, jpa, etc. Perhaps the Web Service should just be relying on the DAL's POM dependency inclusions entirely, but I don't see offhand why this would be causing errors even if it is not best practice to include dependencies in both projects explicitly.

Is there an invalid assumption I am making or some spring configuration I should be turning off in the DAL in order to have both projects work together? Does Spring Boot potentially not like that both projects have REST endpoints but only 1 set of endpoints is exposed? How do I even debug this issue without resorting to trial and error / brute force?

Here is some sample logging from the Web Service project that is always going to the console even though logging is configured:

[main] INFO CNER.WS.App - Starting App on mschwartz-w550s with PID 5072 (C:\dev\event_router\server\WebServices\WS\target\classes started by mschwartz in C:\dev\event_router\server\WebServices\WS) [main] INFO CNER.WS.App - The following profiles are active: default [main] INFO org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext - Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@10feca44: startup date [Fri Mar 25 05:24:02 UTC 2016]; root of context hierarchy [main] INFO org.springframework.beans.factory.support.DefaultListableBeanFactory - Overriding bean definition for bean 'beanNameViewResolver' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]] [main] INFO org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration' of type [class org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration$$EnhancerBySpringCGLIB$$8c5fa87b] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) [main] INFO org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [class org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$82f8180d] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) [main] INFO org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer - Tomcat initialized with port(s): 8187 (http) [main] INFO org.apache.catalina.core.StandardService - Starting service Tomcat

Here is some example logging from the DAL project that is correctly going to the log file - you can see it is related to DAL-specific implementation and Hibernate.

INFO 2016-03-25 00:24:00,985 [main] CNER.WS.App:main(54): Starting Server ... INFO 2016-03-25 05:24:01,955 [background-preinit] org.hibernate.validator.internal.util.Version:(17): HV000001: Hibernate Validator 5.2.2.Final INFO 2016-03-25 05:24:06,669 [localhost-startStop-1] org.hibernate.jpa.internal.util.LogHelper:logPersistenceUnitInformation(46): HHH000204: Processing PersistenceUnitInfo [ name: default ...]

2
Welcome to SO. Please read the formatting help when editing a question. Proper, and readable, formatting helps us help you, along with helping those in the future searching for a similar solution. Also, lots and lots of text tends to slow, rather than help speed answers. Be concise; Imagine a colleague asking you your question. At what point would your eyes glaze over and what would you need to know to help them? Edit your question accordingly. "How to Ask", including the final link, and "minimal reproducible example" are your friends. - the Tin Man
Thanks - to be more concise and clear, I have created a test project here:github.com/schwartzma1/TestSpringBootSecurity It seems like logging will work with these .pom files, however, the Web Security Config is not recognized. Instead spring boot prints something like "2016-03-25 15:44:53.533 INFO 15324 --- [ost-startStop-1] b.a.s.AuthenticationManagerConfiguration : Using default security password: 7caa3d9c-2918-4718-855f-b38cd9f36bd6 Why is the WebSecurityConfig class being ignored in this case? - Michael Schwartz
So - this was resolved by .pom file changes for the logging (check github for example that works) as well as adding "wstest" to the ComponentScan to pick up the WebSecurityConfig class. - Michael Schwartz
Please edit your question, and add the additional into it where you'd have put it if it was there originally. Comments are best left for suggestions and requests for clarification. Putting your information in the question itself makes it possible for us to read one thing and know everything you've said, instead of having to read every comment and piece it together. If someone supplied the answer that solved the problem then select their answer; You don't need to add a comment about to your question; A comment to the selected answer would be more usable. - the Tin Man
If you found the solution on your own then create a new answer, provide the information and any relevant code. SO will let you select it after a timeout period. - the Tin Man

2 Answers

0
votes

Your data access layer sounds like a JAR you'd like to publish to Maven and subsequently let your web layer pull down using its pom.

It should be obvious that the dependencies between the two must match.

I'd wonder why you don't write Spring Boot REST data services and combine the two. Is that what you're doing?

0
votes

Resolved with .pom file changes for logging, and adding following to ComponentScan to pick up the implementation of WebSecurityConfigurerAdapter

@ComponentScan(basePackages= {"repo", "wstest.controllers", "wstest.services", "wstest"})