Erstellen eines RESTful web service mit Spring Boot

Ich Lerne RESTful web services mit Spring boot. Ich bin versuchen, um eine web-service holt-Adresse von einem bestimmten client.Jedoch, wenn ich versuche das ausführen der service, den ich erhalte immer folgende Fehlermeldung:

Whitelabel-Fehler-Seite

Diese Anwendung hat keine explizite Zuordnung für /Fehler, so dass Sie sehen
diese als fallback.

Sun Jan 03 11:20:44 CST 2016 Es wurde ein unerwarteter Fehler (Typ=Nicht
Gefunden, status=404). Keine Nachricht verfügbar

Die URL die ich versuche zuzugreifen, ist

http://localhost:8084/showAddress

Kann mir bitte jemand sagen, wo mache ich falsch. Ich heruntergeladen habe, ein ähnliches Projekt von einem Freund github-account und es läuft perfekt OK.
Der Einfachheit halber habe ich versucht hart zu Kodieren Sie die Werte und erstellt den folgenden code in meiner controller-Klasse:

package com.digitek.controller;

import java.math.BigInteger;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.example.model.Address;


@RestController
public class Controller {

    private static BigInteger id;
    private static Map<BigInteger, Address> addressMap;
    //saves address objects into HashMap
    private static void SaveAddress(Address address){
        //instantiate hashmap when id is null
        if(id == null){
            id = BigInteger.ONE;
            addressMap = new HashMap<BigInteger,Address>();
        }
        address.setId(id);
        id.add(BigInteger.ONE);
        addressMap.put(address.getId(), address);
    }

    static{
        Address a1 = new Address();
        a1.setAddress("29 East Judith Ann Drive");
        SaveAddress(a1);

        Address a2 = new Address();
        a1.setAddress("2 East Judith Ann Drive");
        SaveAddress(a2);
    }

    @RequestMapping(value = "/showAddress" ,method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Collection<Address>> showMessage(){
        Collection<Address> address = addressMap.values();
        return new ResponseEntity<Collection<Address>>(address , HttpStatus.OK);

    }



}

Hier ist meine pom.xml Datei

<?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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>AddressService</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>AddressService</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

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

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

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


</project>

Hier ist die Konsole

    2016-01-03 11:09:30.359  INFO 6028 --- [           main] com.example.AddressServiceApplication    : Starting AddressServiceApplication on Rishit with PID 6028 (started by Rishit Shah in D:\Rishit\Java workspaces\AddressService) 
2016-01-03 11:09:30.364  INFO 6028 --- [           main] com.example.AddressServiceApplication    : No active profile set, falling back to default profiles: default 
2016-01-03 11:09:30.449  INFO 6028 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@33cb5951: startup date [Sun Jan 03 11:09:30 CST 2016]; root of context hierarchy 2016-01-03 11:09:31.655  INFO 6028 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'beanNameViewResolver' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]] 
2016-01-03 11:09:32.792  INFO 6028 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8084 (http) 
2016-01-03 11:09:32.814  INFO 6028 --- [          main] o.apache.catalina.core.StandardService   : Starting service Tomcat 2016-01-03 11:09:32.816  INFO 6028 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.30 2016-01-03 11:09:32.965  INFO 6028 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext 2016-01-03 11:09:32.965  INFO 6028 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2521 ms 
2016-01-03 11:09:33.628  INFO 6028
    --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/] 
2016-01-03 11:09:33.637  INFO 6028 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*] 
2016-01-03 11:09:33.639  INFO 6028
    --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 
2016-01-03 11:09:33.639  INFO 6028 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'httpPutFormContentFilter' to: [/*]
 2016-01-03 11:09:33.639  INFO 6028
    --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'requestContextFilter' to: [/*]
 2016-01-03 11:09:34.221  INFO 6028 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@33cb5951: startup date [Sun Jan 03 11:09:30 CST 2016]; root of context hierarchy 2016-01-03 11:09:34.315  INFO 6028 --- [           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-01-03 11:09:34.317  INFO 6028 --- [           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-01-03 11:09:34.371  INFO 6028 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
 2016-01-03 11:09:34.371  INFO 6028 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2016-01-03 11:09:34.421  INFO 6028 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2016-01-03 11:09:34.588  INFO 6028 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup 
2016-01-03 11:09:34.753  INFO 6028 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8084 (http) 
2016-01-03 11:09:34.764  INFO 6028 --- [          main] com.example.AddressServiceApplication    : Started AddressServiceApplication in 4.867 seconds (JVM running for 5.705) 
2016-01-03 11:10:03.737  INFO 6028 --- [nio-8084-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet' 2016-01-03 11:10:03.737  INFO 6028 --- [nio-8084-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started 2016-01-03 11:10:03.759  INFO 6028 --- [nio-8084-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 22 ms

P. S
Ich habe versucht mit der Anwendung auf verschiedenen ports versucht zu löschen und neu zu erstellen, und auch versucht, läuft eine ähnliche Anwendung, die ich heruntergeladen habe von github erstellt von meinem Freund. Jedes mal, wenn seine Anwendung funktioniert, aber mir nicht. Ich habe auch sicher, dass jede und Elemente unseres pom-Dateien übereinstimmen.
Vielen Dank im Voraus

  • was war der url, die Sie versucht, Zugriff auf?
  • die url ist localhost:8084/showAddress. Ich habe auch aktualisiert, es in der Frage
  • Fehler 404 bedeutet Nicht Gefunden, so sind Sie nicht der Bereitstellung richtig. Machen einfacher .war-Datei, unter der Annahme, dass ist, was Sie tun, mit nichts aber eine grundlegende index.html Seite und, dass die Arbeit. Arbeiten Sie dann bis zu Ihrer gewünschten Anwendung. Sind Sie bereitstellen als ROOT.Krieg? Wenn nicht, haben Sie wahrscheinlich ein context-Präfix. Sind Sie sicher, dass der server ordnungsgemäß konfiguriert ist für Ihren Hafen?
  • Vielen Dank nicholas. Ich denke, der server sollte nicht ein Problem sein, wenn mein Freund hat die gleiche app und es läuft auf meinem pc. Ich habe es runtergeladen von github. Und es ist standardmäßig Verpackung als jar-Datei. Auch spring mvc apps, die ich bisher erstellt wurden funktionieren. Es ist gerade dieses, das zeigt dieser Fehler
  • Post von der Klasse, die enthält Ihre main Methode, bitte.
  • Warum machen Sie die eingebettete tomcat -provided? wenn Sie planen, um die Bereitstellung einer war Datei auf einem standalone tomcat, warum machen Sie ein jar?
  • Sind Sie sicher, dass der controller holt den Frühling? (Sie sehen die Zuordnung /showAddress in die startup-logs)

InformationsquelleAutor Rishit Shah | 2016-01-03
Schreibe einen Kommentar