Issue
1.When I try to send GET request to "/families" route my application responses me with 404.
Controller:
package com.example.controller;
import com.example.domain.*;
import com.example.repository.*;
import com.example.view.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
import java.util.*;
@RestController
@RequestMapping(consumes = "application/json", produces = "application/json")
public class FamilyController {
private FamilyRepository familyRepository;
public FamilyController(FamilyRepository familyRepo) {
this.familyRepository = familyRepo;
}
@GetMapping("/")
public String getFamily(){
List<Family> families = familyRepository.findAll();
ObjectMapper mapper = new ObjectMapper();
mapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);
String result = "";
try {
result = mapper
.writerWithView(FamiliesView.class)
.writeValueAsString(families);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return result;
}
}
Main class:
package com.example;
import com.example.controller.*;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.context.annotation.*;
@SpringBootApplication
public class ConstantaServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConstantaServerApplication.class, args);
}
}
2.But when I try to change route to "/" both in Controller annotation and GET request I'm getting
"message": "Content type '' not supported"
Mapper work properly.
Solution
You have incorrect mapping.
Either set /families
prefix for whole controller using @RequestMapping
annotation on controller:
@RestController
@RequestMapping(value = "/families", consumes = "application/json", produces = "application/json")
public class FamilyController {
@GetMapping("/")
public String getFamily(){...}
}
or adjust @GetMapping
annotation on your getFamily
method
@RestController
@RequestMapping(consumes = "application/json", produces = "application/json")
public class FamilyController {
@GetMapping("/families")
public String getFamily(){...}
}
or register for whole application context path prefix /families
by setting config property server.servlet.context-path
to value /families
Answered By - Martin Dendis