简体   繁体   中英

'com.sun.istack.internal.SAXException2' when doing “Building REST services with Spring” tutorial

I'm pretty new with Spring framework. On the official tutorial , when i replace

@GetMapping("/employees")
List<Employee> all() {
    return repository.findAll();
}

by the following in my controller:

@GetMapping("/employees")
Resources<Resource<Employee>> all() {

    List<Resource<Employee>> employees = repository.findAll().stream()
            .map(employee -> new Resource<>(employee,
                    linkTo(methodOn(EmployeeController.class).one(employee.getId())).withSelfRel(),
                    linkTo(methodOn(EmployeeController.class).all()).withRel("employees")))
            .collect(Collectors.toList());

    return new Resources<>(employees,
            linkTo(methodOn(EmployeeController.class).all()).withSelfRel());
}

I have this error when i try to connect to http://localhost:8080/employees/ :

com.sun.istack.internal.SAXException2 (click)

Can you help me please? :)

Probably you have some strange accented characters that you are trying to parse. Try to add

spring.datasource.url=[your_db_connection_string]?useUnicode=yes&characterEncoding=UTF-8

to your database connection string.

Try to use this:

spring.datasource.connectionProperties=useUnicode=true;characterEncoding=utf-8;

for embedded h2 database.

我通过在@GetMapping批注中添加produces = "application/json; charset=UTF-8"@GetMapping此问题,如下所示:

@GetMapping(value = "/employees", produces = "application/json; charset=UTF-8")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM