Spring MVC 3:将Spring-Data页面作为JSON返回 - 与PagedResourcesAssembler一起出现问题

时间:2013-08-07 12:04:28

标签: json spring-mvc spring-data spring-hateoas

所以,我对spring和java一般都是新手

我尝试做的是在相同的渲染视图上使用表单发布数据以过滤表单下显示的结果列表。

我有一个简单的域类,如下所示:

@Entity
@Table(name = "SEC_PERSON")
public class Person {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_SEC_PERSON")
@SequenceGenerator(name = "SEQ_SEC_PERSON", sequenceName = "SEQ_SEC_PERSON")
@Column(name = "ID")
private Long Id;

@Column(name = "CODE", nullable = false)
private String code;

@Column(name = "FIRSTNAME", nullable = false)
private String firstname;

@Column(name = "SURNAME", nullable = false)
private String surname;

@Column(name = "CREATIONDATE")
private DateTime creationDate;

//getters and setters

一个DTO,因为我希望我的域名与我的演示文稿分离

public class PersonDTO {
private Long id;

@NotEmpty
private String code;

@NotEmpty
private String firstname;

@NotEmpty
private String surname;

private DateTime creationDate;

public PersonDTO() {
}
//getters and setters

扩展Jpa和QueryDsl

的存储库
public interface PersonRepository extends JpaRepository<Person, Long>, QueryDslPredicateExecutor<Person> {}

我的搜索的数据访问类是null安全的(感谢guava)及其等效的非null安全

人标准:

public class PersonCriteria {

private String code;
private String surname;
private String firstname;
private LocalDate creationDateFrom;
private LocalDate creationDateTo;

//getters and setters
}

空安全版

public class NullSafePersonCriteria {

private final PersonCriteria personCriteria;

public NullSafePersonCriteria(final PersonCriteria personCriteria) {
    checkArgument(personCriteria != null);
    this.personCriteria = personCriteria;
}

public Optional<String> getCode() {
    return Optional.fromNullable(this.personCriteria.getCode());
}

public Optional<String> getSurname() {
    return Optional.fromNullable(this.personCriteria.getSurname());
}

public Optional<String> getFirstname() {
    return Optional.fromNullable(this.personCriteria.getFirstname());
}

public Optional<LocalDate> getCreationDateFrom() {
    return Optional.fromNullable(this.personCriteria.getCreationDateFrom());
}

public Optional<LocalDate> getCreationDateTo() {
    return Optional.fromNullable(this.personCriteria.getCreationDateTo());
}

我的搜索谓词

public class PersonPredicates {
public static Predicate PersonLitstQuery(final PersonCriteria personCriteria) {
    final QPerson person = QPerson.person;
    final NullSafePersonCriteria nsPersonCriteria = new NullSafePersonCriteria(personCriteria);

    BooleanExpression criteria = QPerson.person.isNotNull();
    if (nsPersonCriteria.getCode().isPresent()) {
        criteria = criteria.and(person.code.matches(nsPersonCriteria.getCode().get()));
    }

    if (nsPersonCriteria.getSurname().isPresent()) {
        criteria.and(person.surname.startsWithIgnoreCase(nsPersonCriteria.getSurname().get()));
    }

    if (nsPersonCriteria.getFirstname().isPresent()) {
        criteria.and(person.firstname.startsWithIgnoreCase(nsPersonCriteria.getFirstname().get()));
    }

    if ((nsPersonCriteria.getCreationDateFrom().isPresent()) && (nsPersonCriteria.getCreationDateTo().isPresent())) {
        criteria.and(person.creationDate.between(nsPersonCriteria.getCreationDateFrom().get().toDateTimeAtStartOfDay(),
                nsPersonCriteria.getCreationDateTo().get().toDateTimeAtStartOfDay()));
    }

    return criteria;
}

我的服务实施如下:

@Service
public class PersonServiceImpl implements PersonService{
@Transactional(readOnly = true)
@Override
public Page<PersonDTO> search(final PersonCriteria criteria, final int pageIndex) {
    LOGGER.debug("Searching person with set of criterias");

    return new PersonPage(this.mapper.map(Lists.newArrayList(this.personRepository.findAll(PersonLitstQuery(criteria))),
            PersonDTO.class), constructPageSpecification(pageIndex), count(criteria));
}

我使用的映射器只是扩展了一点DozerMapper:

public class DozerMapper{
private final org.dozer.Mapper mapper;

public DozerMapper(final org.dozer.Mapper mapper) {
    this.mapper = mapper;
}

@Override
public <T> T map(final Object source, final Class<T> destinationClass) {
    return this.mapper.map(source, destinationClass);
}

@Override
public <T> List<T> map(final List<?> sources, final Class<T> destinationClass) {
    final List<T> result = Lists.newArrayList();
    for (final Object source : sources) {
        result.add(map(source, destinationClass));
    }
    return result;
}

现在,以上所有的工作,很好的是单元测试并返回我想要的结果。我的问题在于控制器和视图....

我仔细阅读了奥利弗对这个问题的回答:Spring MVC 3: return a Spring-Data Page as JSON

虽然由于某种原因我无法使其发挥作用。我已经将以下依赖项添加到我的项目中以使用HATEOAS和Spring-data-commons:

<dependency>
    <groupId>org.springframework.hateoas</groupId>
    <artifactId>spring-hateoas</artifactId>
    <version>0.7.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>1.6.0.RELEASE</version>
</dependency>

我的控制器看起来像这样:

@Controller
@SessionAttributes("person")
public class PersonController

@RequestMapping(value = REQUEST_MAPPING_LIST, method = RequestMethod.GET)
public HttpEntity<PagedResources> persons(final Model model, @ModelAttribute final PersonCriteria searchCriteria,
        final Pageable pageable, final PagedResourcesAssembler assembler) {
    model.addAttribute(MODEL_ATTRIBUTE_SEARCHCRITERIA, searchCriteria);
    final Page<PersonDTO> persons = this.personService.search(searchCriteria, searchCriteria.getPageIndex());
    return new ResponseEntity<>(assembler.toResource(persons), HttpStatus.OK);
}

和我的jsp:

<html>
<head>
    <title>testing</title>
    <script src="jslinks for jqGrid and jquery" type="text/javascript"></script>
</head>
<body>
<form:form action="person" commandName="searchCriteria" method="POST">
    <div>
        <form:label path="code">Code: </form:label>
        <form:input path="code" type="text"/>
        <form:label path="surname">Surname: </form:label>
        <form:input path="surname" type="text"/>
        <form:label path="firstname">Firstname: </form:label>
        <form:input path="firstname" type="text"/>
        <form:label path="creationDateFrom">Creation Date From: </form:label>
        <smj:datepicker id="creationDateFrom" name="CreationDateFrom" />
        <form:label path="creationDateTo">Creation Date To: </form:label>
        <smj:datepicker id="creationDateTo" name="CreationDateTo" />
    </div>
    <div>
        <input type="submit" value="search"/>
    </div>
</form:form>

