Spring不能将我的请求与我的控制器匹配

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (19)

这是我的控制器:

package pizzainthecloud.pizzaplace.controller;

import com.heavyweightsoftware.exception.HeavyweightException;
import org.addycaddy.client.dto.ContactPointDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pizzainthecloud.pizzaplace.service.AddressValidationService;

import java.util.Random;

@CrossOrigin
@RestController(value = "/address")
public class AddressController {
    public static final String          KEY_ADDRESS = "address";

    private static final Logger         log = LoggerFactory.getLogger(AddressController.class);

    @Autowired
    private AddressValidationService    addressValidationService;

    private Random                      random = new Random();
    private ContactPointDto[]           contactPointDtos = new ContactPointDto[] {
        getContactPoint1(),
        getContactPoint2(),
        getContactPoint3(),
        getContactPoint4(),
        getContactPoint5()
    };

    @RequestMapping(value = "/validate", method = RequestMethod.POST)
    @ResponseBody
    public ContactPointDto[] validate(@RequestParam(KEY_ADDRESS) ContactPointDto address) {

        ContactPointDto[] result;
//        try {
//            result = addressValidationService.validate(address);
//        } catch (HeavyweightException he) {
//            String msg = "Error validating address:" + address;
//            log.error(msg, he);
//            result = new ContactPointDto[0];
//        }

        if (random.nextBoolean()) {
            //sometimes return just one
            int idx = random.nextInt(contactPointDtos.length);
            result = new ContactPointDto[] {contactPointDtos[idx]};
        }
        else {
            result = contactPointDtos;
        }

        return result;
    }

    public static ContactPointDto getContactPoint1() {
        ContactPointDto result = new ContactPointDto();

        result.setStreet1("1 Testy Person Way");
        result.setCity("Testerville");
        result.setState("KY");
        result.setPostalCode("40419");

        return result;
    }

    public static ContactPointDto getContactPoint2() {
        ContactPointDto result = new ContactPointDto();

        result.setStreet1("2 Testy Person Way");
        result.setCity("Testerville");
        result.setState("KY");
        result.setPostalCode("40419");

        return result;
    }

    public static ContactPointDto getContactPoint3() {
        ContactPointDto result = new ContactPointDto();

        result.setStreet1("3 Testy Person Way");
        result.setCity("Testerville");
        result.setState("KY");
        result.setPostalCode("40419");

        return result;
    }

    public static ContactPointDto getContactPoint4() {
        ContactPointDto result = new ContactPointDto();

        result.setStreet1("4 Testy Person Way");
        result.setCity("Testerville");
        result.setState("KY");
        result.setPostalCode("40419");

        return result;
    }

    public static ContactPointDto getContactPoint5() {
        ContactPointDto result = new ContactPointDto();

        result.setStreet1("5 Testy Person Way");
        result.setCity("Testerville");
        result.setState("KY");
        result.setPostalCode("40419");

        return result;
    }
}

当我运行Spring Boot应用程序时,我得到:

2019-06-10 13:32:28.368 DEBUG 4224 --- [qtp531576940-32] o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing OPTIONS request for [/address/validate]
2019-06-10 13:32:28.368 DEBUG 4224 --- [qtp531576940-32] o.s.b.a.e.mvc.EndpointHandlerMapping     : Looking up handler method for path /address/validate
2019-06-10 13:32:28.370 DEBUG 4224 --- [qtp531576940-32] o.s.b.a.e.mvc.EndpointHandlerMapping     : Did not find handler method for [/address/validate]
2019-06-10 13:32:28.370 DEBUG 4224 --- [qtp531576940-32] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /address/validate
2019-06-10 13:32:28.372 DEBUG 4224 --- [qtp531576940-32] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/address/validate]
2019-06-10 13:32:28.372 DEBUG 4224 --- [qtp531576940-32] o.s.w.s.handler.SimpleUrlHandlerMapping  : Matching patterns for request [/address/validate] are [/**]
2019-06-10 13:32:28.373 DEBUG 4224 --- [qtp531576940-32] o.s.w.s.handler.SimpleUrlHandlerMapping  : URI Template variables for request [/address/validate] are {}
2019-06-10 13:32:28.373 DEBUG 4224 --- [qtp531576940-32] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapping [/address/validate] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@7ec5d3e1]]] and 1 interceptor

所以它似乎并没有映射我的控制器。我包括:

@SpringBootApplication
@EnableTransactionManagement
@ComponentScan(basePackageClasses = {Application.class, PizzaPlaceController.class} )
@EnableScheduling
public class Application {

在我的应用程序类中PizzaPlaceController,它在哪里工作,并且在同一个包中AddressController,它发现PizzaPlaceController就好了。

这里的答案没有帮助:RequestMappingHandlerMapping.getHandlerInternal:230 - 没有找到处理程序方法

添加AddressController@ComponentScan列表并没有改变任何东西。

提问于
用户回答回答于

事实证明问题是@RequestMapping。我想如果你把“/ address”放在RestController上,那么“验证”总路径为“/ address / method”的方法。

事实证明,映射是“/ validate”。我将请求方法更改为“/ address / validate”,它运行正常。

谢谢您的帮助。

热门问答

智能钛能处理大数据文件吗?我有30G的数据存储在本地电脑硬盘上?

腾讯智能钛AI开发者

腾讯云 · 智能钛产品团队 (已认证)

腾讯智能钛产品团队官方运营账号。分享产品最新动态,第一时间解答用户疑问。
推荐

硬盘可以升级吗?

最爱开车啦互联网的敏感者
推荐

可以升级,小微活动中没有云硬盘的的,后续可以用同帐号在官网购买和服务器同地域同地区的云硬盘挂载到服务器上使用。

云硬盘挂载初始化方法 可参考 https://cloud.tencent.com/document/product/362/6735

云通信 70402 错误?

推荐已采纳

参数非法,请检查必填字段是否填充,或者字段的填充是否满足协议要求。

您好,可参考此文档:https://cloud.tencent.com/document/product/269/1671

录音识别sdk的录音文件如何保存?

Richel码农
推荐

1.实时语音sdk内置录音器采集音频暂不支持保存语音数据,如需要保存语音数据建议上层自行采集音频数据。

2.实时语音识别sdk不支持暂停&继续操作,停止识别后重新调用start即开始识别。

云通讯im 本地消息存储数据库能否开放使用,要做一个本地聊天记录搜索功能?

推荐
您好,首先核实下您是否咨询app本地存储?App 本地存储;默认情况下,SDK 内部会对收到的消息进行存储,无需用户进行存储。用户可调用接口获取本地消息(无网络操作),另外,通过 getMessage 接口,也会获取本地消息,如果本地消息存在断层,会通过漫游消息补全。消息存储详参...... 展开详请

服务器操作台不是Windows咋换?

蒋小爱

腾讯云 · 技术支持 (已认证)

推荐
您好,您参考重装Windows系统;仅支持中国大陆地域(不含香港)。例如,Linux 重装为 Windows,Windows 重装为 Linux 。 登陆控制台 图片.png 选择【公共镜像】中Windows ,配置Windows密码,点击【开始重装】即可更换Windows 图片...... 展开详请

扫码关注云+社区

领取腾讯云代金券