Jersey - get参数始终为null

时间:2015-11-22 19:25:44

标签: spring web-services rest jersey-2.0

我根据R4J的答案更改了我的代码。 我认为还有更多要纠正的内容,因为我现在无法显示任何内容......

result I get - 控制台清晰(无错误)

有人可以这么善良并帮助我找到问题吗?

下面我描述我的项目:

DB

database table "users"

TestUser.java

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "USERS")
public class TestUser {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    @Column(name = "email", nullable = false)
    private String email;
    @Column(name = "password", nullable = false)
    private String password;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}

TestService.class

import java.util.List;

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

import com.test.testapp.dao.UserDao;
import com.test.testapp.model.TestUser;

@Component
    public class TestService {
        @Autowired
        UserDao userDao;

        public List<TestUser> getUsers() {
            return userDao.findAll();
        }
    }

UserDao.class

import java.util.List;

import javax.persistence.PersistenceException;

import com.test.testapp.model.TestUser;

public interface UserDao /* extends CrudRepository<TestUser, Integer>*/{
    public List<TestUser> findAll() throws PersistenceException;
}

UserDaoImpl.java

    import java.util.List;

    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    import javax.transaction.Transactional;

    import org.jvnet.hk2.annotations.Service;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Repository;

    import com.test.testapp.dao.UserDao;
    import com.test.testapp.model.TestUser;

    @Repository("userDao")
    @Service
    public class UserDaoImpl implements UserDao {

        @Autowired
        private EntityManager entityManager;

        @PersistenceContext
        public void setEntityManager(EntityManager entityManager) {
            this.entityManager = entityManager;
        }

        public TestUser findPersonById(Integer id) {
            return entityManager.find(TestUser.class, id);
        }

        @Override
        @Transactional
        public List<TestUser> findAll() {
            try {
                return entityManager.createQuery("SELECT u FROM Users u ORDER BY p.id", TestUser.class).getResultList();
            } finally {
                entityManager.close();
            }
        }
    }

TestWebApi.java

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Controller
@Path("test")
public interface TestWebApi {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/")
    public Response getUsers();
}

TestWebApiImpl.java

import java.util.List;

import javax.inject.Inject;
import javax.ws.rs.core.Response;

import com.test.testapp.model.TestUser;
import com.test.testapp.service.TestService;
import com.test.testapp.web.TestWebApi;

public class TestWebApiImpl implements TestWebApi {

    @Inject
    TestService testService;

    @Override
    public Response getUsers() {
        List<TestUser> test = testService.getUsers();
        return Response.ok().entity(test).build();
    }
}

1 个答案:

答案 0 :(得分:1)

您正在将JAX-RS注释与Spring-MVC注释混合使用。如果你想坚持使用JAX-RS,那么你的代码应该是这样的:

@Path("users")
@Component
public class UserController {

    @Inject
    UserService userService;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/")
    public List<User> getUsers() {
        return userService.findAll();
    }

    @GET
    @Path("/users/{name}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getUserByName(@NotNull @PathParam("name") String username) {
        User user = userService.findByName(username);
        return Response.ok().entity(user).build();
    }
}

目前,你的Class上有@RestController,它使它成为Spring Rest Controller。所以Spring扫描所有方法并找到&#39; @RequestMapping(&#34; / user / {name}&#34;)&#39;和&#39; @RequestMapping(&#34; / users&#34;)&#39;所以它将这些方法绑定到默认的GET操作并忽略完全@PathVariable注释,因为它来自JAX-RS而不是Spring。

您的代码的Spring-MVC版本将是:

@RestController
@RequestMapping("/")
public class UserController {

    @Inject
    UserService userService;

    @RequestMapping(value = "/users", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public List<User> getUsers() {
        return userService.findAll();
    }

    @RequestMapping(value = "/users/{name}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public Response getUserByName(@NotNull @PathVariable("name") String username) {
        User user = userService.findByName(username);
        return Response.ok().entity(user).build();
    }
}