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

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

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

这是我的控制器:

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”,它运行正常。

谢谢您的帮助。

用户回答回答于

您为路径指定了错误的值。让你的控制器像这样。@SpringBootApplication扫描当前包及其子包中的所有文件。如果您的控制器在当前包之外,则只需要提供扫描路径。

     @RestController// value is the component name 
     @RequestMapping(value = "/blogs")// this value is the path
     public class BlogController {

       @Autowired
       private BlogService<BlogRequestDTO, BlogResponseDTO> service;

       @GetMapping
       public ResponseEntity<List<BlogResponseDTO>> getAllBlog() {
          return new ResponseEntity<>(service.getAll(), 
             HttpStatus.OK);
       }

       @GetMapping(value = "/{blogId}")
       public ResponseEntity<BlogResponseDTO> 
          getBlog(@PathVariable(required = true) String blogId) {
          BlogRequestDTO request = new BlogRequestDTO();
          request.setId(Long.valueOf(blogId));
          return new ResponseEntity<>(service.get(request), 
          HttpStatus.OK);
       }
    }

我们需要阅读@RestController,@ RequestMapping的文档,并检查值属性在两个注释中是否有不同的功能。一个工作样本在这里https://github.com/BALVIKASHSHARMA/SampleBlog

扫码关注云+社区

领取腾讯云代金券