Spring @RestController出现意外错误(type = Not Acceptable,status = 406),spring-boot

时间:2016-04-19 06:53:53

标签: java spring-boot jackson spring-restcontroller spring-rest

我正在尝试从@RestController方法返回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>

以下是我的应用类 -

@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时,我收到以下错误 -

  

出现意外错误(type = Not Acceptable,status = 406)。可以   找不到可接受的代表

我的小路上的杰克逊相关的罐子是 -

  • jackson-core-asl:1.9.13
  • jackson-jaxrs:1.9.13
  • jackson-mapper-asl:1.9.13
  • jackson-annotations:2.6.3
  • jackson-core:2.6.3
  • jackson-module-jaxb-annotations:2.2.3
  • jackson-jaxrs-json-provider:2.2.3
  • jackson-jaxrs-base:2.5.4
  • jackson-databind:2.6.3

所以,我的类路径中有 MappingJackson2HttpMessageConverter

请建议。提前谢谢。

3 个答案:

答案 0 :(得分:3)

有一种解决方案可以开箱即用,类似于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)

答案 1 :(得分:0)

尝试

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

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

如果它不起作用,则必须在RequestController中添加全局路径。类似的东西:

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

和此请求:

Runtime Error Error: Cannot find module 'ErrorUtils' from 'env.js'

我希望它可以帮到你。

干杯

答案 2 :(得分:0)

在您的POM.xml

中添加此依赖项
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.8</version>

问题是这个jar的版本。所以请试试这个解决方案