Spring @RestController,spring-boot 出现意外错误(类型=不可接受,状态=406)

There was an unexpected error (type=Not Acceptable, status=406) for Spring @RestController, spring-boot

我正在尝试从我的@RestController 方法 return xml -

@RestController
public class MCSController {
.
.
@RequestMapping(value = "/encoders", method = { RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON,
        MediaType.APPLICATION_XML })
public List<EncoderVO> getEncoders() {

    List<EncoderVO> encoders = null;
    try {
        encoders = infoService.listEncoders();
    } catch (MCSException e) {
        logger.error("Error in listing encoders : " + e.getMessage());
    }
    return encoders;
}

这是我的 EncoderVO.java -

@XmlRootElement
public class EncoderVO {

@XmlElement
private Long id;

@XmlElement
private String name;

@XmlElement
private Boolean flagActive;

public EncoderVO() {
}

public EncoderVO(Long id, String name, Boolean flagActive) {
    super();
    this.id = id;
    this.name = name;
    this.flagActive = flagActive;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Boolean getFlagActive() {
    return flagActive;
}

public void setFlagActive(Boolean flagActive) {
    this.flagActive = flagActive;
}

}

这是我的 pom.xml 文件 -

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

<groupId>com.test</groupId>
<artifactId>mcs</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<!-- Inherit defaults from Spring Boot -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.0.RELEASE</version>
</parent>

<properties>
    <aws.sdk-version>1.9.1</aws.sdk-version>
    <liquibase.version>3.3.0</liquibase.version>
</properties>

<!-- Add typical dependencies for a web application -->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <artifactId>jackson-annotations</artifactId>
                <groupId>com.fasterxml.jackson.core</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
        <exclusions>
            <exclusion>
                <artifactId>jackson-annotations</artifactId>
                <groupId>com.fasterxml.jackson.core</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jms</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-broker</artifactId>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- Aws SDK Dependencies -->
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-s3</artifactId>
        <version>${aws.sdk-version}</version>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-elastictranscoder</artifactId>
        <version>${aws.sdk-version}</version>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-sqs</artifactId>
        <version>${aws.sdk-version}</version>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-sns</artifactId>
        <version>${aws.sdk-version}</version>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-datapipeline</artifactId>
        <version>${aws.sdk-version}</version>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>


        <artifactId>aws-java-sdk-cloudfront</artifactId>
            <version>${aws.sdk-version}</version>
        </dependency>

    </dependencies>

</project>

下面是我的申请 class -

@SpringBootApplication(exclude = { SpringBootWebSecurityConfiguration.class, LiquibaseAutoConfiguration.class })
@ComponentScan(basePackages = { "com.test.ott.mcs" })
@EnableAspectJAutoProxy
@EnableJms
@EntityScan("com.test.ott.mcs.entities")
@EnableJpaRepositories("com.test.ott.mcs.repository")
    public class MCSApplication {

    @Bean
    JmsListenerContainerFactory<?> jmsContainerFactory(ConnectionFactory connectionFactory) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        // A core poll size of 3 threads and a maximum pool size of 10 threads
        factory.setConcurrency("3-10");
        return factory;
    }

    // Using factory pattern with spring annotation
    @Bean
    public FactoryBean serviceLocatorFactoryBean() {
        ServiceLocatorFactoryBean factoryBean = new ServiceLocatorFactoryBean();
        factoryBean.setServiceLocatorInterface(EncodingAPIFactory.class);
        return factoryBean;
    }

    public static void main(String args[]) {
        SpringApplication.run(MCSApplication.class, args);
    }

}

当我在浏览器中点击 url /encoders 时,出现以下错误 -

There was an unexpected error (type=Not Acceptable, status=406). Could not find acceptable representation

我的 calsspath 中与 jackson 相关的 jar 是 -

所以,我的 class 路径中有 MappingJackson2HttpMessageConverter

求推荐。提前致谢。

尝试

http://localhost:8080/mcs-0.0.1-SNAPSHOT/encoders.

mcs 是应用程序的名称, 0.0.1-SNAPSHOT 版本。

如果它不起作用,您必须将全局路径添加到 RequestController 中。类似于:

@RestController
@RequestMapping(value = "/mcs")
public class MCSController {
.
.
@RequestMapping(value = "/encoders", method = { RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON,
        MediaType.APPLICATION_XML })
public List<EncoderVO> getEncoders() {

和这个请求:

http://localhost:8080/mcs-0.0.1-SNAPSHOT/mcs/encoders

希望对你有所帮助

干杯

有一个开箱即用的解决方案,类似于 JAX-RS,但输出稍差。解决方案使用jackson-dataformat-xml。将依赖项添加到您的项目:

    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-xml</artifactId>
    </dependency>

响应将如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<List>
   <item>
      <id>1</id>
      <name>testEncoder1</name>
      <flagActive>true</flagActive>
   </item>
   <item>
      <id>2</id>
      <name>testEncoder2</name>
      <flagActive>true</flagActive>
   </item>
   <item>
      <id>3</id>
      <name>testEncoder3</name>
      <flagActive>true</flagActive>
   </item>
</List>

更多详情请查看Spring MVC Way (jackson-dataformat-xml)

在你里面添加这个依赖POM.xml

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.8</version>

问题出在这个罐子的版本上。所以请试试这个解决方案