I will keep a memorandum of what I learned when I first touched Spring Boot from November to December last year.
For those who want to touch Spring Boot from now on. I think that it will be known to those who have used it.
A simple external public API that just returns information based on the received input value.
name of the class | Overview |
---|---|
RestController class | A class that implements the interface part of the API. |
Form class | A class that holds information about the content of requests from clients and the response returned. |
RestControllerAdvice class | RestController A class that implements common processing. |
Service class | A class that implements business logic. |
Repository class | Class for DB access. |
Entity class | A class that holds the data acquired from the DB and the data to be input to the DB. |
Add @SpringBootApplication
.
Annotation | Overview |
---|---|
@EnableAutoConfiguration |
Spring Boot automatic setting is turned on, and various settings are automatically made according to the defined dependencies. |
@ComponentScan |
A component scan is performed and in the package to be scanned@Component The class with is automatically loaded and managed by the DI container. The components scanned are@Autowired Can be set to a variable by. * Refer to the sample code below. |
@Configuration |
By assigning it, beans can be registered individually and setting classes can be read. |
Various classes are read and set by @ComponentScan
and @EnableAutoConfiguration
, and the application is executed.
@ComponentScan
and packages in the hierarchy below it will be scanned, so classes above the class with @ComponentScan
will be scanned. Not loaded.SampleApplication.java
@SpringBootApplication
public class SampleApplication {
public static void main (final String[] args) {
SpringApplication.run (SampleApplication .class, args);
}
@Override
protected SpringApplicationBuilder configure (final SpringApplicationBuilder builder) {
return builder.sources (SampleApplication .class);
}
}
@RestController
annotation. Target of @ComponentScan
.SampleController.java
/**
*Sample controller.
*/
@RestController
@Validated
public class SampleController {
@Autowired
private SampleService service; //Associate the Service class to be used.
@PostMapping (path = "/sample", //Specify the path to map to the method.
consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, //Specify the Media Type of the request to be received.
produces = MediaType.APPLICATION_JSON_UTF8_VALUE) //Specify the Media Type of the response to be returned.
@CrossOrigin
public ResponseEntity<SampleForm> sampleMethod (
@RequestHeader (value = "sample_key") //Define the key value of the value received from the header.
@NotEmpty (message = "sample_key is empty.") //If you want to scrutinize without using Form, define the scrutiny content here.
@Valid String sampleKey, //Define a variable to store the value received from the header.
@RequestBody
@Valid SampleForm form //Define the Form class that stores the value received from the body and the variable that stores it.
) {
return new ResponseEntity<> (service.sampleMethod (form.getSampleUserId), HttpStatus.OK);
}
}
SampleForm.java
@Data
@ToString
public class SampleForm implements Serializable {
private static final long serialVersionUID = 1L;
@JsonProperty ("sample_user_id") //Define the key value on JSON.
@NotEmpty (message = "sampleUserId is empty") //Define the content of the scrutiny.
private String sampleUserId;
}
@RestControllerAdvice
. Target of @ComponentScan
.@ExceptionHandler
, error handling will be done automatically when the specified exception is thrown.SampleExceptionHandler.java
@RestControllerAdvice
public class SampleExceptionHandler {
@ExceptionHandler (Exception.class) //Define the exception to catch.
@ResponseStatus (HttpStatus.INTERNAL_SERVER_ERROR) //Define the HTTP status code of the response returned when the above exception occurs.
protected ErrorForm handleInternalServerError (Exception e) {
logger.error (e);
return new ErrorForm ("Internal Server Error");
}
@RequiredArgsConstructor
private class ErrorForm implements Serializable {
private static final long serialVersionUID = 1L;
private final String message;
}
}
@Service
. Target of @ComponentScan
.SampleService.java
@Service
@Transactional //Automatically roll back when an exception occurs.
public class SampleService {
@Autowired
private SampleUserRepository sampleUserRepository; //Associate the Repository class to be used.
public SampleForm returnSampleForm (String sampleUserId) {
return convertToSampleForm(sampleUserRepository.findBySampleUserId(sampleUserId));
}
}
@Repository
. Target of @ComponentScan
.SampleUserRepository.java
@Repository
public interface SampleUserRepository extends JpaRepository<SampleUser, String> { //Define the access destination table and primary key type.
SampleUser findBySampleUserId (String sampleUserId); //Define method names and arguments according to the naming convention.
}
@Entity
.SampleUser.java
@Entity
@Table (name = "sample_user") //Define the table name to be linked.
@Data
public class SampleUser implements Serializable {
private static final long serialVersionUID = 1L;
/**Sample user ID*/
@Id //Give to the item of the primary key.
@GeneratedValue (strategy = GenerationType.IDENTITY) //If you want automatic numbering, define the numbering method.
private String sampleUserId;
/**Sample username*/
private String sampleUserName;
}
Recommended Posts