3

I have to create a rest api from scratch. I have already some experience with Jersey by doing mostly everything manualy.

I wanted to do it right now as this project is new. So I am currently trying the pet store sample that is available every time I am trying an openapi 3.0 online editor.

Using openapi-generator, I have generated the spring boot server for the pet store.

There are a lot of tutorial that will stop there. I don't understand where or how do I have to add my business logic code (database access, ....).

And after that I have a question, how is the specification update is done ?

2 Answers 2

2

With Spring Framework, you use Repository and Entity classes to handle data layer. And then add Service classes for business logic.

https://www.baeldung.com/spring-component-repository-service

Database (example in MySql): https://spring.io/guides/gs/accessing-data-mysql/

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I was able to use the delegate mode by setting delegatePattern to true in the openapi generator.
0

You don't need to use delegatePattern to add your business logic. You need to implement generated *Api classes and put @RestController annotation on the class declaration level. Generated *Controller classes are just stubs for "default" behavior. If you implement your own controller, it will be prioritized during the autowiring.

Generated stub-controller:

@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", comments = "Generator version: 7.8.0")
@RestController
@RequestMapping("${openapi.swaggerPetstore.base-path:/v1}")
public class PetsApiController implements PetsApi {

}

Your controller implementation:

@RestController
@RequestMapping("${openapi.swaggerPetstore.base-path:/v1}")
class MyPetsApiController implements PetsApi {
    
    private final PetsService petsService;
    
    @Override
    public Flux<Pet> listPets(Integer limit, ServerWebExchange exchange) {
        return petsService.listPets(limit)
    }
     
    ...
}

If you get the following error:

Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'petsApiController' for bean class [com.example.PetsApiController] conflicts with existing, non-compatible bean definition of same name and class [com.example.PetsApiController]

...you have various options here. I'll list two of them:

  1. You can exclude generated stub from the component scan:

    @SpringBootApplication
    @ComponentScan(
            basePackages = "com.example",
            excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = PetsApiController.class)
    )
    public class Application
    
  2. You can skip generation of the stub controller. Set interfaceOnly to false: https://openapi-generator.tech/docs/generators/spring/. Now openapi-generator won't create any @Controller classes; you can make it yourself.

I would suggest to use the second option.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.