Spring不匹配我的请求到我的控制器

时间:2019-06-10 18:00:59

标签: java spring-boot spring-mvc

这是我的控制人:

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 - Did not find handler method for

AddressController添加到@ComponentScan列表中并没有任何改变。

3 个答案:

答案 0 :(得分:0)

您为路径指定了错误的值。使您的控制器是这样的。 @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的文档 并检查value属性在两个注释中是否具有不同的功能。这里是一个有效的示例https://github.com/BALVIKASHSHARMA/SampleBlog

答案 1 :(得分:0)

事实证明,问题出在@RequestMapping。我以为如果在RestController上放置“ / address”,然后在总路径为“ / address / method”的方法上进行“ validate”。

事实证明,映射是“ / validate”。我将请求方法更改为“ / address / validate”,并且工作正常。

感谢您的帮助。

答案 2 :(得分:-1)

从Application类中删除以下行。

@ComponentScan(basePackageClasses = {Application.class, PizzaPlaceController.class} )

它应该为您工作。