RestController在没有堆栈跟踪的情况下返回500错误

时间:2015-11-06 14:26:05

标签: java spring-boot

我像这样定义UserControllerUserRepository以及User

UserController.java

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;
        }
    }
}

UserRepository.java

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>{
    /**
     * 
     * @param uid UID
     * @param provider Profider ex) twitter, google+, facebook
     * @return User
     */
    public User findByUidAndProvider(@Param("uid") String uid, @Param("provider") Providers provider);

    /**
     * 
     * @param uid UID
     * @param devices Device ex) iOS, Android
     * @return User
     */
    public User findByUidAndDevice(@Param("uid")String uid, @Param("device") Devices device);
}

User.java

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;
    }

}

当User不存在时,换句话说处理第一个if语句时,此方法返回新的User对象。但是当用户已经存在时,它将返回状态代码500 然后当它返回500错误时它没有打印stacktrace。

所以我不确定为什么它会返回500错误。请告诉我这段代码有什么问题,以及为什么它返回500错误。

2 个答案:

答案 0 :(得分:0)

首先,我认为您可能会处理Spring Data JPA层抛出的DataAccessException。对于500错误,我认为这可能是延迟加载的问题。我认为当您第一次在数据库中插入对象实体时,这是一种不同的行为。你用的是什么服务器?这是一个tomcat吗?如果是这样,您如何监控日志?根据您的配置,某些日志在标准catalina.out文件中不可见。您需要签入localhost日志文件以查看堆栈跟踪。

答案 1 :(得分:0)

没有堆栈跟踪,很难猜出问题。也许@NoOne是对的,但也许你在旧数据库的数据库中有一个独特的约束。

如果您没有看到stacktrace客户端,可以使用this snipped来获取stacktrace服务器端。因为5xx错误只是告诉了smth。在服务器上出错了。