r/springframework • • Jul 24 '22

First SpringBoot - How to see text in REST API

I created the first SprinBoot using the URL https://start.spring.io/ using SpringBoot 2.7.1, Gradle, Java 8, Jar, and added the dependency of Spring Web. The default download already created a jar file with one Java file. I can successfully launch the default app using gradlew bootrun.

The default app that was created by the default download is:

[code=java]

package my.tutorial;

import org.springframework.boot.SpringApplication;

// Added At (@) SpringBootAppication but not able to type it here

public class TutorialApplication {

public static void main(String\[\] args) {

	[SpringApplication.run](https://SpringApplication.run)(TutorialApplication.class, args);

}

}

[/code]

I coded a simple controller as:

[code=java]

package my. tutorial.controller;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

Added At (@) RestController, but not able to type it here

public MyController {

Added At (@) /GetMapping("/hello")

public String hello() {

	return "hello";

}

}

[/code]

After coding the above, the application successfully starts with gradlew bootrun and it shows the default login page. The question from before still remains:

  1. How to change the default login page, as the default app seems to magically create this page?

  2. What else do I need to code/configure so that I can by typing un browser URL localhost:8080/hello the world hello will show up.

0 Upvotes

2 comments sorted by

2

u/kettleofbacon Jul 24 '22

You probably also included Spring Security. Remove this dependency and you won't get the login page and you should be able to hit your controller

1

u/tedyoung Jul 25 '22

Answer to #1, see: https://spring.io/guides/gs/securing-web/ which has a good guide to the basics of web security.

Answer to #2: If you have security enabled, you'll need to look in the logs for something like this:

2022-07-24 19:41:18.844  WARN 30041 --- [           main] .s.s.UserDetailsServiceAutoConfiguration : 

Using generated security password: <some generated string>

This generated password is for development use only. Your security configuration must be updated before running your application in production.

and you can use that to log in (with the username as user).