如何使用转义符发送到RabbitMQ

时间:2019-05-15 07:14:32

标签: java rabbitmq

我正在像这样推动RabbitMq

@PostMapping(value = {"/push"},produces = APPLICATION_JSON_VALUE)
     public ResponseEntity<String> push(
            @RequestBody String payload,
            HttpServletResponse httpServletResponse,HttpServletRequest httpServletRequest) {
        rabbitMQPublisherService.send(payload);
        return new ResponseEntity<>(HttpStatus.OK);
    }



@Service
public class RabbitMQPublisherService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Value("${rabbitmq.queue}")
    private String queue;

    public void send(String message) {

        rabbitTemplate.convertAndSend(queue,new String(message.getBytes()));
    }
}


@Configuration
public class RabbitMQConfiguration {

    @Value("${rabbitmq.queue}")
    private String queueName;

    @Value("${rabbitmq.exchange}")
    private String exchange;

    @Value("${rabbitmq.routingkey}")
    private String routingKey;

    @Bean
    public Queue queue() {
        return new Queue(queueName, false);
    }

    @Bean
    public DirectExchange exchange() {
        return new DirectExchange(exchange);
    }

    @Bean
    public Binding binding() {
        return BindingBuilder.bind(queue()).to(exchange()).with(routingKey);
    }

    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public AmqpTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(jsonMessageConverter());
        return rabbitTemplate;
    }
}

但是每次,我推一个杰森,它用转义符推入队列,我不希望找基石耕作者。在这方面建议我

输出

  

“ {\ n \” check \“:\” 1 \“,\ n \”类型\“:\” 1212 \“ \ n}”

所需的输出

  

“ {” check“:” 1“,” type“:” 1212“}”

1 个答案:

答案 0 :(得分:0)

您所需的输出不是有效的字符串。它包含双引号,必须将其转义为\"

换行符\n只是空白,可以忽略。

输出是包含此JSON对象的字符串:

{
    "check": "1",
    "type": "1212"
}

这正是您所期望的。