티스토리 뷰

Dev/Spring

Spring RESTful Service

마이스토리 2016. 5. 30. 15:30

> 샘플코드

package lyj.sample.web.api;


import java.math.BigInteger;

import java.util.Collection;

import java.util.HashMap;

import java.util.Map;


import lyj.sample.model.Greeting;


import org.springframework.http.HttpStatus;

import org.springframework.http.MediaType;

import org.springframework.http.ResponseEntity;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RestController;


@RestController

public class GreetingController {

private static BigInteger nextId;

private static Map<BigInteger, Greeting> greetingMap;

private static Greeting save(Greeting greeting){

if (greetingMap == null){

greetingMap = new HashMap<BigInteger, Greeting>();

nextId = BigInteger.ONE;

}

// If Update.....

if (greeting.getId() != null){

Greeting oldGreeting = greetingMap.get(greeting.getId());

if (oldGreeting == null){

return null;

}

greetingMap.remove(greeting.getId());

greetingMap.put(greeting.getId(), greeting);

return greeting;

}

// If Create.....

greeting.setId(nextId);

nextId = nextId.add(BigInteger.ONE);

greetingMap.put(greeting.getId(), greeting);

return greeting;

}

private static boolean delete(BigInteger id){

Greeting deletedGreeging = greetingMap.remove(id);

if (deletedGreeging == null){

return false;

}

return true;

}

static {

Greeting g1 = new Greeting();

g1.setText("Hello World!");

save(g1);

Greeting g2 = new Greeting();

g2.setText("Hola Mundo!");

save(g2);

}

@RequestMapping(value="/api/greetings", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)

public ResponseEntity<Collection<Greeting>> getGreetings(){

Collection<Greeting> greetings = greetingMap.values();

return new ResponseEntity<Collection<Greeting>>(greetings, HttpStatus.OK);

}

@RequestMapping(value="/api/greetings/{id}", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)

public ResponseEntity<Greeting> getGreeting(@PathVariable("id") BigInteger id){

Greeting greeting = greetingMap.get(id);

if (greeting == null){

return new ResponseEntity<Greeting>(HttpStatus.NOT_FOUND);

}

return new ResponseEntity<Greeting>(greeting, HttpStatus.OK);

}

@RequestMapping(value="/api/greetings", method=RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE, produces=MediaType.APPLICATION_JSON_VALUE)

public ResponseEntity<Greeting> createGreeting(@RequestBody Greeting greeting){

Greeting savedGreeting = save(greeting);

return new ResponseEntity<Greeting>(savedGreeting, HttpStatus.CREATED);

}

@RequestMapping(value="/api/greetings/{id}", method=RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE, produces=MediaType.APPLICATION_JSON_VALUE)

public ResponseEntity<Greeting> upateGreeting(@RequestBody Greeting greeting){

Greeting updatedGreeting = save(greeting);

if (updatedGreeting == null){

return new ResponseEntity<Greeting>(HttpStatus.INTERNAL_SERVER_ERROR);

}

return new ResponseEntity<Greeting>(updatedGreeting, HttpStatus.OK);

}

@RequestMapping(value="/api/greetings/{id}", method=RequestMethod.DELETE, consumes=MediaType.APPLICATION_JSON_VALUE)

public ResponseEntity<Greeting> deleteGreeting(@PathVariable("id") BigInteger id, @RequestBody Greeting greeting){

if (delete(id)){

return new ResponseEntity<Greeting>(HttpStatus.NO_CONTENT);

}else{

return new ResponseEntity<Greeting>(HttpStatus.INTERNAL_SERVER_ERROR);

}

}

}



댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28