Java Spring重新模板异常处理错误描述null-结果为500 null

Java Spring重新模板异常处理错误描述null-结果为500 null,java,spring,spring-boot,spring-rest,Java,Spring,Spring Boot,Spring Rest,我目前有一个SpringBoot应用程序(版本-2.1.8.RELEASE),它公开了RESTWebService,提供一些基本的电子邮件功能。我已经向它添加了swagger组件,可以通过swagger UI界面测试Web服务。例如,当我通过swagger UI测试Web服务时,我没有输入诸如mailfrom之类的必填字段,我收到一条错误消息,指出emailfrom缺少状态码,这正是我所期望的 但是,当使用SpringMVC项目(web服务客户端-SpringVersion3.2.5.RELEA

我目前有一个SpringBoot应用程序(版本-2.1.8.RELEASE),它公开了RESTWebService,提供一些基本的电子邮件功能。我已经向它添加了swagger组件,可以通过swagger UI界面测试Web服务。例如,当我通过swagger UI测试Web服务时,我没有输入诸如mailfrom之类的必填字段,我收到一条错误消息,指出emailfrom缺少状态码,这正是我所期望的

但是,当使用SpringMVC项目(web服务客户端-SpringVersion3.2.5.RELEASE)执行相同的场景时,不会显示错误描述,因为我希望在使用上面的swagger Ui进行测试时会有相同的行为。在下面执行集成测试时,请参见屏幕截图结果,结果为500 null

请在下面找到spring boot应用程序rest控制器:

@RestController
@RequestMapping("/email")
public class EmailController {

    private static final Logger LOG = LoggerFactory.getLogger(EmailController.class);

    @Autowired
    SendMailService sendMailService;


    @ApiOperation(value = "This service send mail based on the information provided from EmailRequestDto", response = String.class)
    @PostMapping(value = "/sendMail")
    public @ResponseBody ResponseEntity<String> sendMail(@RequestBody EmailRequestDto emailRequestDto) {
        LOG.debug("calling method sendMail");

            sendMailService.sendEmail(emailRequestDto);

        return new ResponseEntity<>("Mail has been sent successfully", HttpStatus.OK);
    }
请在下面找到我从客户端项目调用web服务的集成测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:dummy-service-test-context.xml" })
public class SendMailServiceTest {

    @Test
    public void testService() {


        final String uri = "http://localhost:8080/email-service/email/sendMail";

        EmailRequestDto emailRequestDto = new EmailRequestDto();
//      emailRequestDto.setMailTo((Arrays.asList("dummy@gmail.com")));
//      emailRequestDto.setEmailContent("Dear Sir");
//      emailRequestDto.setMailFrom("dummy_38@hotmail.com");
//      emailRequestDto.setSubject("Sending Email subject");

        emailRequestDto.setMailTo(null);
        emailRequestDto.setEmailContent(null);
        emailRequestDto.setMailFrom(null);
        emailRequestDto.setSubject(null);

        HttpEntity<EmailRequestDto> request = new HttpEntity<>(emailRequestDto);

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> es = restTemplate.exchange(uri, HttpMethod.POST, request,String.class);

//      ResponseEntity<?> response = restTemplate.postForEntity(uri, request, ResponseEntity.class, new HashMap());

        Assert.assertTrue(es.getStatusCode().equals(HttpStatus.OK));


    }

}
但在我的客户机集成测试中,它是:

org.springframework.web.client.HttpClientErrorException: 400 null

我不认为您运行测试类时服务器正在运行,可能想用@SpringBootTest(classes={Application.class})注释您的测试类,另外我想用Mockito进行集成测试,然后启动服务器并调用restTemplateHi感谢您的回复,但服务器正在运行,因为我可以通过swagger UIS访问rest web服务,您确定吗?我是说在运行测试用例之后?右键单击,作为Junit测试运行?你的服务器正在运行吗?哦,对不起,我以为你在谈论spring boot应用程序,不,你是对的,客户端应用程序没有运行我不认为你的服务器正在运行,当你运行测试类时,可能想用@SpringBootTest(classes={application.class})注释你的测试类另外,我希望使用Mockito进行集成测试,然后启动服务器并使用restTemplateHi调用。谢谢您的回复,但服务器正在运行,因为我可以通过swagger UIS访问rest web服务。您确定吗?我是说在运行测试用例之后?右键单击,作为Junit测试运行?你的服务器正在运行吗?哦,对不起,我以为你在说spring boot应用程序,不,你说得对,客户端应用程序没有运行
@Getter
@Setter
@ApiModel
public class EmailRequestDto implements Serializable {

    private static final long serialVersionUID = 1L;
    private List<String> mailTo;
@ApiModelProperty(value = "Email address who is sending the mail", required = true)
@NonNull
private String mailFrom;
@ApiModelProperty(value = "Subject of the  mail", required = true)
@NonNull
private String subject;
@ApiModelProperty(value = "The content of the mail", required = true)
@NonNull
private String emailContent;
private List<String> bcc;
@ApiModelProperty(value = "This attribute identify if the mail content should be sent as html or plain text", required = true)
@NonNull
boolean fileHtml;
nested exception is com.fasterxml.jackson.databind.JsonMappingException: mailFrom is marked non-null but is null
org.springframework.web.client.HttpClientErrorException: 400 null