我是Spring Boot的新手,但在Spring Boot REST中閱讀關於異常handlig的帖子和博客幾個小時後,沒有人編寫任何有關處理自定義Converter引發的異常的問題,決定寫在這裏。在Spring Boot中處理異常REST從自定義轉換器拋出REST
我開發了基於Spring Boot的小型REST應用程序,該應用程序只是從IntelliJ生成的。典型的方法是這樣的
@RestController
@RequestMapping("/resources")
public class CVResourceService {
private final TechnologyRepository technologyRepository;
private final ProjectRepository projectRepository;
@Autowired
public CVResourceService(TechnologyRepository technologyRepository, ProjectRepository projectRepository) {
this.technologyRepository = technologyRepository;
this.projectRepository = projectRepository;
}
@RequestMapping(value = "https://stackoverflow.com/users/{guid}/projects/langs/{lang}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Collection getUserProjects(@PathVariable("guid") GUID userGUID, @PathVariable("lang") Language language) {
return ProjectDTOAssembler.toDTOs(projectRepository.findOne(userGUID, language));
}
}
因爲無論和lang
是字符串,我想這條信息來自同一begining強類型,我創建了簡單的轉換器GUID
和Language
類型和應用類註冊吧:
public final class GUIDConverter implements Converter{
@Override
public GUID convert(String source) {
return GUID.fromString(source);
}
}
public class LanguageConverter implements Converter{
@Override
public Language convert(String source) {
Language language = Language.of(source);
if (language == null) { throw new WrongLanguagePathVariableException(); }
return language;
}
}
GUID
從工廠方法拋出異常,
...
public static GUID fromString(String string) {
String[] components = string.split("-");
if (components.length != 5)
throw new IllegalArgumentException("Invalid GUID string: " + string);
return new GUID(string);
}
...
Language
返回null,然後我拋出轉換器的自定義異常。 中註冊申請:
@SpringBootApplication
public class Application extends WebMvcConfigurerAdapter {
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new GUIDConverter());
registry.addConverter(new LanguageConverter());
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
使用所有類型的處理異常的有@ResponseStatus
,@ControllerAdvice
和@ExpectationHandler
我沒聽轉換器的例外控制器重寫(或更好的地圖)‘狀態’,‘錯誤’,「 JSON錯誤響應原始字段的異常「和」消息「。可能是因爲在調用REST方法之前引發異常。我也試過ResponseEntityExceptionHandler
的解決方案,但它根本不起作用。
對於請求http://localhost:8080/resources/users/620e643f-406f-4c69-3f4c-3f2c303f3f3f/projects/langs/end
,其中正確的語言是en
,響應與例外是:
{
"timestamp": 1458812172976,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.web.method.annotation.MethodArgumentTypeMismatchException",
"message": "Failed to convert value of type [java.lang.String] to required type [com.cybercom.cvdataapi.domain.Language]; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.PathVariable com.cybercom.cvdataapi.domain.Language] for value 'end'; nested exception is com.cybercom.cvdataapi.interfaces.rest.converter.WrongLanguagePathVariableException",
"path": "/resources/users/620e643f-406f-4c69-3f4c-3f2c303f3f3f/projects/langs/end"
}
其中自定義異常僅僅是在最後位置message
領域,但當然應與交流我的自定義消息。自定義excetion應該在exception
字段中,現在是Spring異常。這是目標,當然,不知道如何在這種情況下實現它。
請解決我的問題,即捕獲轉換器拋出的異常並將它們映射爲@ControllerAdvice
和控制器拋出的異常。 Thx提前。
請例外添加到您的問題 – msparer