带有List的嵌套元素在Spring中具有ResponseEntity为null

时间:2016-05-25 19:46:56

标签: spring-restcontroller spring-rest

我使用Spring引导1.3.5和Java 8有一个REST控制器。这在使用高级REST客户端( ARC )实用程序进行测试时非常有效 - 发送xml请求并返回预期的xml响应如此控制器代码右下方所示。

@RestController
@EnableAutoConfiguration
@RequestMapping("/rateQuote")
public class RateQuoteController {

     @Autowired
     private RateQuotingService rateQuotingService;           

     @RequestMapping(method = RequestMethod.POST, consumes = {"application/json","application/xml"}, produces = {"application/json","application/xml"})
     public ResponseEntity<RateQuoteDto> getRateQuote(@RequestBody RESTRequestDto request){                              
            RateQuoteDto rateQuoteDto = rateQuotingService.getRateQuote(request.getStateCode(), request.getZipCode(), request.getClient(), request.getThreshold(),
                    benefit,request.getLob(), localEffectiveDate);
            return (new ResponseEntity<>(rateQuoteDto, HttpStatus.OK));
     }    
}

请求 ARC -

url - http://localhost:8080/rateQuote

方法 - POST

标题 -

Content-Type: application/xml   
Accept: application/xml

请求正文 -

<request>
    <stateCode>NY</stateCode>
    <zipCode>10005</zipCode>
    <client>BMG</client>
    <threshold>1000</threshold>
    <lob>PDI</lob>
    <benefitLevel>15000</benefitLevel>
    <effectiveDate>2016-01-01</effectiveDate>
</request

回应 -

<?xml version="1.0" encoding="UTF-8"?>
<rateQuote>
   <status>Approve</status>
   <message>Quote is successfully retrieved.</message>
   <quote>
      <threshold1 amount="1000">
         <benefits>
            <benefitLevel amount="15000">
               <annualPremium>990.00</annualPremium>
               <semiAnnualPremium>440.00</semiAnnualPremium>
               <quaterlyPremium>200.00</quaterlyPremium>
               <monthlyPremium>77.00</monthlyPremium>
            </benefitLevel>
         </benefits>
      </threshold1>
   </quote>
</rateQuote>

到目前为止一切顺利。但是,当用语法进行调用时,我遇到了响应问题。问题是响应的大多数节点都是null,如下所示:

    @Test
    public void getQuote()throws Exception{
        RestTemplate template = new RestTemplate();
        //RESTRequestDto   is JAXB decorated
        RESTRequestDto  body = new RESTRequestDto();
        body.setStateCode("NY");
        body.setZipCode("10005");
        body.setClient("BMG");
        body.setThreshold("1000");
        body.setLob("PDI");
        body.setBenefitLevel("15000");
        body.setEffectiveDate(DateUtils.createDate("2016-01-01", "yyyy-MM-dd"));


        RequestEntity<RESTRequestDto> request = RequestEntity.post(new URI("http://localhost:8080/rateQuote"))
                .accept(MediaType.APPLICATION_XML).contentType(MediaType.APPLICATION_XML).body(body);
        ResponseEntity<RateQuoteDto> response = template.exchange(request, RateQuoteDto.class);
        RateQuoteDto rateQuote = response.getBody();

        System.out.println(rateQuote);
    }

回复 -

RateQuoteDto{status=Approve, message=Quote is successfully retrieved., zipCode=null, clientId=null, quote=QuoteDto{threshold1=ThresholdDto{amount=1000, benefitLevelLst=null}}}

但是,将媒体类型更改为JSON的工作方式如下所示:

   RequestEntity<RESTRequestDto> request = RequestEntity.post(new URI("http://localhost:8080/rateQuote"))
            .accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON).body(body);

回复 -

RateQuoteDto{status=Approve, message=Quote is successfully retrieved., zipCode=10005, clientId=BMG, quote=QuoteDto{threshold1=ThresholdDto{amount=1000, benefitLevelLst=[BenefitDto{amount=15000, annualPremium=990.00, semiAnnualPremium=440.00, quaterlyPremium=200.00, monthlyPremium=77.00}]}}}

观察 - 如下所示,ist benefitLevelLst不与 MediaType.XML 绑定,返回上面显示的空值,但与 MediaType.JSON 一起正常工作。知道为什么?

public class ThresholdDto {
    @XmlAttribute
    private String amount;

    @XmlElementWrapper(name = "benefits")
    @XmlElement(name = "benefitLevel")
    private List<BenefitDto> benefitLevelLst;

    . . .
}

0 个答案:

没有答案