spring-boot integrate with swagger

1 篇文章 0 订阅
1 篇文章 0 订阅

Add below dependency in pom.xml

<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger-ui</artifactId>
</dependency>
<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>1.16.8</version>
</dependency>

Add the @EnableSwagger2 annotation to the Application class

@SpringBootApplication
@EnableSwagger2
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Applicataion.class, args);
    }
}

Key annotations of swagger

AnnotationComments
@ApiModelModel name like ‘User Model’
@ApiModelPropertyThe property description like ‘Name’, ‘Password’
@ApiThe API description like ‘User API’
@ApiOperationThe API method description like ‘Get User By Id’
@ApiImplicitParamsWill contains list of @ApiImplicitParam
@ApiImplicitParamthe parameter definition
@ApiResponsesContains list of @ApiResponse
@ApiResponsethe response definition

Sample for domain POJO

@ApiModel("User Model")
@AllArgsConstrcutor
@Getter
public class User {
    @ApiModelProperty("User ID")
    private int id;
    @ApiModelProperty("User Name")
    private String name;
    @ApiModelProperty("Password")
    private String password;
}

Note below annotations comes from lombok dependency
(Need IDE support lombok plugin)

AnnotationComments
@AllArgsConstrcutorIt will automatically generate the constructor
@NoArgsConstrcutorIt will automatically generate the constructor with empty parameters
@GetterIt will automatically generate the getterXX() method
@SetterIt will automatically generate the setXX() method
@Builderconvert the class to builder strategy
@DataContains @NoArgsConstrcutor @Setter @Getter and will override toString(),hashcode(),equals() methods

Sample for controller

@Api("User API")
@RestController
@RequestMapping("/user")
public class UserControll {
    @ApiOperation("Get the user by id")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType="path",name="id",dataType="int",required=true,value="User ID",defaultValue="0")
    })
    @ApiResponses({
        @ApiResponse(code=403, message="Access forbidden"),
        @ApiResponse(code=404, message="Page not found")
    })
    @RequestMapping(value="/{id}", method=RequestMethod.GET)
    public User getUser(@PathVariable("id") int id) {
        return new User();
    }
}

Reference

《Java微服务实战》- 赵计刚

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot provides easy integration with the Spring Integration framework for implementing messaging and integration patterns. Spring Integration provides support for various messaging protocols, including MQTT, a lightweight publish/subscribe messaging protocol. To integrate Spring Integration with MQTT in Spring Boot, follow these steps: 1. Add the following dependencies to your `pom.xml` file: ``` <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-core</artifactId> </dependency> <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-mqtt</artifactId> </dependency> ``` 2. Create a configuration class for MQTT integration: ``` @Configuration @EnableIntegration public class MqttIntegrationConfig { @Value("${mqtt.broker.url}") private String brokerUrl; @Value("${mqtt.client.id}") private String clientId; @Bean public MessageChannel mqttInputChannel() { return new DirectChannel(); } @Bean public MqttPahoClientFactory mqttClientFactory() { DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory(); MqttConnectOptions options = new MqttConnectOptions(); options.setServerURIs(new String[] { brokerUrl }); factory.setConnectionOptions(options); return factory; } @Bean public MessageProducer inbound() { MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter( clientId, mqttClientFactory(), "topic1", "topic2"); adapter.setCompletionTimeout(5000); adapter.setConverter(new DefaultPahoMessageConverter()); adapter.setQos(1); return adapter; } @Bean public MessageHandler mqttMessageHandler() { return new MessageHandler() { @Override public void handleMessage(Message<?> message) throws MessagingException { System.out.println("Received MQTT message: " + message.getPayload()); } }; } @Bean public IntegrationFlow mqttInFlow() { return IntegrationFlows.from(inbound()) .handle(mqttMessageHandler()) .channel(mqttInputChannel()) .get(); } } ``` 3. Configure the MQTT broker URL and client ID in your application properties file: ``` mqtt.broker.url=tcp://localhost:1883 mqtt.client.id=myClientId ``` 4. Use the `mqttInputChannel` channel to send messages to the MQTT broker. For example: ``` @Autowired private MessageChannel mqttInputChannel; public void sendMqttMessage(String payload) { mqttInputChannel.send(MessageBuilder.withPayload(payload).build()); } ``` With these steps, you can easily integrate MQTT messaging with your Spring Boot application using Spring Integration.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值