My own WebPages are not shown on localhost - java

I have a Spring application with Apache Wicket (Its my first Spring-Application) and its autogenerated. If I run my application and I call it on localhost there is only shown a site with "TestDataManager is running" on it instead of the Site I call in the Main. I figured out that I have in the tests- package a class named ExampleController and its not from me. In this class is witten what is shown on localhost. But in my Main I dont call this Class.
Can Somone say how to fix this.
#SpringBootApplication
#RestController
public class Application extends WebApplication {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#Override
public Class<? extends Page> getHomePage() {
return WhatToDoPage.class;
}
}
ExampleController:
#RestController
public class ExampleController {
#Value("TestDataManager is running")
private String message;
#GetMapping("/")
public String indexGet() {
return message;
}
}

Spring Boot scans the classpath, finds ExampleController and registers it as a REST controller bean.
Later when you make a call to / it uses it to return the response. Since you return a String without #GetMapping(produces = ...) it uses text/plain as response content type.
Apache Wicket is not involved in your application. I am not sure why you use/tag it.

Related

SpringBoot with RequestMapping on Application and another RequestMapping in controller classes, is that possible?

I am learing SpringBoot and Rest api just now and in all examples I have seen, in every controller classes they have a #RequestMapping("/api/v1") and then some #GetMapping or simular.
But it feels like why am I writing a RequestMapping for every controller if the first URI mapping is the same for all controllers.
Is it possible to have a #RequestMapping in the SpringBootApplication that maps "/api/v1" and then in the controller classes then have another ®RequestMapping for subfolders like "/products" and simular.
I have tried to solve this, but I cant make it function.
#SpringBootApplication
#RestController
#RequestMapping("/api/v1")
public class CommonApplication {
public static void main(String[] args) {
SpringApplication.run(CommonApplication.class, args);
}
}
#RestController
#RequestMapping()
public class ProductController {
private ProductService productService;
#GetMapping("/products")
public String getProducts()
{
return "Hello from getProducts 12";
}
}
I want this full URI "/api/v1/products" to function and return the String text. But I only get 404.
It is possible to have request mapping in both application and controller class. Go through the documentation which is attached below once for having better understanding.
https://zetcode.com/spring/requestmapping/

RestTemplate with POST request not invoked

I am trying to call a service in another Spring Boot application using RestTemplate,like below:
#RestController
public class Controller {
#Autowired
RestTemplate restTemplate;
#RequestMapping("/hello")
public String getHi() {
return restTemplate.getForObject("http://localhost:8082/hello", String.class);
}
}
This is the Main class for application A:
#SpringBootApplication
public class ServiceApplication {
#Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class, args);
}
}
Below the other Spring Boot application B:
#RestController
public class Controller {
#RequestMapping ("/hello")
public String getHi() {
return "Hi";
}
}
Application A (RestTemplate): In this I am trying to achieve the API call to go through like, Postman -> Application A -> Application B. I tried this API with Postman, but the request is not sent. Just showing "Sending request...". Also didn't get failure response.
Application B: Called GET endpoint from Postman which invokes getHi() in
Controller(RestController) class in Postman and browser, both work fine.
Port used:
Application A: 8083
Application B: 8082
Both applications are running. I have gone through lots of websites and git codes, but couldn't find a solution.

How to serve static resource in springboot having custom path from Controller?

