0

I wanna to pass List in get method! Here is my GeoPointRequest class:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class GeoPointRequest {

    private Double lat;
    private Double lon;
}

But when I do this, I've got an exception:

java.lang.NoSuchMethodException: java.util.List.<init>()
    at java.base/java.lang.Class.getConstructor0(Class.java:3350)
    at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2554)
    at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:216)
    at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:84)
    at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:139)
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:127)
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:167)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)

And my request method looks like this:

@GetMapping(value = "/getPointsByPolygon")
    public List<GeoPointModelResponse> getPointsByPolygon(List<GeoPointRequest> geoPointRequests) {
        return searchService.getPointsByPolygon(geoPointRequests);
    }

I wanna build something like that - scheme//host:port/methodname?myObject=lat-lon,lat-lon, ....

What am I doing wrong? How it can be fixed?

Mefisto_Fell
  • 876
  • 1
  • 10
  • 30
  • 3
    Possible duplicate of [Spring Data JPA : java.lang.NoSuchMethodException: java.util.List.()](https://stackoverflow.com/questions/52629519/spring-data-jpa-java-lang-nosuchmethodexception-java-util-list-init) – Harry Joy Oct 04 '19 at 12:24
  • This post consist of PutMapping which can be used with @RequestBody, in my post I'm using GetMapping – Mefisto_Fell Oct 04 '19 at 12:50

2 Answers2

0

@RequestBody can be added along with PutMapping or PostMapping. Since you are using GetMapping, there is no body attached with request. So you may try with @RequestParam

public List<GeoPointModelResponse> getPointsByPolygon(List<GeoPointRequest> geoPointRequests)

and Remove @AllArgsConstructor and @NoArgsConstructor. Because @Data has @ToString, @EqualsAndHashCode, @Getter / @Setter and @RequiredArgsConstructor together

varman
  • 8,704
  • 5
  • 19
  • 53
  • 3
    Get method cannot have body – Mefisto_Fell Oct 04 '19 at 12:29
  • 3
    lombok `@Data` will not create `@NoArg` or `@AllArg` constructors https://projectlombok.org/features/Data – Ryuzaki L Oct 04 '19 at 12:34
  • @Mefisto_Fell Sorry, I haven't noticed, now I updated.. You are having Default constructor. So what you can do is, use `@RequestParam` and get the value, the data type that you get should be same as Entity class where you ealily map to it – varman Oct 04 '19 at 12:49
  • @varman Thank you! But, How does the url will be look like, I cannot recognize it)? – Mefisto_Fell Oct 04 '19 at 13:44
  • @Mefisto_Fell have you configured you port in application.properties? have you given `@RequestMapping`in Controller? if not, `localhost:8080/getPointsByPolygon` is the URL, you may check the response through postman. – varman Oct 04 '19 at 13:56
  • @Mefisto_Fell and brief us, what would you try to achieve through the scenario. – varman Oct 04 '19 at 13:57
  • @varman Yes, I've configured my application.properties and Controller class. The thing that I don't understand it what will be after that url `localhost:8080/getPointByPolygon?geoPointsRequest=`(here has to be lat and lon field/s from GeoPointRequest class, but I don't know how define them correctly. Like this `localhost:8080/getPointByPolygon?geoPointsRequest=lat=124,lon=1432&geoPointRequest` or how?) – Mefisto_Fell Oct 04 '19 at 14:18
  • @Mefisto_Fell are you using postman? if not, use postman, it's easy to use. How many parameters, you need to pass? Or `@RequestParam List geoPointRequests` can be used – varman Oct 04 '19 at 14:45
  • This won't work out of the box with path variables. You need to write the converter, like I explained in my answer, or pass your parameters through the request's body (in which case you can't use the HTTP verb GET). – Gustavo Passini Oct 05 '19 at 23:24
0

You might need to create your own converter and bind it to Spring's DataBinder to be able to translate path variables to a custom object.

From Spring documenation - URI patterns:

URI variables are automatically converted to the appropriate type, or TypeMismatchException is raised. Simple types (int, long, Date, and so on) are supported by default and you can register support for any other data type. See Type Conversion and DataBinder.

Gustavo Passini
  • 2,348
  • 19
  • 25