Defining #Annotations shows full package path instead of the #Annotation - spring

how can i possibly fix this i don't want to see the full package path.. help
I have not found info regarding this...

At the top, add this:
import org.springframework.web.bind.annotation.RestController;
#RestController
public class RestController

It's because your class is named "RestController" same as the interface you are trying to implement by #RestController annotation

Related

How to use #JsonTest when there is no #SpringBootApplication?

I want to use #JsonTest to write tests on JSON serialization for my library. However, when I add the annotation to a test, I get:
Unable to find a #SpringBootConfiguration, you need to use #ContextConfiguration or #SpringBootTest(classes=...) with your test
As this is a library, I indeed don't have a main class annotated with #SpringBootConfiguration. How can I avoid that I need to introduce this just to get the testing framework going?
Seems the only way is to actually add a dummy application. In my case, I added this into src/test/java to avoid it will end up in the production sources. So something like this:
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Just created this here to make #JsonTest work
*/
#SpringBootApplication
public class DummyApplication {
}
#SpringBootConfiguration
#EnableAutoConfiguration
#ComponentScan
public class SpringTestContext {
}
Put this in your library module, located in root test dir, This will supply a ApplicationContext for your tests.

Odd Spring Boot #Autowired error on a repository NOT due to wrong project structure or missing annotations

I'm asking this question with a risk of having it marked as duplicate, but I honestly don't think it is, so here it goes.
I'm having trouble running a basic Spring Boot application. The error I'm getting is this:
Parameter 0 of constructor in com.example.demo.service.EventService required a bean of type 'com.example.demo.dao.EventRepository' that could not be found.
Now this seems like a pretty common error (and question on SO) that should point the developer to check the following issues:
Most commonly the project structure is wrong (or let's say 'custom') and the bean is not getting scanned because of hierarchy [Questions: this, this, this, this and probably many more]. Solution should be to place the SpringBootApplication in the top package and all the components in its sub-packages so it could scan everything properly or to custom-define the packages that need to be scanned.
The bean was not defined, i.e. there's a missing annotation of type #Service, #Repository etc. so the bean is not being created.
Another reason might be using two different ways of defining beans (as posted here). I'm using purely annotation-style defining, so I think I should be good here.
Also, this question has an answer mentioning to exclude the autoconfiguration of JPA repositories from application.properties file, but I don't have this configuration set.
There were also a couple of questions/answers mentioning issues with dependencies and pom.xml file which fixed this problem, but my pom.xml is a pretty basic file created from Spring Initializr, so again, I don't think this is the solution.
The error message also says:
Consider defining a bean of type 'com.example.demo.dao.EventRepository' in your configuration.
The 'missing bean' is this repository:
package com.example.demo.dao;
import com.example.demo.entity.Event;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
#Repository
public interface EventRepository extends JpaRepository<Event, Integer> {
}
You can see it has #Repository annotation (although I've created repositories without this annotation before and it worked fine so I don't think this is required, but I added it now just in case this was the issue), it extends JpaRepository so it should be a valid repository and it's inside com.example.demo.dao package.
The class that's autowiring this is a service:
package com.example.demo.service;
import com.example.demo.dao.EventRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
#Service
public class EventService {
EventRepository eventRepository;
#Autowired
public EventService(EventRepository eventRepository) {
this.eventRepository = eventRepository;
}
}
I'll also provide the main application so you can see its package is com.example.demo which is a parent package for both the service and the repository:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
I've also tried rebuilding the project and the infamous "closing and reopening" of IntelliJ, because it was known for sometimes acting stupid in my experience and this would solve it, but not this time.
Also as a side note, I've successfully created these kinds of projects before, so I'm really not sure what's the issue right now.
Am I missing something obvious here? What else can I check?
EDIT:
Here's the entity class as well (generated by an IDE tool):
package com.example.demo.entity;
import javax.persistence.*;
import java.sql.Timestamp;
import java.util.Objects;
#Entity
public class Event {
private int id;
private String name;
private Timestamp dateFrom;
private Timestamp dateTo;
// getters & setters abstracted
}
Turns out it was an issue with pom.xml after all.
I've added the JPA dependency later and accidentally added the wrong one. My pom.xml had
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
instead of
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Apparently the IDE tool used for generating entities from tables was missing javax.persistence so it added that jar manually through lib folder. Everything looked fine to IntelliJ, but something was messed between dependencies.
Anyway, I changed the dependency in pom.xml and removed the extra jar added. Now everything works fine.

Spring Boot doc is out of date

https://spring.io/guides/gs/spring-boot/
This is out of date as far as I can tell.
The github repo didn't exist so I used the initilizr mentioned beforehand and imported that into intellij.
then I tried creating a new java class like the first part of the tutorial says in https://spring.io/guides/gs/spring-boot/#_create_a_simple_web_application
but straight out the bat, the imports in the provided code fail :
package hello;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
#RestController
public class HelloController {
#RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}
I googled around a bit and apparently (but I'm probably misunderstanding.) RestController is deprecated in favor of Controller?
what about RequestMapping then?
how do I set up a basic test rest service.
PS: in the initilizr (https://start.spring.io/), I chose Spring Boot 2.0.0 M6
in your pom.xml file, change : the very first <artifactId> line to :
<artifactId>spring-boot</artifactId>
it might have been something different (for me it was <artifactId>spring-boot-starter-parent</artifactId>).
with this you imports should be available.

where to place componentscan

I have spring mvc application with complete annotations. Where is the best place to add #componentScan? Let me know any of these recommended
class that extends AbstractAnnotationConfigDispatcherServletInitializer?
class that extend WebMvcConfigurationSupport
class that extend WebSecurityConfigurerAdapter
I placed in 2 without security and workign fine. When I added security, I got problem with security config not able to find userdetails service. Then I moved to 3.
I found other issues with security and put code to just reuturn null instead of
securityconfig object from getRootConfigClasses(). Then I got issues of controllers not found. I am able to fix it to put componenentscan in 2.
I just want to know any links and how it works. Is it ok to put #componentscan in all of these 3? Appreciate your help.
It depends on your project's package tree and what you want to scan. If you want to scan all annoted classes with such annotations like : #Configuration, #Component, #Repository, etc... put #ComponentScan at the top of your package tree.
You can also use the basePackages attribute to specify where to start the scanning.
Say you have an application packages organized like this :
com.app.config,com.app.config.web, com.app.services, com.app.web.controllers
If you want to scan all annoted classes, put the class annoted with #ComponentScan in com.app package.
If you want to scan only controller, add #ComponentScan(basePackages="com.app.web.controllers")
It's up to you to decide.

Is there a way to use Spring #Profile annotation at a package level?

I'm trying to put all my bean definitions for a specific profiles together, and would rather not push them all into one giant AppConfig.java class. I was wondering if there was a way to annotate at a package level using package-info.java and have all configuration files within that package inherit the profile.
I've tried the following in package-info.java:
#Profile("test")
package com.system.configuration.test;
import org.springframework.context.annotation.Profile;
But the #Configuration classes within the package seem to be used whether it is the "test" profile or not.
Is the only choice to annotate each class individually?
You can do it in different way by creating separate #Configuration classes for different profiles:
#Configuration
#Profile("test")
#ComponentScan("com.system.configuration.test")
public class TestProfile {
}
And then on your main configuration class you need to do imports:
#Configuration
#Import(TestProfile.class)
public class MainConfiguration {
}

Resources