I have this resource handler, and I am able to call the static web page located in different location , but I am trying to call from controller class I am not able to get the page
#Configuration
public class Static_ResourceHandler implements WebMvcConfigurer {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/system/files/**").addResourceLocations("file:/home/niteshb/Documents/data");
}
}
This is what I am calling
http://localhost:8080/system/files/test.html
but how to call it from controller , I was trying something like this but its not working
This is my controller class call ..
#GetMapping("/")
public String getfile() {
return "test.html";
}
Create a Get mapping for /system/files/, for which you had created the resource handler,
and return the file in the newly created method.
#GetMapping("/system/files/")
public String getStaticfile() {
return "/system/files/test.html";
}
Hope that should work.

Springboot Restcontroller refusing to work

I have spent the last 2 hours trying to figure out just what in gods name is going on with my simple springboot rest app.
NO matter what I do, I simply cannot get the restcontroller to work, every URL I try gives me a 404. Here is my code below.
#RestController
public class PbxPortalRestControllerSet {
#RequestMapping("/testMe")
public String testMe()
{
return "I am alive";
}
}
#SpringBootApplication
public class PbxPortalApplication {
public static void main(String[] args) {
SpringApplication.run(PbxPortalApplication.class, args);
}
}
Application.properties file
server.port = 8088
can anyone tell what the heck is going on? I have done this tons of times before, but I can't for the life of me figure out why this refuses to work.
I try to to go to localhost:8088/testMe, and I get a 404.
If PbxPortalApplication and PbxPortalApplication classes are in different package, you need to tell your application to scan the controller while loading up the app context.
Add the #ComponentScan to your PbxPortalApplication class
#SpringBootApplication
#ComponentScan(basePackageClasses = PbxPortalRestControllerSet.class)
public class PbxPortalApplication
I found the issue. I was using the wrong POM entry. I was using Jersey somehow instead of the built in spring ones. For some reason even though I was using the wrong library, eclipse was telling me that my annotation entries were perfectly fine. Once i deleted the entry for Jersey everything worked
#RestController
public class DemoController {
#GetMapping(value= "/getName")
public String getName(){
return "This is a Spring Boot Application";
}
}
Main Class is simple:
package com.pcftest.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);
}
}
Can you try this once ?? And see if it works..
#RequestMapping(value= "/testMe" , method = RequestMethod.GET)
Basically sometimes when you dont provide the request method type you get such types of errors
It seems that you are you have placed your controller different sub package than spring SpringApplication file. So Controller is not accessible from Spring main()
Please add
#SpringBootApplication
#ComponentScan("ControllersPackege")
CodeSnipet:SpringBootApplication
#SpringBootApplication
#ComponentScan("ControllerPackege")
public class PbxPortalApplication {
public static void main(String[] args) {
SpringApplication.run(PbxPortalApplication.class, args);
}
}
CodeSnipet:Controller
#RestController
public class PbxPortalRestControllerSet {
#RequestMapping("/testMe")
public String testMe()
{
return "I am alive";
}
}
application.properties file
server.port = 8088
NB:
Best way to put in same package or put controller class in sub-package

Could not autowire. No beans of 'NoteRepository' type found

My Controller class is below.
#Controller
public class app {
#GetMapping(path = "/")
public #ResponseBody
String hello() {
return "Hello app";
}
}
It works fine when I navigate through url. But when this below code is added it says "Could not autowire. No beans of 'NoteRepository' type found".
#Autowired
NoteRepository noteRepository;
// Create a new Note
#PostMapping("/notes")
public Note createNote(#Valid #RequestBody Note note) {
return noteRepository.save(note);
}
App controller class is in the same package where main class(which run the application) is. but when we add above code to a controller in different package it doesn't show error. but it doesn't work when we navigate through url even a simple get method.
My main class is below.
#SpringBootApplication
#EnableJpaAuditing
public class CrudApplication {
public static void main(String[] args) {
SpringApplication.run(CrudApplication.class, args);
}
}
My Repository class is
#Repository
public interface NoteRepository extends JpaRepository<Note, Long> {
}
My project structure
I want to find solution to:
Inject an instance of NoteRepository. I always get the message "Could not autowire. No beans of type found" error. Spring cannot inject it, doesn't matter if the interface is in the same or a different package.
I am not able to run methods in a controller(MyController) that are located in a different package than application entry point.
The main symptom is this:
App controller class is in the same package where main class(which run the application) is. but when we add above code to a controller in different package it doesn't show error. but it doesn't work when we navigate through url even a simple get method.
By default, Spring Boot application will only auto discover beans declared in the same package than the main class. For beans that are in a different package, you need to specify to include them. You can use #ComponentScan for this.
package foo.bar.main;
//import statements....
//this annotation will tell Spring to search for bean definitions
//in "foo.bar" package and subpackages.
#ComponentScan(basePackages = {"foo.bar"})
#SpringBootApplication
#EnableJpaAuditing
public class CrudApplication {
public static void main(String[] args) {
SpringApplication.run(CrudApplication.class, args);
}
}
package foo.bar.controller;
//import statements....
//since #ComponentScan, now this bean will be discovered
#Controller
public class app {
#GetMapping(path = "/")
public #ResponseBody
String hello() {
return "Hello app";
}
}
For Spring Data to recognize which repositories should create, you should add #EnableJpaRepositories annotation to your main class. Also, in order for Spring Data and the JPA implementation to scan the entities, add #EntityScan:
#ComponentScan(basePackages = {"foo.bar"})
#SpringBootApplication
#EnableJpaAuditing
#EnableJpaRepositories("your.repository.packagename")
#EntityScan("your.domain.packagename")
public class CrudApplication {
//code...
}
you should add packages for spring to scan them
#SpringBootApplication(scanBasePackages={"package1", "package2"})

Categories

Resources