Spring boot controllers not mapped for url - spring-boot

I have spring controllers in my app, when executing my app the all controllers end URLs are showing not fond(404) error in browser.
public class EmployeeContoller {
private EmployeeService employeeService; #Autowired.
public EmployeeContoller (EmployeeService employeeService)
{this.employeeService=employeeService. #RequestMapping("/employee/{id}")
public Employee getEmployee(#PathVariable String id){ return employeeService.getEmployee(I'd);
}

Related

Spring boot OAuth2 client with Thymeleaf

I am practicing spring boot OAuth2. I have and authorization server, resource server, and client server. I managed to secure an endpoint (/products).
I want show a list of products using Thymeleaf.
Here is the controller in the resource server:
#RestController
public class ProductController {
#Autowired
ProductRepository productRepository;
#GetMapping("/products")
public List<Product> getProducts(final Model model) {
return (List<Product>) productRepository.findAll();
}
}
Here is the controller in the client server:
#RestController
public class ArticlesController {
#Autowired
private WebClient webClient;
#GetMapping(value = "/articles")
public String[] getArticles(
#RegisteredOAuth2AuthorizedClient("articles-client-authorization-code") OAuth2AuthorizedClient authorizedClient) {
return this.webClient.get().uri("http://localhost:8090/articles")
.attributes(oauth2AuthorizedClient(authorizedClient)).retrieve().bodyToMono(String[].class).block();
}
#GetMapping(value = "/products")
public List<Product> getProducts(
#RegisteredOAuth2AuthorizedClient("articles-client-authorization-code") OAuth2AuthorizedClient authorizedClient) {
return this.webClient.get().uri("http://localhost:8090/products")
.attributes(oauth2AuthorizedClient(authorizedClient)).accept(MediaType.ALL).retrieve()
.bodyToFlux(Product.class).collectList().block();
}
}
With the code above I have a list of JSON Products. What is the best way to show this list as a table with Thymeleaf using the webClient?

Spring Boot Application ,JPA,

I have created a small application using the spring boot framework. I have created a Rest Controler class.
and deploy it on tomcat, but I am getting 404 error i.e
The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
#RestController
#RequestMapping("students")
public class StudentController {
#Autowired
StudentRepository repository;
#GetMapping
public List<Student> getAllStudents() {
return (List<Student>) repository.findAll();
}
#PostMapping
public String createStudent() {
return "created";
}
#PutMapping
public String updateStudent() {
return "updated";
}
#DeleteMapping
public String deleteStudent() {
return "deleted";
}
}
You are missing slash in annotation, it should look like this
#RestController
#RequestMapping("/students")
public class StudentController {
...
}

how to use session scoped bean in jhipster microservice architecture

My session scoped bean:
#Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
#Component
public class AuthNodes {
private String authNodes;
public String getAuthNodes() {
return authNodes;
}
public void setAuthNodes(String authNodes) {
this.authNodes = authNodes;
}
}
is injected in a REST controller of a JHipster generated microservice:
#RestController
#RequestMapping("/api")
public class NodeResource {
#Autowired
private AuthNodes authNodes;
...
#GetMapping("/nodes-and-children/{user:.+}")
#Timed
public ResponseEntity<List<Node>> getFilteredNodesAndChildren(#PathVariable String user,
#ApiParam Pageable pageable) {
...
String hosts = authNodes.getAuthNodes();
if (hosts == null) {
authNodes.setAuthNodes("my user's authorized node names");
}
...
but at each call the previously set value is lost and authNodes.getAuthNodes() returns null.
What is wrong?
Thanks, Mic

Null pointer exception is being shown when i use medicore class to use repository

When i call a service layer from controller,data are being saved in database.
My controller is:
#RestController
#RequestMapping("/api")
public class ApiController {
#Autowired(required = true)
PersonService personService; //This is service layer class
#RequestMapping("/add")
public void add(){
person.setLastName("Rahim");
person.setFirstName("Uddin");
person.setAddress("Dhaka");
person.setCity("Dhaka");
personService.add(person);
}
}
Service layer is:
#Service
#Transactional
public class PersonServiceImpl implements PersonService {
#Autowired
PersonDao personDao;
#Override
public void addPerson(Person person) {
personDao.addPerson(person);
}
}
Till now everything is ok.
But when i call the service layer through another class, null pointer exception is being shown.
At that time:
My controller is:
#RestController
#RequestMapping("/api")
public class ApiController {
#Autowired(required = true)
PersonService personService; //This is service layer class
#RequestMapping("/add")
public void add(){
MiddleClass m=new MiddleClass();
m.create();
}
}
My MiddleClass is:
public class MiddleClass {
#Autowired
PersonService personService;
public void create(){
Person person=new Person();
person.setLastName("Rahim");
person.setFirstName("Uddin");
person.setAddress("Dhaka");
person.setCity("Dhaka");
personService.addPerson(person);//this time same service layer
//is showing null pointer exception here
}
}
WHY?????????? is it for lacking of any annotation in MiddleClass?
You're creating the MiddleClass instance yourself, using new. So Spring has no way to know that it has to inject the service into the MiddleClass instance. It can only inject beans into other beans it creates itself.
So, MiddleClass must be a Spring bean, and it must be autowired into the controller, just like the service is.

PlayFramework: Depedencies are not inject using Spring and got NullPointerException

When i try to integrate Spring-Dependency-Injection in Play-framework with Java 8. In controller the dependencies are not injected. I am using spring stereo-type annotations. Get
Follwowing is my code:
Configuration:
public class GlobalConfiguration extends GlobalSettings{
private AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
#Override
public void onStart(Application app) {
super.onStart(app);
// AnnotationConfigApplicationContext can only be refreshed once, but we do it here even though this method
// can be called multiple times. The reason for doing during startup is so that the Play configuration is
// entirely available to this application context.
applicationContext.scan("com.harmeetsingh13.controllers", "com.harmeetsingh13.service.impl", "com.harmeetsingh13.dao.impl");
applicationContext.refresh();
// This will construct the beans and call any construction lifecycle methods e.g. #PostConstruct
applicationContext.start();
}
#Override
public void onStop(Application app) {
// This will call any destruction lifecycle methods and then release the beans e.g. #PreDestroy
applicationContext.close();
super.onStop(app);
}
#Override
public <A> A getControllerInstance(Class<A> clazz) throws Exception {
return applicationContext.getBean(clazz);
}
}
Controller:
#Component
public class UserController extends Controller{
#Autowired
private UserService userService;
public Result findUserById(Integer userId) {
Optional<User> user = userService.findUserById(userId);
if(user.isPresent()){
}
return null;
}
}
Service:
#Service
public class UserServiceImpl implements UserService {
#Autowired
private UserDao userDao;
#Override
public Optional<User> findUserById(int id) {
List<User> users = userDao.getAllUsers();
return users.stream().filter(user -> user.id == id).findFirst();
}
}
This is the link where i found sample application
This is really my stupid mistake. In play-framework we always need to put the custom global configuration file in project app folder at root and play-framework always find to search Global file name at app folder root and load into the memory. In my case, my GlobalConfiguration file are not loaded in the memory and default configuration are used by play-framework. For Global-Settings click on this link for more information

Resources