spring-guides / gs-consuming-rest

Consuming a RESTful Web Service :: Learn how to retrieve web page data with Spring's RestTemplate.
https://spring.io/guides/gs/consuming-rest/
Apache License 2.0
195 stars 287 forks source link

Don't start tomcat as a REST consumer. #17

Closed duelle closed 8 years ago

duelle commented 8 years ago

Removed SpringBootApplication and CommandLineRunner from Application class. So it does not start a tomcat instance on its own.

When I tried to run a REST service provider (locally) and a consumer based on this example I found out that this consumer example also tries to start a tomcat instance. That lead to an "address already in use" exception. Apart from that it seems to be unnecessary for a consumer to start tomcat on its own.

Therefor, I removed this part from the Application class.

dsyer commented 8 years ago

That would only be a problem if your two apps shared a classpath. If you use the project here it isn't an issue so I don't think we want this change in the guide particularly. Maybe we should add a note to the README saying that to launch the application when Tomcat is on the classpath you need to set webEnvironment=false in the SpringApplication.

gregturn commented 8 years ago

This example by itself doesn't run Tomcat:

/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.3.3.RELEASE)

2016-02-26 10:15:17.065  INFO 83719 --- [           main] hello.Application                        : Starting Application on retina with PID 83719 (/Users/gturnquist/src/spring-guides/gs-consuming-rest/complete/target/classes started by gturnquist in /Users/gturnquist/src/spring-guides/gs-consuming-rest/complete)
2016-02-26 10:15:17.068  INFO 83719 --- [           main] hello.Application                        : No active profile set, falling back to default profiles: default
2016-02-26 10:15:17.101  INFO 83719 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1c9597cd: startup date [Fri Feb 26 10:15:17 CST 2016]; root of context hierarchy
2016-02-26 10:15:17.568  INFO 83719 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-02-26 10:15:17.944  INFO 83719 --- [           main] hello.Application                        : Quote{type='success', value=Value{id=8, quote='I don't worry about my code scaling. Boot allows the developer to peel back the layers and customize when it's appropriate while keeping the conventions that just work.'}}
2016-02-26 10:15:17.945  INFO 83719 --- [           main] hello.Application                        : Started Application in 1.168 seconds (JVM running for 9.125)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.349 s
[INFO] Finished at: 2016-02-26T10:15:17-06:00
[INFO] Final Memory: 34M/335M
[INFO] ------------------------------------------------------------------------
2016-02-26 10:15:18.062  INFO 83719 --- [       Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@1c9597cd: startup date [Fri Feb 26 10:15:17 CST 2016]; root of context hierarchy
2016-02-26 10:15:18.064  INFO 83719 --- [       Thread-1] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

I'm not sure how to word such a warning. If you run this app with Tomcat, then you risk a collision with another Tomcat? That is true for every guide.

duelle commented 8 years ago

Thank you for your feedback. In my case I created two separate projects (one service and one consumer) and ran into the "address already in use" issue. I didn't set or change anything regarding the classpath or tomcat in particular.

When testing the example consumer with my changes in place it worked exaclty as described - so I thought this would not be necessary and removed it. Or is there something I am missing here?

If the current state is the expected and intended behaviour, you can of course close this PR.

dsyer commented 8 years ago

it worked exaclty as described

I don't think it can have worked as described - if you remove the main method there is nothing to launch any more. Take a look at your classpath again: you have a different set of libraries than this sample if your app is starting Tomcat.

gregturn commented 8 years ago

If you open gs-consuming-rest (with no edits) inside your IDE, you should find this set of dependencies:

You'll notice, there is no spring-boot-starter-web and no Tomcat, so it can't start a Tomcat server. You had to have altered the build file to make it start doing that.

duelle commented 8 years ago

There is a main method and following the instructions on how to run it using the Gradle wrapper it seems to work:

[duelle : gs-consuming-rest]$ git remote -v
origin  git@github.com:duelle/gs-consuming-rest.git (fetch)
origin  git@github.com:duelle/gs-consuming-rest.git (push)

[duelle : gs-consuming-rest]$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean

[duelle : gs-consuming-rest]$ cd complete/

[duelle : complete]$ ./gradlew clean build
:clean
:compileJava
:processResources UP-TO-DATE
:classes
:findMainClass
:jar
:bootRepackage
:assemble
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build

BUILD SUCCESSFUL

Total time: 5.203 secs

[duelle : complete]$ java -jar build/libs/gs-consuming-rest-0.1.0.jar
18:09:11.514 [main] DEBUG o.s.web.client.RestTemplate - Created GET request for "http://gturnquist-quoters.cfapps.io/api/random"
18:09:11.561 [main] DEBUG o.s.web.client.RestTemplate - Setting request Accept header to [application/json, application/*+json]
18:09:11.800 [main] DEBUG o.s.web.client.RestTemplate - GET request for "http://gturnquist-quoters.cfapps.io/api/random" resulted in 200 (OK)
18:09:11.802 [main] DEBUG o.s.web.client.RestTemplate - Reading [class hello.Quote] as "application/json;charset=UTF-8" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@dcdf5d7]
18:09:11.822 [main] INFO  hello.Application - Quote{type='success', value=Value{id=12, quote='@springboot with @springframework is pure productivity! Who said in #java one has to write double the code than in other langs? #newFavLib'}}

[duelle : complete]$

As I already stated, I didn't change any configurations, classpaths etc. and just thought I could help preventing others from running into the same "issue" I had when running this example with a local service.

duelle commented 8 years ago

Ok, now I can't reproduce my "issue" anymore. Sorry for the confusion guys and thanks again for your fast replies and patience!

dsyer commented 8 years ago

No problem at all. Thanks for your interest and for trying to help others.