我正在尝试使用Sprig + jersey编写Web服务,这是我的代码:
package orgProfiles.webserviceJersey;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import orgProfiles.model.Registration;
import orgProfiles.repository.RegisterUserRepository;
@Path("mytest")
@Component
public class TestService {
@Autowired
private RegisterUserRepository registerUserRepository;
@GET
@Produces(MediaType.APPLICATION_XML)
public List<Registration> hitest() {
return registerUserRepository.findAll();
}
}
存储库:
package orgProfiles.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import orgProfiles.model.Registration;
@Repository("registerUserRepository")
public interface RegisterUserRepository extends JpaRepository<Registration,Long>{
@Query("Select c from Registration c where c.name=:companyname")
Registration findByCompanyname(@Param("companyname")String companyname);
// @Query("Select c from Registration c where c.place like %:place%")
List<Registration> findByPlaceContaining(String place);
// @Query("Select c from Registration c where c.name like %:name%")
List<Registration> findByNameContaining( String name);
// @Query("Select c from Registration c where c.type like :type")
List<Registration> findByTypeContaining(String type);
List<Registration> findByTypeAndPlace(String type, String place);
List<Registration> findByNameAndPlace(String name, String place);
}
型号:
package orgProfiles.model;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
@Entity
public class Registration {
@Id
@GeneratedValue
private Long id;
private String name;
@Column(name="description", columnDefinition="TEXT")
private String description;
private String type;
private String place;
@Column(name="address", columnDefinition="TEXT")
private String address;
private String website;
private double latitude;
private Long phoneNum;
private double longitude;
@OneToOne(cascade=CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumn(name="id")
private LoginCredentials loginCredentials;
public String getAddress() {
return address;
}
public String getDescription() {
return description;
}
public Long getId() {
return id;
}
public double getLatitude() {
return latitude;
}
public LoginCredentials getLoginCredentials() {
return loginCredentials;
}
public double getLongitude() {
return longitude;
}
public String getName() {
return name;
}
public Long getPhoneNum() {
return phoneNum;
}
public String getPlace() {
return place;
}
public String getType() {
return type;
}
public String getWebsite() {
return website;
}
public void setAddress(String address) {
this.address = address;
}
public void setDescription(String description) {
this.description = description;
}
public void setId(Long id) {
this.id = id;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public void setLoginCredentials(LoginCredentials loginCredentials) {
this.loginCredentials = loginCredentials;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public void setName(String name) {
this.name = name;
}
public void setPhoneNum(Long phoneNum) {
this.phoneNum = phoneNum;
}
public void setPlace(String place) {
this.place = place;
}
public void setType(String type) {
this.type = type;
}
public void setWebsite(String website) {
this.website = website;
}
}
的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.profiles</groupId>
<artifactId>orgProfiles</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>orgProfiles Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.9.Final</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.3.0.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>2.12</version>
</dependency>
<!-- Apache Commons FileUpload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!-- Apache Commons IO -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.11</version>
</dependency>
</dependencies>
<build>
<finalName>orgProfiles</finalName>
</build>
</project>
当我尝试访问该服务时(http:// l o c alhost:8080 / orgProfiles / ws / mytest),它最终会出现HTTP状态500 - 内部服务器错误错误。我如何将我的实体标记为@XmlRootElement?当我把那个注释说它XmlRootElement无法解析为一个类型。当我将mediatype更改为MediaType.APPLICATION_JSON时,它的工作正常。我可以添加JAXB依赖项来获取@XMlRootElement注释吗?先谢谢
答案 0 :(得分:0)
尝试添加jersey-json依赖项:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>${jersey.version}</version>
</dependency>