使用Spring Boot创建RESTful Web服务

时间:2016-01-03 17:34:37

标签: java spring rest spring-mvc

我正在使用Spring启动学习RESTful Web服务。我正在尝试创建一个获取特定客户端地址的Web服务。但是,当我尝试运行该服务时,我不断收到以下错误:

  

Whitelabel错误页面

     

这个应用程序没有/ error的显式映射,所以你看到了   这是一个后备。

     

Sun Jan 03 11:20:44 CST 2016出现意外错误(type = Not   找到了,状态= 404)。没有可用的消息

我尝试访问的网址是

  

http://localhost:8084/showAddress

有人可以告诉我哪里出错了。我从朋友的github帐户下载了一个类似的项目,它运行得很好。  为了简单起见,我尝试对值进行硬编码,并在我的控制器类中创建以下代码:

\n

这是我的pom.xml文件

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);

    }



}

这是控制台日志

<?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>

P.S 我尝试在不同的端口上运行应用程序,尝试删除并重新创建它,并尝试运行我从朋友创建的github下载的类似应用程序。每次他的申请工作,但我的申请没有。我还确保我们的pom文件的每个和元素匹配。 提前谢谢

1 个答案:

答案 0 :(得分:13)

确保您的主类位于其他类之上的根包中。

当您运行Spring Boot Application(即使用@SpringBootApplication注释的类)时,Spring将仅扫描主类包下面的类。

  com
   +- digitek
         +- Application.java  <--- your main class should be here, above your controller classes
         |
         +- model
         |   +- Address.java
         +- controller
             +- AddressController.java

您遇到的问题是Spring无法找到您创建的控制器,因为您将其放在Spring未扫描的目录中。

文档中有一章解释了如何使用spring boot here.

构建代码