I try to produce xml-format data from spring boot restcontroller. Below is User model codes first.
@Entity
@Table(name="BlogUser")
@XmlRootElement
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="USER_ID", nullable = false, unique = true)
private Long id;
@Column(unique=true, nullable=false)
@Length(min=2, max=30)
@NotEmpty
private String username;
@Column(nullable=false)
@Length(min=5)
@NotEmpty
private String password;
@Column
@Email
@NotEmpty
private String email;
@Column
@NotEmpty
private String fullname;
@Column
private UserRole role;
}
And Below codes are RestConstroller.java
@RestController
@RequestMapping(value="/rest/user")
@SessionAttributes("user")
public class UserRestController {
@Autowired
private UserService userService;
@GetMapping(value="getAllUser", produces=MediaType.APPLICATION_XML_VALUE)
public ResponseEntity<List<User>> getAllPost() {
List<User> users = this.userService.findAll();
if(users == null || users.isEmpty())
return new ResponseEntity<List<User>>(HttpStatus.NO_CONTENT);
return new ResponseEntity<List<User>>(users, HttpStatus.OK);
}
}
}
Json format data are successfully returned. But xml-format values are not generated. It throws the following exception.
.w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]
I add the a few dependencies into pom.xml like below,
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
But still throws the same exception. I can not understand what I miss to solve this issue.
Set the consumes
attribute in your @GetMapping
annotation.
@GetMapping(value = "getAllUser", produces = MediaType.APPLICATION_XML_VALUE, consumes = MediaType.APPLICATION_XML_VALUE)