5 Aralık 2019 Perşembe

SpringData Rest @Projection Anotasyonu

Giriş
Maven'da şöyle yaparız.
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
Örnek
Şöyle yaparız.
@Projection(name = "userProjection", types = { User.class })
public interface UserProjection {
  //Getters
}
Bu projection'a erişmek için kullanılan url şöyle
"@RepositoryRestResource.name + projection= + @Projection.name" yani şöyle
http://localhost:8080/rest/users/1?projection=userProjection
Örnek
Şöyle yaparız
@Projection(name = "foo", types = YourDomainClass.class)
interface Inlined {

  // list all other properties

  Sector getSector();
}

3 Aralık 2019 Salı

SpringMVC @ResponseBody Anotasyonu - Java Nesnesini Json'a Çevirir

Giriş
Bir nesnenin JSON'a serialize edilerek gönderilmesini sağlar. Açıklaması şöyle.
If you annotate a method with @ResponseBody, Spring will use a json mapper to generate the response. Instead of annotating every method with @ResponseBody you can annotate your class with @RestController
Açıklaması şöyle.
@ResponseBody is a marker for the HTTP response body and @ResponseStatus declares the status code of the HTTP response.
Açıklaması şöyle.
@ResponseBody annotation on the method tells Spring MVC that it does not need to render the object through a server-side view layer, but that instead the object returned is the response body, and should be written out directly.
Normalde bu anotasyonu metod üzerinde tek tek kullanmak yerine tüm sınıfı @RestController olarak işaretleyip kullanmak daha iyi olabilir.

Örnek
Eğer bu anotasyonu kullanmazsak şöyle yapmamız gerekir.
@RequestMapping(value = "/search")
public ResponseEntity<String> search(BizData bizData,Page page,String sourceType){
  return new ResponseEntity<>(new Gson().toJson(...), HttpStatus.OK);
}
Örnek
Şöyle yaparız.
@PostMapping("/request_list")
@ResponseBody
public List <Staff> processCreationForm(Model model){...}
Örnek
Get isteğine Json verisi dönmek için şöyle yaparız.
@RequestMapping(value = "/history", method = RequestMethod.GET)
public @ResponseBody Map<String, ChatModel> getHistory() {
  Map<String, ChatModel> model = ...

  return model;
}
Örnek
Post isteğine Json verisi dönmek için şöyle yaparız.
@RequestMapping(value="getNames", method=RequestMethod.POST)
public @ResponseBody Foo addNew(@RequestBody Foo jsonString) {
  Foo foo= new Foo();
  foo.setName(jsonString.getName());
  
  return foo;
}

2 Aralık 2019 Pazartesi

SpringData PageRequest Sınıfı

Giriş
Pageable nesnesi üretmek için kullanılır. Pageable nesnesi repository nesneler ile birlikte kullanılır.

of metodu
Metodun içi şöyle.
/**
 * Creates a new unsorted {@link PageRequest}.
 *
 * @param page zero-based page index.
 * @param size the size of the page to be returned.
 * @since 2.0
 */
public static PageRequest of(int page, int size) {
    return of(page, size, Sort.unsorted());
}
Örnek
Şöyle yaparız.
public Optional<Page<User>> getAllByPage(Integer pageSize, Integer page) {
  Pageable pageable = PageRequest.of(page, pageSize);
  return userRepository.findAll(pageable);
}