7

I m actually developping a little application to train, and I m facing a problem using Spring Hateoas using PathVariable.

In fact, when I use something like :

   @RequestMapping(value = "/directories/{idDirectory}", method = RequestMethod.GET)
    public DirectoryDTO findById(@PathVariable String idDirectory) {
        DirectoryEntity directoryEntity = directoryService.findById(idDirectory);
        DirectoryDTO directoryDto = new DirectoryDTO(directoryEntity);
        directoryDto.add(linkTo(methodOn(DirectoryController.class).findById(idDirectory)).withSelfRel());
        return directoryDto;
    }

I've got an error like the following one :

[com.sun.istack.internal.SAXException2]: unable to marshal type DirectoryDTO to element because it's missing an annotation

Here my DirectoryEntity :

@Document(collection = "directory")
public class DirectoryEntity {

    @Id
    private String id;
    private String name;
    private String path;
    private List<DirectoryEntity> childrenDirectories;
    private DirectoryEntity parentDirectory;
    private List<FileEntity> fileEntities;

/* Get/set omitted */
}

And the DTO :

public class DirectoryDTO extends Resource<DirectoryEntity> {


    public DirectoryDTO(DirectoryEntity content, Link... links) {
        super(content, links);
    }

    public DirectoryDTO(DirectoryEntity content, Iterable<Link> links) {
        super(content, links);
    }
}

What am I doing wrong ?

mfrachet
  • 8,772
  • 17
  • 55
  • 110
  • Is the a `RestController`? Otherwise, you probably need to add `@ResponseBody` to your method declaration. – woemler Jun 09 '15 at 13:58
  • it's a RestController, sorry I didn't tell it – mfrachet Jun 09 '15 at 13:58
  • Try to add @XmlRootElement annotation to your DirectoryDTO class. – Thomas Weglinski Jun 13 '15 at 14:29
  • Not working. I got : "no default constructor", but Hateoas Resource needs constructors with parameters – mfrachet Jun 15 '15 at 04:44
  • yes it will get your constructors but try add the default one , cause its being used in order to wrap the object and then populate it with the setters / getters methods – AntJavaDev Jun 16 '15 at 14:23
  • The default one in rejected due to the implementation of Resource – mfrachet Jun 18 '15 at 06:31
  • follow these links: http://stackoverflow.com/questions/26166751/problems-with-jaxb-marshal-unable-to-marshal-type-java-lang-string and http://stackoverflow.com/questions/23480517/spring-hateoas-w-spring-boot-jaxb-marshal-error-when-returning-a-resourcest – Rudra21 Jun 19 '15 at 09:25

1 Answers1

0

You have to add the @XmlRootElement(name = "directoryEntity") 's annotation in DirectoryDTO.

mana
  • 6,347
  • 6
  • 50
  • 70
Ansemo Abadía
  • 488
  • 3
  • 10
  • I tried this and I got : "This page contains the following errors: error on line 2 at column 93: Entity 'eacute' not defined Below is a rendering of the page up to the first error." – mfrachet Jun 17 '15 at 12:22
  • Your original probolem has changed. Maybe, you have an error in your frontend, It's expects the "eacute" entity – Ansemo Abadía Jun 17 '15 at 13:52
  • I dont have a front end, I m using RestController without any page rendering. Only direct output from the controller – mfrachet Jun 17 '15 at 13:53
  • You have to replace &*acute for numertical entity representation in you model. See this link: http://stackoverflow.com/questions/4656121/oacute-not-allowed-in-xml-file-but-allowed-in-net-resource-file – Ansemo Abadía Jun 17 '15 at 14:03
  • The problem is that I think Spring (with its dependencies) is not able (using hateoas module) to parse the { char in @RequestMapping(value = "/directories/{idDirectory} – mfrachet Jun 17 '15 at 14:05