I am a Spring Boot newbie and one thing bothers me: if I have a simple Spring Boot application like this:
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
I can call the default web page of the application from a browser by using URL:
http://localhost:8080/greeting
To return a page called greeting.html which is specified by the controller:
#RequestMapping("/greeting")
public String greeting() Model model) {
return "greeting";
}
Is there a way to have Spring Boot automatically open greeting.html in the browser? Can I tell Spring boot which controller method I want it to run when the project starts?
Not sure if there is a setting you can set for this case but what you can do is using redirection.
Just map "/" and redirect to "/greeting":
#RequestMapping("/")
public String index(Model model) {
return "redirect: greeting";
}
But if you want that the browsers open any page after lunching the application you should lookup if your IDE can do that.
Just map your greeting page to the root:
#RequestMapping("/")
public String greeting() Model model) {
return "greeting";
}
Then when you call http://localhost:8080/ you will be redirected to the greeting page as a default page.
Related
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/
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.
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.
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
I'm starting an embedded tomcat via spring-boot and want to serve a static index.html page as part of a running application.
But the following does not work:
#SpringBootApplication
public class HMyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
#RestController
public class HomeContoller {
#RequestMapping("/")
public String index() {
return "index";
}
}
src/main/resources/static/index.html
Result: when I call localhost:8080, I just see the word "index", but not my html page. Why?
My fault: I had an additional class with #EnableWebMvc annotation. This somehow messed up the spring-boot autoconfiguration. I removed it and now it works returning index.html.
For me this worked, i am sure there is a better way ( like without .html ).
#RequestMapping("/")
public String index() {
return "index.html";
}
You can use ModelAndView in order to serve static HTML content in spring boot.
#RequestMapping("/")
public ModelAndView home()
{
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
return modelAndView;
}
application.properties:-
spring.mvc.view.suffix = .html
HTML File : - src/main/resources/static/index.html
Thats because of #RestController annotation, just removing this annotation works for me.