-1

Here is my controller:

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

When I run my Spring Boot application, I get:

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

So it seems to be that it's not mapping my controller. I have included:

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

In my application class where PizzaPlaceController is working and is in the same package as AddressController, and it finds PizzaPlaceController just fine.

Answers here not helpful: RequestMappingHandlerMapping.getHandlerInternal:230 - Did not find handler method for

Adding AddressController to the @ComponentScan list didn't change anything.

Thom
  • 14,013
  • 25
  • 105
  • 185
  • Your logs say you made the call before it was mapped. "Did not find handler method for [/address/validate]" ... "Mapping [/address/validate] to HandlerExecutionChain with handler" Wait until it says its mapped then try hitting it – Philip Rego Jun 10 '19 at 18:14
  • Also says you're making an Options request. But you method only handles POST. Try making a simple GetMapping in this class and see if you can hit it. – Philip Rego Jun 10 '19 at 18:15
  • what happens if you just use `@ComponentScan("pizzainthecloud.pizzaplace")` – John Eipe Jun 10 '19 at 18:17
  • this line `@RequestParam(KEY_ADDRESS) ContactPointDto` makes me wonder if you have defined a constructor for ContactPointDto which receives a simple string. – Perimosh Jun 10 '19 at 18:24

3 Answers3

0

You had given the wrong value for the path. Make your controller like this. @SpringBootApplication scan all the files in the current package and its sub package. If your controller is outside the current package then only you need to give the path to scan.

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

We need to read the documentation of @RestController, @RequestMapping and check the value attribute have different function in both the annotation. A working sample is here https://github.com/BALVIKASHSHARMA/SampleBlog

  • Again, I have other controllers right next to this one that work fine. It has to be something specific about my controller. – Thom Jun 10 '19 at 18:29
  • you can have as many controller you want to differentiate two controllers use RequestMapping on the class level itself and provide the value for requestmapping annotation. Make sure all the controllers should be in the SpringBootApplication annotated class package or its child. @Thom – Bal Vikash Sharma Jun 10 '19 at 18:33
0

It turns out the problem was the @RequestMapping. I thought that if you put "/address" on the RestController, then "validate" on the method that the total path is "/address/method".

It turns out that the mapping was "/validate". I changed the request method to "/address/validate" and it is working fine.

Thanks for the help.

Thom
  • 14,013
  • 25
  • 105
  • 185
-1

Remove below line from your Application class.

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

It should work for you.

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
Sushil Behera
  • 817
  • 6
  • 20