• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Spring @RestController not working

 
Bartender
Posts: 1938
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am using the sample from spring.io to get the basic "Hello World!" SpringBoot application running.

Hurray...that part works.

But, when I try to add another @RestController, like the code below, and then try to visit that URL, as in "localhost:8080/test", I get the dreaded 404 error.

So the problem is that I can't seem to figure out how to simply configure a new REST controller to run at some URL. I'm assuming with the @EnableAutoConfiguration, I don't need to manually wire things in, but something is wrong.

Thanks in advance.

- mike


 
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you share some more details for me to help you out.Like what is in there your front end etc .
 
Saloon Keeper
Posts: 14682
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you show us your Application class, and tell us how you're starting your application?
 
Mike London
Bartender
Posts: 1938
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Can you show us your Application class, and tell us how you're starting your application?



From the spring.io site, here's my Application class:



To run the application, I just click the ">" in the IntellJ toolbar.

Running (in Firefox): "localhost:8080" gives me the expected "Hello World!"

But, I don't seem to be able to wire in more controllers.

My goal is to have multiple methods (different controllers) that can be externally called as an API.

Thanks in advance,

- mike

 
Stephan van Hulst
Saloon Keeper
Posts: 14682
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why aren't your classes in a package?
 
Mike London
Bartender
Posts: 1938
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Why aren't your classes in a package?



I was just trying to get the basic code to work and would refactor into packages later (post "Hello World" level).

Any suggestions on the actual problem I posted?

Thanks,

-- mike
 
Marshal
Posts: 4084
554
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike London wrote:

Stephan van Hulst wrote:Why aren't your classes in a package?



I was just trying to get the basic code to work and would refactor into packages later (post "Hello World" level).

Any suggestions on the actual problem I posted?

Thanks,

-- mike



Maybe that is (part of) the actual problem ...

https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-structuring-your-code.html wrote:When a class doesn’t include a package declaration it is considered to be in the “default package”. The use of the “default package” is generally discouraged, and should be avoided. It can cause particular problems for Spring Boot applications that use @ComponentScan, @EntityScan or @SpringBootApplication annotations, since every class from every jar, will be read.

 
Mike London
Bartender
Posts: 1938
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:

Mike London wrote:

Stephan van Hulst wrote:Why aren't your classes in a package?



I was just trying to get the basic code to work and would refactor into packages later (post "Hello World" level).

Any suggestions on the actual problem I posted?

Thanks,

-- mike



Maybe that is (part of) the actual problem ...

https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-structuring-your-code.html wrote:When a class doesn’t include a package declaration it is considered to be in the “default package”. The use of the “default package” is generally discouraged, and should be avoided. It can cause particular problems for Spring Boot applications that use @ComponentScan, @EntityScan or @SpringBootApplication annotations, since every class from every jar, will be read.



Gotcha. It turns out you had the answer there. Creating the package wasn't enough (though it may have been sufficient to cause the problem).

Turns out, I did not have @ComponentScan in the main App class.

Added that and everything (my two "hello" masterpiece methods) works!

Thanks so much.

- mike

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic