添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

"org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation" to produce xml response

Ask Question

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 {
  @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.

also try setting the consumes=MediaType.APPLICATION_XML_VALUE in your controller endpoint. – k9yosh Mar 27, 2019 at 4:17 Sorry, I missed it since it is marked as an @Entity. Usually, I don't mix up Entities with DTOs. Try generating getters and setters in your User class. – k9yosh Mar 27, 2019 at 4:23 Thank you for reply. I use lombok to generate getter/setter method. But the same exception is thrown. – Joseph Hwang Mar 27, 2019 at 4:36

I modify the method like below:

@GetMapping(value="getAllUser", produces = { "application/xml", "text/xml" }, consumes = MediaType.ALL_VALUE)
    public ResponseEntity<List<User>> getAllPost() {

It work perfectly. It return xml-type values.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.