Покрытие java POJO в JSON и наоборот выдает ошибку как неподдерживаемую полезную нагрузку на ActiveMQ с использованием Jackson - PullRequest
1 голос
/ 04 марта 2020

Мое приложение принимает некоторые данные через пользовательский интерфейс и помещает их в ActiveMQ. Другая служба прослушивает сообщение в очереди сообщений.

Я получаю сообщение об ошибке:

Cannot convert object of type [com.ibm.www.pojos.BookOrder] to JMS message. Supported message payloads are: String, byte array, Map<String,?>, Serializable object.

Я использую конвертер сообщений Джексона для преобразования POJO в JSON.

BookOrder. java

public class BookOrder {
    @JsonCreator
    public BookOrder(@JsonProperty("bookOrderId") String bookOrderId, @JsonProperty("book") Book book, @JsonProperty("customer")Customer customer) {
        this.bookOrderId = bookOrderId;
        this.book = book;
        this.customer = customer;
    }

    private final String bookOrderId;
    private final Book book;
    private final Customer customer;

    public String getBookOrderId() {
        return bookOrderId;
    }

    public Book getBook() {
        return book;
    }

    public Customer getCustomer() {
        return customer;
    }
}

JMSConfig. java

@EnableJms
@Configuration
public class JmsConfig {
    @Bean
    public MessageConverter jacksonJmsMessageConverter(){
        MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter(); //JSON type converter
        converter.setTargetType(MessageType.TEXT);
        converter.setTypeIdPropertyName("_type"); //unique identifier between multiple APIs
        return converter;
    }

    @Bean
    public ActiveMQConnectionFactory connectionFactory(){
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("admin", "admin", "tcp://localhost:61616");  //localhost:8161
        return factory;
    }

    @Bean
    public DefaultJmsListenerContainerFactory jmsListenerContainerFactory(){
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory());
        factory.setMessageConverter(jacksonJmsMessageConverter());
        return factory;
    }

}

AppController. java

@Controller
public class AppController {
    @Autowired
    private BookOrderService bookOrderService;

    List<Book> books = Arrays.asList(
            new Book("jpw-1234", "Lord of the Flies"),
            new Book("uyh-2345", "Being and Nothingness"),
            new Book("iuhj-87654","At Sea and Lost"));


    List<Customer> customers = Arrays.asList(
            new Customer("mr-1234", "Ram Kumar "),
            new Customer("jp-2345", "Laxman Singh"),
            new Customer("sm-8765", "Bharat Pandey")
    );

    @RequestMapping("/")
    public String appHome(ModelMap map){
        map.addAttribute("customers", customers);
        map.addAttribute("books", books);
        return "index";
    }

    @RequestMapping(path = "/process/order/{orderId}/{customerId}/{bookId}/", method = RequestMethod.GET)
    public @ResponseBody String processOrder(@PathVariable("orderId") String orderId,
                                             @PathVariable("customerId") String customerId,
                                             @PathVariable("bookId") String bookId )throws JsonMappingException, JsonParseException, IOException {

        try {
            bookOrderService.send(build(customerId, bookId, orderId));

        } catch (Exception exception) {
            return "Error occurred!" + exception.getLocalizedMessage();
        }

        return "Order sent to warehouse for bookId = " + bookId + " from customerId = " + customerId + " successfully processed!";
    }

    private BookOrder build(String customerId, String bookId, String orderId){
        Book myBook = null;
        Customer myCustomer = null;

        for(Book bk : books){
            if(bk.getBookId().equalsIgnoreCase(bookId)){
                myBook = bk;
            }
        }

        if(null == myBook){
            throw new IllegalArgumentException("Book selected does not exist in inventory!");
        }

        for(Customer ct : customers){
            if(ct.getCustomerId().equalsIgnoreCase(customerId)){
                myCustomer = ct;
            }
        }

        if(null == myCustomer){
            throw new IllegalArgumentException("Customer selected does not appear to be valid!");
        }

        return new BookOrder(orderId, myBook, myCustomer);
    }
}

пом. 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.mq.www</groupId>
    <artifactId>JMSMQ</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.6.RELEASE</version>
        <relativePath></relativePath>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>8</java.version>
    </properties>


    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.1.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.0.0.RELEASE</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.thoughtworks.xstream</groupId>
            <artifactId>xstream</artifactId>
            <version>1.4.7</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>5.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>org.eclipse.jdt.core.compiler</groupId>
            <artifactId>ecj</artifactId>
            <version>4.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>4.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-broker</artifactId>
            <version>5.15.11</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <version>2.2.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-juli</artifactId>
            <version>9.0.30</version>
        </dependency>

        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-util</artifactId>
            <version>7.0.47</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.9.8</version>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.3.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-jasper</artifactId>
                <version>9.0.30</version>
            </plugin>
        </plugins>
    </build>
</project>

Ответы [ 2 ]

0 голосов
/ 04 марта 2020

Просто добавьте Serializable

public class BookOrder implements Serializable { ... }
0 голосов
/ 04 марта 2020

Полагаю, вам нужно, чтобы ваш BookOrder реализовал интерфейс Serializable. Что-то вроде:

public class BookOrder implements Serializable {
   private static final long serialVersionUID = 1L;
   ...
}
...