java - There was an unexpected error (type=Not Acceptable, status=406) for Spring @RestController, spring-boot -
i'm trying return xml @restcontroller method -
@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; }
here 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; } }
this pom.xml file -
<?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 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 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>
below application 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); // core poll size of 3 threads , maximum pool size of 10 threads factory.setconcurrency("3-10"); return factory; } // using factory pattern 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); } }
when hit url /encoders in browser, below error -
there unexpected error (type=not acceptable, status=406). not find acceptable representation
the jackson related jars in calsspath -
- 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
so, have mappingjackson2httpmessageconverter in classpath.
please suggest. in advance.
there solution work out-of-the-box, jax-rs bit worse output. solution uses jackson-dataformat-xml. add dependency project:
<dependency> <groupid>com.fasterxml.jackson.dataformat</groupid> <artifactid>jackson-dataformat-xml</artifactid> </dependency>
response this:
<?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>
for further details please @ spring mvc way (jackson-dataformat-xml)
Comments
Post a Comment