2

I am complete noob in Spring Boot, but I tried to create a Back-end server. I coded all the work that must be done in the background and it is working perfectly. This is the "RequestsController" class:

@AllArgsConstructor
@RestController
@RequestMapping(path = {"/api"})
public class RequestsController {

    private final HashMap<String, HashMap<String,String>> DatasetMaps = ProcessingApplication.DatasetMaps;

    @GetMapping(path = {"Check"})
    public String Check(@RequestBody CheckModel checkModel){
        System.out.println("aici");
        GetCheck getCheck = new GetCheck(this.DatasetMaps);
        return getCheck.GetCheckDatasetMethod(checkModel.checking);
    }

    @GetMapping(path = {"Search"})
    public HashMap<String,HashMap<String,HashMap<String,HashMap<Double,Double>>>> Search(@RequestBody SearchModel search){
        MainSearch mainSearch = new MainSearch(search.userinfo,search.words);
        return mainSearch.SearchInVideous();
    }
}

And this is the "SearchModel" class:

public class SearchModel {

    protected final List<String> userinfo;
    protected final ArrayList<String> words;
    
    public SearchModel(List<String> userinfo,
                       ArrayList<String> words){
        this.userinfo = userinfo;
        this.words = words;
    }
}

This is the security class:

@Configuration
@AllArgsConstructor
@EnableWebSecurity
public class ServerSecurity{

    @Bean
    public SecurityFilterChain defaultSecurityFilterChain(HttpSecurity httpSecurity) throws Exception{
        httpSecurity
                .csrf()
                .disable()
                .authorizeHttpRequests()
                .requestMatchers("/api/**","/api/Check")
                .permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .formLogin();
        httpSecurity.httpBasic();

        return httpSecurity.build();
    }

    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
        return authenticationConfiguration.getAuthenticationManager();
    }

}

Moreover, I did the first test and is working as you can see in the following image: enter image description here

In this image I used "Postman" in order to send a "Get" requests and using the second "GetMapping" from the "RequestsController" class as you can see. This requests is working perfectly.

My problem is when I try to use the First "GetMapping" from "RequestsController", as you can see in the following image: enter image description here

This is "CheckModel" class:

public class CheckModel {
    protected final String[] checking;

    public CheckModel(String[] checking){
        this.checking = checking;
    }
}

Unfortunately, the error is too log, but I will put just a part of it and if you want I can put the entire error. This is the error:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.FinalYearProjectServer.GetAndPostHandler.CheckModel` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
 at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 2, column: 5]
    at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67) ~[jackson-databind-2.14.2.jar:2.14.2]
    at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1909) ~[jackson-databind-2.14.2.jar:2.14.2]
    at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:408) ~[jackson-databind-2.14.2.jar:2.14.2]
    at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1354) ~[jackson-databind-2.14.2.jar:2.14.2]
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1420) ~[jackson-databind-2.14.2.jar:2.14.2]

I really do not know what to do in this situation.

  • 1
    the key difference is: (the working) one uses "lists", the other: `String[]`... – xerx593 Apr 21 '23 at 17:35
  • 1
    (noob tips: (still) [don't use body in get requests!](https://stackoverflow.com/q/978061/592355), [lower case urls/path are "more convenient"](https://stackoverflow.com/q/7996919/592355), static access/init is strange/smelly in "spring beans", (interface vs. impl variable ... `Map` vs `HashMap`): use "as broad as possible" (interface), "as narrow (impl) as you need"! ...;) – xerx593 Apr 21 '23 at 17:42

1 Answers1

1

The problem is that @RequestBody annotation to work properly requires default (no args) constructor. That's why to fix this error you just need to have no args constructor (implicit or explicit) in CheckModel class.

public class CheckModel {
    private String[] checking;

    public CheckModel(){} // implicit no args constructor

    public String[] getChecking() {
        return checking;
    }

    public void setChecking(String[] checking) {
        this.checking = checking;
    }
}

You can read about this problem more, here