【发布时间】:2021-07-11 17:27:36
【问题描述】:
1.当我尝试向“/families”路由发送 GET 请求时,我的应用程序以 404 响应我。
控制器:
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;
}
}
主类:
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.但是当我尝试在控制器注释和 GET 请求中将路由更改为“/”时,我得到了
"message": "Content type '' not supported"
映射器工作正常。
【问题讨论】:
标签: java spring spring-boot spring-mvc