

Spring
bootで下記のコントローラーを実行しましたが最初のif文がtrueの時つまりユーザが存在しないときはエラーは出ませんがfalseの時つまりユーザが存在した場合に500エラーが返されます。  
しかし、標準出力を見ても、SQLのログがあるだけでエラーが出ていません。

コード

コントローラ

[code]

    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    
    import org.apache.commons.lang3.RandomStringUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.security.crypto.codec.Hex;
    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;
    
    import com.zusn.domain.User;
    import com.zusn.repository.UserRepository;
    
    @RestController
    @RequestMapping(value = "/user")
    public class UserController {
        @Autowired
        private UserRepository userRepository;
    
        @RequestMapping(value = "/create", method = RequestMethod.POST)
        public User createUser(@RequestBody User newUesr) throws NoSuchAlgorithmException{
            User user = userRepository.findByUidAndDevice(newUesr.getUid(), newUesr.getDevice());
            if(user == null){
                MessageDigest md = MessageDigest.getInstance("SHA");
                Long now = System.nanoTime();
                md.update(now.byteValue());
                String random = RandomStringUtils.randomAlphabetic(32);
                md.update(random.getBytes());
                newUesr.setConsumerKey(String.valueOf(Hex.encode(md.digest())));
                return userRepository.save(newUesr);
            }else{
                return user;
            }
        }
    }
    
[/code]

ユーザレポジトリ

[code]

    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.data.repository.query.Param;
    import org.springframework.stereotype.Repository;
    
    import com.zusn.domain.Devices;
    import com.zusn.domain.Providers;
    import com.zusn.domain.User;
    
    @Repository
    public interface UserRepository extends JpaRepository<User, Long>{
        /**
         * UIDとプロバイダーからユーザを返す
         * @param uid UID
         * @param provider Profider ex) twitter, google+, facebook
         * @return User
         */
        public User findByUidAndProvider(@Param("uid") String uid, @Param("provider") Providers provider);
    
        /**
         * UIDとデバイスからユーザを探す
         * @param uid UID
         * @param devices Device ex) iOS, Android
         * @return User
         */
        public User findByUidAndDevice(@Param("uid")String uid, @Param("device") Devices device);
    }
    
[/code]

なぜ500エラーが返されるのでしょうか。

**追記**

ユーザモデル

[code]

    import javax.persistence.CascadeType;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.OneToOne;
    import javax.persistence.Table;
    
    
    @Entity
    @Table(name = "users")
    public class User {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id", nullable = false)
        private Long id;
    
        @Column(name = "uid", nullable = false)
        private String uid;
    
        @Column(name = "provider")
        private Providers provider;
    
        @Column(name = "device", nullable = false)
        private Devices device;
    
        @Column(name = "consumer_key", nullable = false, unique = true)
        private String consumerKey;
    
        @OneToOne(cascade = CascadeType.ALL, fetch=FetchType.LAZY)
        private Profile profile;
    
    
    
        public User() {
            super();
        }
    
        public User(String uid, Providers providers, String consumerKey) {
            super();
            this.uid = uid;
            this.provider = providers;
            this.consumerKey = consumerKey;
        }
    
        public String getConsumerKey() {
            return consumerKey;
        }
    
        public void setConsumerKey(String consumerKey) {
            this.consumerKey = consumerKey;
        }
    
        public User(Providers provider){
            this.provider=provider;
        }
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getUid() {
            return uid;
        }
    
        public void setUid(String uid) {
            this.uid = uid;
        }
    
        public Providers getProvider() {
            return provider;
        }
    
        public void setProvider(Providers provider) {
            this.provider = provider;
        }
    
        public Profile getProfile() {
            return profile;
        }
    
        public void setProfile(Profile profile) {
            this.profile = profile;
        }
    
        public Devices getDevice() {
            return device;
        }
    
        public void setDevice(Devices device) {
            this.device = device;
        }
    
    }
    
[/code]


CC BY-SA 3.0
2015-11-06T00:00:19.927
18447
2023-09-27T03:08:06.127
2015-11-06T09:55:03.867
7232
7232
question
java spring spring-boot
Spring boot でスタックトレースがでない500エラー


エラーの内容を確認したいということであれば、application.properties（またはapplication.yaml）でSpring
Frameworkのログレベルを変更すると情報を得られる可能性があります。

application.propertiesの場合の設定例)

[code]

    logging.level.*=DEBUG
    または
    logging.level.org.springframework=DEBUG
    
[/code]

アプリケーションプロパティ設定一覧  
<https://spring.pleiades.io/spring-
boot/docs/current/reference/html/application-properties.html>


CC BY-SA 4.0
2023-08-21T04:29:41.680
95996
2023-08-21T04:31:59.533
2023-08-21T04:31:59.533
59048
59048
18447
answer