简体   繁体   中英

My first Springboot application doesn't start

I'm trying to build a simple springboot application. This is my class controller "HelloController.java":

package controller;

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

@RestController

public class HelloController {

    @RequestMapping("/")
    public String index()
    {
       return "Welcome to Spring Boot";
    }

}

the main class "Application.java"

    package hello;

import org.joda.time.LocalTime;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        Greeter greeter = new Greeter();
        LocalTime currentTime = new LocalTime();
        System.out.println( greeter.sayHello() );               
        System.out.println( "The current local time is: " + currentTime );
        SpringApplication.run(Application.class, args);

    }
}

and pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework</groupId>
    <artifactId>gs-maven</artifactId>
    <!--packaging>jar</packaging-->
    <version>0.1.0</version>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.2</version>
                    <executions>
                        <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                            <configuration>
                                <transformers>
                                    <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                        <mainClass>hello.Application</mainClass>
                                    </transformer>
                                </transformers>
                            </configuration>
                        </execution>
                    </executions>

            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

This is the stacktrace returned from application using IntelliJ:

2016-06-24 17:12:27.754  INFO 4072 --- [           main] hello.Application                        : Starting Application on WIN-PNSVSNOEM58 with PID 4072 (C:\Users\NICOLA\git\javaprojects\spring-base\target\classes started by NICOLA in C:\Users\NICOLA\git\javaprojects\spring-base)
2016-06-24 17:12:27.777  INFO 4072 --- [           main] hello.Application                        : No active profile set, falling back to default profiles: default
2016-06-24 17:12:28.330  INFO 4072 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7ce026d3: startup date [Fri Jun 24 17:12:28 CEST 2016]; root of context hierarchy
2016-06-24 17:12:39.844  INFO 4072 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-06-24 17:12:39.880  INFO 4072 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2016-06-24 17:12:39.883  INFO 4072 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.33
2016-06-24 17:12:40.885  INFO 4072 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2016-06-24 17:12:40.886  INFO 4072 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 12671 ms
2016-06-24 17:12:42.090  INFO 4072 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2016-06-24 17:12:42.101  INFO 4072 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-06-24 17:12:42.102  INFO 4072 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-06-24 17:12:42.102  INFO 4072 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-06-24 17:12:42.103  INFO 4072 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'requestContextFilter' to: [/*]
2016-06-24 17:12:43.195  INFO 4072 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7ce026d3: startup date [Fri Jun 24 17:12:28 CEST 2016]; root of context hierarchy
2016-06-24 17:12:43.565  INFO 4072 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2016-06-24 17:12:43.568  INFO 4072 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2016-06-24 17:12:43.671  INFO 4072 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-06-24 17:12:43.672  INFO 4072 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-06-24 17:12:44.182  INFO 4072 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-06-24 17:12:46.558  INFO 4072 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-06-24 17:12:46.783  INFO 4072 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-06-24 17:12:46.795  INFO 4072 --- [           main] hello.Application                        : Started Application in 21.875 seconds (JVM running for 28.272)

and when I start application using mvn command return this error:

[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 21.138 s
[INFO] Finished at: 2016-06-24T16:47:12+02:00
[INFO] Final Memory: 17M/40M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:2.2:shade (default) on project gs-maven: Unable to parse configuration of mojo org.apache.maven.plugins:maven-shade-plugin:2.2:shade for parameter resource: Cannot find 'resource' in class org.apache.maven.plugins.shade.resource.ManifestResourceTransformer -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1

Should work if you remove the whole maven shade plugin part. Looks like you are not declaring any entries to be overriden there anyway. Then maven build should work ok and you should be able to deploy resulting artifact.

Moreover, it looks like you are using slightly different syntax in the plugin that in the official docs, specifically the main class:

<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
    <manifestEntries>
        <Main-Class>${app.main.class}</Main-Class>
        <X-Compile-Source-JDK>${maven.compile.source}</X-Compile-Source-JDK>
        <X-Compile-Target-JDK>${maven.compile.target}</X-Compile-Target-JDK>
     </manifestEntries>
</transformer>

https://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html#ManifestResourceTransformer

Ok, I remove the maven plugin shade from pox.xml but I'm not solve the problem. Anyway I'm trying the example from the spring.io website:

[ https://spring.io/guides/gs/maven/]

and the pom.xml file uses this plugin. I don't understand exactly what the maven's goal "shade" does and the main motive for inserting it in this simple example in order to display a greet in the localhost:8080. About it, when I insert the url

[ http://localhost:8080/]

it gives me a 404 http status-code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM