Spring

Spring 일지 #72 (20211019) 시험 대비(member 등록)

uni5948 2021. 10. 20. 23:19

72. member 등록

-member 등록

 *restcontroller 생성(RestMemberController.java)

RestMemberController.java

package com.example.controller;

 

import java.util.HashMap;

import java.util.Map;

 

import com.example.entity.Member;

import com.example.service.MemberService;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.MediaType;

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

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

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

 

@RestController

@RequestMapping(value = "/member")

public class RestMemberController {

   

    @Autowired

    MemberService mService;

 

    @PostMapping(value = "/insert.json",

    consumes = MediaType.ALL_VALUE,

    produces = MediaType.APPLICATION_JSON_VALUE)

    public Object insertGET(@RequestBody Member member){

        Map<String, Object> map = new HashMap<>();

        try{

            mService.insertMember(member);

            map.put("status", 200);

        }

        catch(Exception e){

            e.printStackTrace();

            map.put("status", e.hashCode());

        }

        return map;

    }

}

 

 * Application  등록(B20211019Application.java)

 

package com.example.b20211019;

 

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.autoconfigure.domain.EntityScan;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

 

@SpringBootApplication

@EnableJpaRepositories(basePackages = "com.example.repository")

@EntityScan(basePackages = {"com.example.entity"})

@ComponentScan(basePackages = { "com.example.controller",

"com.example.service", "com.example.config" })

public class B20211019Application {

 

    public static void main(String[] args) {

        SpringApplication.run(B20211019Application.class, args);

        System.out.println("start");

    }

}

 

 *등록하기

등록 성공