JAVA-11765: Dissolve spring-boot-rest-2 and distribute its articles (#12195)
* JAVA-11765: Dissolve spring-boot-rest-2 and distribute its articles spring-boot-modules * JAVA-11765: removed module from main pom
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.endpoint;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringBootRestApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringBootRestApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.endpoint.controller;
|
||||
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class HelloController {
|
||||
|
||||
@GetMapping("/hello")
|
||||
public ResponseEntity<String> hello() {
|
||||
return ResponseEntity.ok("hello baeldung");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.endpoint.listener;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Configuration
|
||||
public class AnnotationDrivenEndpointsListener {
|
||||
private final Logger LOGGER = LoggerFactory.getLogger("AnnotationDrivenEndpointsListener.class");
|
||||
|
||||
@EventListener
|
||||
public void handleContextRefresh(ContextRefreshedEvent event) {
|
||||
ApplicationContext applicationContext = event.getApplicationContext();
|
||||
RequestMappingHandlerMapping requestMappingHandlerMapping = applicationContext
|
||||
.getBean("requestMappingHandlerMapping", RequestMappingHandlerMapping.class);
|
||||
Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();
|
||||
map.forEach((key, value) -> LOGGER.info("{} {}", key, value));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.endpoint.listener;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Configuration
|
||||
public class EndpointsListener implements ApplicationListener<ContextRefreshedEvent> {
|
||||
private final Logger LOGGER = LoggerFactory.getLogger("EndpointsListener.class");
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ContextRefreshedEvent event) {
|
||||
ApplicationContext applicationContext = event.getApplicationContext();
|
||||
RequestMappingHandlerMapping requestMappingHandlerMapping = applicationContext
|
||||
.getBean("requestMappingHandlerMapping", RequestMappingHandlerMapping.class);
|
||||
Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();
|
||||
map.forEach((key, value) -> LOGGER.info("{} {}", key, value));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.endpoint.swagger;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import springfox.documentation.builders.PathSelectors;
|
||||
import springfox.documentation.builders.RequestHandlerSelectors;
|
||||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spring.web.plugins.Docket;
|
||||
|
||||
@Configuration
|
||||
public class SpringFoxConfig {
|
||||
|
||||
@Bean
|
||||
public Docket api() {
|
||||
return new Docket(DocumentationType.SWAGGER_2)
|
||||
.select()
|
||||
.apis(RequestHandlerSelectors.any())
|
||||
.paths(PathSelectors.any())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.baeldung.putvspost;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Address {
|
||||
|
||||
private @Id @GeneratedValue Long id;
|
||||
private String name;
|
||||
private String city;
|
||||
private String postalCode;
|
||||
|
||||
Address() {
|
||||
}
|
||||
|
||||
public Address(String name, String city, String postalCode) {
|
||||
this.name = name;
|
||||
this.city = city;
|
||||
this.postalCode = postalCode;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setCity(String city) {
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
public String getPostalCode() {
|
||||
return postalCode;
|
||||
}
|
||||
|
||||
public void setPostalCode(String postalCode) {
|
||||
this.postalCode = postalCode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Address [id=" + id + ", name=" + name + ", city=" + city + ", postalCode=" + postalCode + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.baeldung.putvspost;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class AddressController {
|
||||
|
||||
private final AddressRepository repository;
|
||||
|
||||
AddressController(AddressRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
@GetMapping("/addresses")
|
||||
List<Address> getAllAddresses() {
|
||||
return repository.findAll();
|
||||
}
|
||||
|
||||
@GetMapping("/addresses/{id}")
|
||||
Optional<Address> getAddressesById(@PathVariable Long id) {
|
||||
return repository.findById(id);
|
||||
}
|
||||
|
||||
@PostMapping("/addresses")
|
||||
Address createNewAddress(@RequestBody Address newAddress) {
|
||||
return repository.save(newAddress);
|
||||
}
|
||||
|
||||
@PutMapping("/addresses/{id}")
|
||||
Address replaceEmployee(@RequestBody Address newAddress, @PathVariable Long id) {
|
||||
|
||||
return repository.findById(id)
|
||||
.map(address -> {
|
||||
address.setCity(newAddress.getCity());
|
||||
address.setPostalCode(newAddress.getPostalCode());
|
||||
return repository.save(address);
|
||||
})
|
||||
.orElseGet(() -> {
|
||||
return repository.save(newAddress);
|
||||
});
|
||||
}
|
||||
|
||||
@DeleteMapping("/addresses/{id}")
|
||||
void deleteEmployee(@PathVariable Long id) {
|
||||
repository.deleteById(id);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.baeldung.putvspost;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface AddressRepository extends JpaRepository<Address, Long> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.putvspost;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class PutVsPostApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(PutVsPostApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.unsupportedmediatype;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class UnsupportedMediaTypeApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(UnsupportedMediaTypeApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.baeldung.unsupportedmediatype;
|
||||
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
import java.io.Serializable;
|
||||
|
||||
@XmlRootElement
|
||||
public class User implements Serializable {
|
||||
private Integer id;
|
||||
private String name;
|
||||
private Integer age;
|
||||
private String address;
|
||||
|
||||
public User(){
|
||||
}
|
||||
|
||||
public User(Integer id, String name, Integer age, String address) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Integer getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(Integer age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{"
|
||||
+ "id=" + id
|
||||
+ ", name='" + name + '\''
|
||||
+ ", age=" + age
|
||||
+ ", address='" + address + '\''
|
||||
+ '}';
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.unsupportedmediatype;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/user")
|
||||
public class UserController {
|
||||
|
||||
@GetMapping(value = "/")
|
||||
List<User> getAllUsers(){
|
||||
return Collections.singletonList(new User(1, "Andy", 28, "14th Street"));
|
||||
}
|
||||
|
||||
@GetMapping(value = "/{user-id}")
|
||||
User getUser(@PathVariable("user-id") Integer userId){
|
||||
return new User(userId, "Andy", 28, "14th Street");
|
||||
}
|
||||
|
||||
@PostMapping(value = "/", consumes = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE})
|
||||
void AddUser(@RequestBody User user){
|
||||
// Adding the User in the repository
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
spring.mvc.async.request-timeout=750
|
||||
spring.mvc.async.request-timeout=750
|
||||
management.endpoints.web.exposure.include=mappings
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.baeldung.unsupportedmediatype;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@WebMvcTest(UserController.class)
|
||||
public class ApplicationUnitTest {
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Test
|
||||
public void JsonTestCase() throws Exception {
|
||||
mockMvc.perform(MockMvcRequestBuilders.post("/user/")
|
||||
.contentType(MediaType.APPLICATION_JSON_VALUE)
|
||||
.content( "{\n"
|
||||
+ " \"name\": \"Andy\",\n"
|
||||
+ " \"age\": 1,\n"
|
||||
+ " \"address\": \"Hello world\"\n"
|
||||
+ "}"))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void JsonFailTestCase() throws Exception {// Because no content-type added
|
||||
mockMvc.perform(MockMvcRequestBuilders.post("/user/")
|
||||
.content( "{\n"
|
||||
+ " \"name\": \"Andy\",\n"
|
||||
+ " \"age\": 1,\n"
|
||||
+ " \"address\": \"Hello world\"\n"
|
||||
+ "}"))
|
||||
.andExpect(status().isUnsupportedMediaType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void XmlTestCase() throws Exception {
|
||||
mockMvc.perform(MockMvcRequestBuilders.post("/user/")
|
||||
.contentType(MediaType.APPLICATION_XML_VALUE)
|
||||
.content("<user><name>Andy</name><age>1</age><address>Hello world</address></user>"))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void StringFailTestCase() throws Exception { // Because content-type is not supported
|
||||
mockMvc.perform(MockMvcRequestBuilders.post("/user/")
|
||||
.contentType(MediaType.TEXT_PLAIN_VALUE)
|
||||
.content("<user><name>Andy</name><age>1</age><address>Hello world</address></user>"))
|
||||
.andExpect(status().isUnsupportedMediaType());
|
||||
}
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
spring.mvc.async.request-timeout=750
|
||||
spring.mvc.async.request-timeout=750
|
||||
spring.main.allow-bean-definition-overriding=true
|
||||
|
||||
Reference in New Issue
Block a user