file : CourseApiApp.java
package io.javabrains.springbootstarter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class CourseApiApp {
public static void main(String[] args){
//this is the first step to convert the application into spring app
SpringApplication.run(CourseApiApp.class,args);
}
}
File HelloControllerTwo
package io.javabrains.springbootstarter.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
#RequestMapping("/hello")
public class HelloControllerTwo {
public String sayHi() {
return "Hi";
}
}
I have created a very basic spring app to get hi on /hello path. I but I am still getting a fallback /error message on /hello
Could anyone please let me know what am I doing wrong?
Your #RequestMapping annotation should be on your method, not your class.
You should also specify a method in your annotation.
Try this:
#RestController
public class HelloController {
#RequestMapping(value = "/hello", method = RequestMethod.GET)
public String sayHello() {
return "Hello";
}
}
Related
I am running a simple Spring boot application, In console its running successfully. But when I am trying by postman it gives this error message.
{
"timestamp": "2020-07-26T04:22:15.626+00:00",
"status": 404,
"error": "Not Found",
"message": "",
"path": "/loginRegistration/user/"
}
My project structure is...
My Main class is
import org.example.controller.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
#SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
System.out.println("Welcome");
}
}
My Controller is....
import com.fasterxml.jackson.core.JsonProcessingException;
import org.example.entity.Registration;
import org.example.model.UserDto;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
#RestController
#RequestMapping("/user")
public class User {
#Autowired
UserService userService;
#RequestMapping(value = "/registration", method = RequestMethod.POST)
public String registerUser(#Validated UserDto userdto) throws JsonProcessingException {
userService.registerUser(userdto);
return "success";
}
#RequestMapping(value = "/getUserList", method = RequestMethod.GET)
public List<Registration> getUserList() {
List<Registration> list = userService.getUserList();
return list;
}
#RequestMapping(value = "/")
public String test() {
return "testing";
}
}
Please guide me what changes need to be done to run on postman.
spring application name does not use as context path. You will have to
define below property in your application.properties file to use
'loginRegistration' as your context path.
server.servlet.context-path=/loginRegistration
And now you can use it as /loginRegistration/user/
Let me know if that helps. Thanks
You have to make sure, you providing war name along with your given URL... And see packages should be under you main class... if not u have to add in main class using componentscan
Another alternative to what #Jimmy described is to define #RequestMapping in your User class as -
#RestController
#RequestMapping("/loginRegistration/user/")
public class User {
I am trying to execute my new Spring Boot application.
The first two classes are:
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class UbbioneApplication {
public static void main(String[] args) {
SpringApplication.run(UbbioneApplication.class, args);
}
}
then the servlet Initializer class
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(UbbioneApplication.class);
}
}
But when I am used to run my application by writing mvn spring-boot:run in the console, I have this message appearing:
Whitelabel Error Page
Could you help me please how to resolve this issue?
Thanks in advance.
I think I have an answer:
I created a controller to my application and I updated my code as following:
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
#Configuration
#EnableAutoConfiguration
#ComponentScan(basePackages = {"Name_controller_path"})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Then my controller will look like this:
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class Appcontroller {
#RequestMapping(value = "/home", method = RequestMethod.GET)
String home() {
return "home";
}
}
Then use this path to view your execution: http://localhost:8080/home.
I'm building a spring boot application. My problem is that when I run the project, my login page is not shown. This is my code:
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class RestLogin {
#RequestMapping("/")
public String login() {
return "login";
}
}
I get only a white page and "login" is written in it. When I try to change the #RestController with #Controller I get this GET http://localhost:8080/ 404 (). My login.html page is located under the webapp>tpl>login.html
How can I display my login page?
Edit
This is my application class
#SpringBootApplication
public class ExampleApplication extends SpringBootServletInitializer {
private static Logger logger = LoggerFactory.getLogger(ExampleApplication.class);
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(ExampleApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(ExampleApplication.class, args);
}
}
I dont know your configuration but:
#Configuration
#EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
#Override
protected void configure(HttpSecurity http) throws Exception {
http.httpBasic().and().authorizeRequests()
.antMatchers("/**").permitAll();
http.authorizeRequests().antMatchers("/**").permitAll();
}
}
In the Application.properties file add:
spring.mvc.view.suffix: .html
Change #RestController to #Controller for RestLogin class. Also put your html file inside the static folder inside resources folder.
You need an application class with a main method. See this tutorial.
Here's a snippet:
package hello;
import java.util.Arrays;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
System.out.println("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
};
}
}
This is the normal behavior.
New version of Spring web comes with #RestController annotation which nothing but #Controller + #ResponseBody. So when you have a return statement in a method you must use #RestController or annotate your method with #ResponseBody.
Here the problem is that Spring don't know a lot about the http method type, can you please try to use #GetMapping("/") to combinbe path and method at the same time.
According to your posted code and your description, you're getting an expected behavior.
When you annotate your controller with #RestController, that means that your methods on that controller will try to return their result as JSON.
According to your code:
#RestController
public class RestLogin {
#RequestMapping("/")
public String login() {
return "login";
}
}
You're returning the String "login", that's why you're getting empty white page with the word login as JSON
If you will change the #RestController to #Controller then it no longer will return your string as JSON,
but Spring will try to figure out from the that "login" string a view, and for that you'll need to add a view resolver bean to your project.
I am developing a simple web application using Spring boot,
Please find the below code.
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
#RestController
public class HelloApplication {
#RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}
when i am trying to Run, i am not getting the option to run.. in run as ..
please find the screen shots
Your class should be :
#Controller
#SpringBootApplication
public class SampleController {
public static void main(String[] args) {
SpringApplication.run(SampleController.class, args);
}
#RequestMapping
#ResponseBody
String home() {
return "hello world";
}
}
Due to project requirements I have to deploy a Spring application to a server incapable of running Tomcat and only capable of running WildFly. When I had a very simple project running on Tomcat and the root URL was hit (localhost:8080) it rendered my index.html. Since migrating to WildFly and refactoring the structure of my project, localhost:8080 no longer renders the index.html but I can still reach other URLs.
I've tried to implement a jboss-web.xml file under BrassDucks/src/main/webapp/WEB-INF/jboss-web.xml like this:
<jboss-web>
<context-root>Brass-Ducks</context-root>
</jboss-web>
Where Brass-Ducks is the artifactID but to no avail.
Consider my ApplicationConfig.java
package brass.ducks;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#Configuration
#ComponentScan
#EnableAutoConfiguration
public class ApplicationConfig extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(ApplicationConfig.class, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(applicationClass);
}
private static Class<ApplicationConfig> applicationClass = ApplicationConfig.class;
}
#RestController
class GreetingController {
#RequestMapping("/hello/{name}")
String hello(#PathVariable String name) {
return "Hello, " + name + "!";
}
}
and consider my Controller.java
package brass.ducks.application;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
#RestController
#RequestMapping("/")
public class Controller {
#RequestMapping("/greet")
#ResponseBody
public String greeting() {
return "Hello, there.";
}
}
And finally should it be relevant, my folder structure:
localhost:8080/greet returns "Hello, there" and localhost:8080/hello/name returns "Hello name". How can I fix this?
Depending on your exact configuration something along the lines of this should work:
#Controller
public class LandingPageController {
#RequestMapping({"/","/home"})
public String showHomePage(Map<String, Object> model) {
return "/WEB-INF/index.html";
}
}
This is going to explicitly map / to index.html.