    <smjg:grid 
        gridModel="gridModel" 
        id="persons" 
        datatype="\"json\""
        url="\'person\'"
        jsonReader="{root:\"content\", repeatitems: false, records: \"numberOfElements\", total: \"totalPages\"}">
        <smjg:gridColumn name="code" />
        <smjg:gridColumn name="surname" align="left"/>
        <smjg:gridColumn name="firstname" align="left"/>
    </smjg:grid>
</body>
</html>

说明: smj和smjg标签是我正在处理的taglibs,它们将jquery链接到spring mvc。例如:smjg:grid将创建标签和将调用jqgrid函数的javascript。

Olivier从这篇文章Spring MVC 3: return a Spring-Data Page as JSON回答的第一个区别是,如果我在HttpEntity中推断出PersonDTO,那么我会得到以下编译错误:

Type mismatch: cannot convert from ResponseEntity<PagedResources> to HttpEntity<PagedResources<PersonDTO>>

第二个区别是我似乎应该将我的PersonDTO推断为PagedResourcesAssembler,这是正确的吗?

当我直接调用url localhost时的结果:8081 / app / person我收到了http 500错误:

org.springframework.http.converter.HttpMessageNotWritableException: Could not marshal [PagedResource { content: [Resource { content: com.app.admin.service.PersonDTO@60a349d0[id=2050,code=TEST2,firstname=ChadsdaTest,surname=Francois,creationDate=<null>], links: [] }, Resource { content: com.app.admin.service.PersonDTO@48462da5[id=5050,code=TESTNEW,firstname=Francois,surname=asdasdx,creationDate=<null>], links: [] }, Resource { content: com.app.admin.crediadmin.service.PersonDTO@5458c9fc[id=51,code=TEST,firstname=Francois,surname=asdawdsx,creationDate=<null>], links: [] }, Resource { content: com.app.admin.service.PersonDTO@de47c70[id=2051,code=TEST3,firstname=Chaqweqasdamsh,surname=Frasda,creationDate=<null>], links: [] }, Resource { content: com.app.admin.service.PersonDTO@7bd2085d[id=3053,code=TEST7,firstname=Francois,surname=Cadsdsx,creationDate=<null>], links: [] }, Resource { content: com.app.admin.service.PersonDTO@14676697[id=3050,code=TESTER,firstname=Francois,surname=CasdadsixChaix,creationDate=<null>], links: [] }, Resource { content: com.app.admin.service.PersonDTO@109de504[id=3051,code=TESTER3,firstname=FrancoisF,surname=Chtest,creationDate=<null>], links: [] }], metadata: Metadata { number: 0, total pages: 2, total elements: 7, size: 5 }, links: [<http://localhost:8081/app/person?page=1&size=5&sort=surname,asc>;rel="next"] }]: null; nested exception is javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.SAXException2: unable to marshal type  "org.springframework.hateoas.Resource" as an element because it is not known to this context.]
org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter.writeToResult(Jaxb2RootElementHttpMessageConverter.java:99)
org.springframework.http.converter.xml.AbstractXmlHttpMessageConverter.writeInternal(AbstractXmlHttpMessageConverter.java:66)
org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:179)
org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:148)
org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:124)
org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:69)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:122)

和根本原因:

javax.xml.bind.MarshalException
- with linked exception: [com.sun.istack.SAXException2: unable to marshal type "org.springframework.hateoas.Resource" as an element because it is not known to this context.]
com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:318)
com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:244)

我不确定我在这里做错了什么。

如果我用.json调用相同的url,那么我得到的json输出看起来很奇怪,因为我还没有生成json。

1 个答案:

答案 0 :(得分:14)

你现在可能已经解决了这个问题,但是因为我有这个工作,所以我认为我会为类似船上的其他人添加至少一个问题的解决方案。

Type mismatch: cannot convert from ResponseEntity<PagedResources> to HttpEntity<PagedResources<PersonDTO>>

要解决此问题,请在返回类型中添加其他类型参数:

@RequestMapping(value = REQUEST_MAPPING_LIST, method = RequestMethod.GET)
public HttpEntity<PagedResources<PersonDTO>> persons(final Model model, @ModelAttribute final PersonCriteria searchCriteria,
        final Pageable pageable, final PagedResourcesAssembler assembler) {
    ...
}

com.sun.istack.SAXException2: unable to marshal type "org.springframework.hateoas.Resource" as an element because it is not known to this context.]

看起来Spring正在尝试生成XML,我认为它默认情况下会在类路径上找到JAXB实现。如果您不需要从此方法生成XML,则可以将produces = {MediaType.APPLICATION_JSON_VALUE}添加到其@RequestMapping。