Goal:
I am trying to create a UML class diagram for a java spring application. Spring uses a lot of annotations and I couldn't find any resources online on how to properly model them in UML.
I know that there is this question and it gives a good answer on how to model class annotations but it does not talk about method or variable annotations.
Examples:
Example class with annotations:
@RestController
@RequestMapping("/someRoute")
public class BaseController {
@Autowired
protected BaseService service;
@GetMapping(BaseEntity.ID_MAPPING)
public ResponseEntity<BaseEntity> findById(
@PathVariable(value = BaseEntity.ID) long id
) throws ResourceNotFoundException {
BaseEntity entity = service.findById(id);
return ResponseEntity.ok().body(entity);
}
}
For the class annotations I would use this:
For method and attribute annotations I tried using this:
But as you can see this gets very long and complicated to read very fast. This also would not work if something had multiple tagged annotations.
Question:
So I would like to know if there is a correct or better way to show annotations in an UML class diagram? Or if java annotations should even be in an UML diagram at all?
1 Answer 1
It all depend on the purpose of your model:
- If it is a design model that aims to communicate the big picture, it would be advisable not to show any annotations: these would on’y make the big picture more difficult to grasp by adding unnecessary implementation details.
- if it is an implementation model that aims to be ultra-accurate, for example because it’s used to automatically generate code, the you need to find a way to convey all the required details. The stereotypes approach at class level is perfectly suitable. At operations level. the a list of operation properties between curly brackets (e.g.
{op1, op2,...}
seems the right way. But UML is really not meant to be a programming language, and if there is no code generation, in a matter of months your UML will be out of sync.
-
4There are also some uses for UML between these. Martin Fowler wrote about four UML Modes - Sketch, Notes, Blueprint, and Programming Language. First, figure out who the audience is and then what the best mode is to convey the information that they desire is.01/04/2021 00:38:00Commented Jan 4, 2021 at 0:38
-
1Thank you for your answers. I think I'm going to use my diagram to show the "big picture" :)Tim Engbrocks– Tim Engbrocks01/04/2021 08:45:09Commented Jan 4, 2021 at 8:45
-
1@ThomasOwens Thanks for the links. Indeed, there are many ways to use UML. One could also mention documentation as code as well as roundtrip engineering. I mentioned only two very common uses, just to highlight the need to think about the purpose of the diagram when it comes to what should or shouldn’t be shown in the diagram.Christophe– Christophe01/04/2021 09:33:00Commented Jan 4, 2021 at 9:33
«»
.