在非根路由上获取响应 404
Getting response 404 on non-root routes
1.When 我尝试向“/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;
}
}
主要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 当我尝试在控制器注释和 GET 请求中将路由更改为“/”时,我得到了
"message": "Content type '' not supported"
映射器工作正常。
您的映射不正确。
在控制器上使用 @RequestMapping
注释为整个控制器设置 /families
前缀:
@RestController
@RequestMapping(value = "/families", consumes = "application/json", produces = "application/json")
public class FamilyController {
@GetMapping("/")
public String getFamily(){...}
}
或调整 getFamily
方法上的 @GetMapping
注释
@RestController
@RequestMapping(consumes = "application/json", produces = "application/json")
public class FamilyController {
@GetMapping("/families")
public String getFamily(){...}
}
或通过将配置 属性 server.servlet.context-path
设置为值 /families
来注册整个应用程序上下文路径前缀 /families
在上面的代码中,您可以从控制器级别删除@RequestMapping。
@RequestMapping(consumes = "application/json", produces = "application/json")
控制器应该是这样的。
@RestController
public class FamilyController {
要了解有关此行为的更多信息,您可以按照 baeldung
等教程进行操作
1.When 我尝试向“/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;
}
}
主要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 当我尝试在控制器注释和 GET 请求中将路由更改为“/”时,我得到了
"message": "Content type '' not supported"
映射器工作正常。
您的映射不正确。
在控制器上使用 @RequestMapping
注释为整个控制器设置 /families
前缀:
@RestController
@RequestMapping(value = "/families", consumes = "application/json", produces = "application/json")
public class FamilyController {
@GetMapping("/")
public String getFamily(){...}
}
或调整 getFamily
方法上的 @GetMapping
注释
@RestController
@RequestMapping(consumes = "application/json", produces = "application/json")
public class FamilyController {
@GetMapping("/families")
public String getFamily(){...}
}
或通过将配置 属性 server.servlet.context-path
设置为值 /families
/families
在上面的代码中,您可以从控制器级别删除@RequestMapping。
@RequestMapping(consumes = "application/json", produces = "application/json")
控制器应该是这样的。
@RestController
public class FamilyController {
要了解有关此行为的更多信息,您可以按照 baeldung
等教程进行操作