Last active
February 11, 2021 14:11
-
-
Save maemresen/868d2ef37d839123f05f189ec4fd9a76 to your computer and use it in GitHub Desktop.
Spring VIP - Quick Start Snippets. See https://springvip.yazilim.vip
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package vip.yazilim.libs.springvip.template.example; | |
import org.springframework.data.jpa.repository.JpaRepository; | |
import org.springframework.stereotype.Repository; | |
@Repository | |
public interface IPersonRepo extends JpaRepository<Person, Integer> { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package vip.yazilim.libs.springvip.template.example; | |
import vip.yazilim.libs.springvip.util.generic.service.IGenericServiceCrud; | |
public interface IPersonService extends IGenericServiceCrud<Person, Integer> { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package vip.yazilim.libs.springvip; | |
import org.springframework.boot.autoconfigure.security.SecurityProperties; | |
import org.springframework.core.ParameterizedTypeReference; | |
import org.springframework.http.HttpMethod; | |
import org.springframework.web.client.RestTemplate; | |
import vip.yazilim.libs.springvip.util.HttpRequestModel; | |
import static vip.yazilim.libs.springvip.util.HttpRequestHelperKt.jsonRequest; | |
/** | |
* @author maemresen - [email protected] | |
* 2/9/2021 | |
*/ | |
public class Example { | |
public void main() { | |
// Example 1 - Get All Users /users | |
jsonRequest("http://my.api.com", "/users", null | |
, new ParameterizedTypeReference<SecurityProperties.User>() { | |
} | |
, HttpMethod.GET | |
, new RestTemplate() | |
, new HttpRequestModel() | |
); | |
// Example 2 - Get By Id /users/1 | |
HttpRequestModel httpRequestModel = new HttpRequestModel(); | |
httpRequestModel.setUrlParam("id", "1"); | |
jsonRequest("http://my.api.com", "/users/{id}", null | |
, new ParameterizedTypeReference<SecurityProperties.User>() { | |
} | |
, HttpMethod.GET | |
, new RestTemplate() | |
, httpRequestModel | |
); | |
// Example 3 - Get By name /users?name=emre&age=30 | |
HttpRequestModel httpRequestModel = new HttpRequestModel(); | |
httpRequestModel.setQueryParam("name", "emre"); | |
httpRequestModel.setQueryParam("age", "30"); | |
jsonRequest("http://my.api.com", "/users", null | |
, new ParameterizedTypeReference<SecurityProperties.User>() { | |
} | |
, HttpMethod.GET | |
, new RestTemplate() | |
, httpRequestModel | |
); | |
// Example 4 - Create /users with body | |
User user = new User(); | |
user.name="John"; | |
user.surname="Doe"; | |
jsonRequest("http://my.api.com", "/users", user | |
, new ParameterizedTypeReference<SecurityProperties.User>() { | |
} | |
, HttpMethod.POST | |
, new RestTemplate() | |
, httpRequestModel | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package vip.yazilim.libs.springvip.template.example; | |
import javax.persistence.*; | |
import lombok.Data; | |
@Entity | |
@Table(name = "person") | |
@Data // just for lombok. Not required!! | |
public class Person { | |
@Id | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
@Column(columnDefinition = "serial") | |
private int id; | |
@Column(name = "first_name") | |
private String firstName; | |
@Column(name = "last_name") | |
private String lastName; | |
private String career; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package vip.yazilim.libs.springvip.template.example; | |
import org.jetbrains.annotations.NotNull; | |
import org.springframework.data.jpa.repository.JpaRepository; | |
import org.springframework.stereotype.Service; | |
import vip.yazilim.libs.springvip.util.generic.service.impl.AGenericServiceCrud; | |
@Service | |
public class PersonServiceImpl extends AGenericServiceCrud<Person, Integer> implements IPersonService { | |
public PersonServiceImpl(@NotNull JpaRepository<Person, Integer> repository) { | |
super(repository, Person.class, Integer.class); | |
} | |
@NotNull | |
@Override | |
protected Integer getId(@NotNull Person entity) { | |
return entity.getId(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package vip.yazilim.libs.springvip.template.example; | |
import org.jetbrains.annotations.NotNull; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RestController; | |
import vip.yazilim.libs.springvip.config.bean.IRestResponseBuilder; | |
import vip.yazilim.libs.springvip.util.generic.rest.AGenericRest; | |
import vip.yazilim.libs.springvip.util.generic.rest.GenericRest; | |
import vip.yazilim.libs.springvip.util.generic.rest.method.DeleteById; | |
import vip.yazilim.libs.springvip.util.generic.rest.method.GetAll; | |
import vip.yazilim.libs.springvip.util.generic.rest.method.GetById; | |
@RestController | |
@RequestMapping("/rest/person") | |
@GenericRest | |
@GetAll @GetById @DeleteById | |
public class RestPerson extends AGenericRest<Person, Integer> { | |
public RestPerson(@NotNull IRestResponseBuilder restResponseBuilder, @NotNull IPersonService personCrudService) { | |
super(restResponseBuilder, personCrudService, Person.class, Integer.class); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment