4

所以,总的来说,我对 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> {}

一个用于我的搜索的数据访问类,它是空安全的(感谢 guava)及其等效的非空安全

人选标准:

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;
}

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

我已经仔细阅读了 Oliver 对这个问题的回答: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 标签是我目前正在研究的标签库,它们将 jquery 链接到 spring mvc。例如:smjg:grid 将创建将调用 jqgrid 函数的标记和 javascript。

与这篇文章Spring MVC 3: return a Spring-Data Page as JSON的 Olivier 回答的第一个区别是,如果我在 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。

4

1 回答 1

15

您现在可能已经解决了这个问题,但是由于我有这个工作,我想我会为类似船上的其他任何人添加解决方案来解决您的至少一个问题。

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。

于 2014-01-20T16:25:18.503 回答