我正在使用JAX-RS在java中实现一个restful服务,并且在测试服务时它只适用于我的一个方法,当我添加一个带有不同@PATH注释的新方法时,测试网页只是空白而没有错误
我的资源类
@Path("啤酒&#34) 公共类BeerResources {
tours_users_id
}
应用程序配置类
@ javax.ws.rs.ApplicationPath(" / webresources&#34) 公共类ApplicationConfig扩展了Application {
@Context
private UriInfo context;
/**
* Creates a new instance of BeerResources
*/
public BeerResources() {
}
@GET
@Path("/costliest")
@Produces(MediaType.TEXT_PLAIN)
public String getCostliest() {
//TODO return proper representation object
return new BusinessLayer().getCostliest();
}
@GET
@Path("/cheapest")
@Produces(MediaType.APPLICATION_XML)
public String getCheapest() {
//TODO return proper representation object
return new BusinessLayer().getCheapest();
}
}
答案 0 :(得分:0)
您可以尝试使用Spring Boot + JAX-RS方法或Spring Boot + Spring MVC。以下是我Github page上的两个。
还有Spring Boot + JAX-RS源代码:
Application.java:
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplicationBuilder builder = new SpringApplicationBuilder(Application.class);
new Application().configure(builder).run(args);
}
}
DrinkEndpoint.java:
@Component
@Path("/drinks")
public class DrinkEndpoint {
@Autowired
private DrinkService drinkService;
@GET
@Path("/list")
@Produces(MediaType.APPLICATION_JSON)
public Iterable<Drink> getCostliest() {
return drinkService.getDrinks();
}
@GET
@Path("/{drink}")
@Produces(MediaType.APPLICATION_JSON)
public Response getCheapest(@PathParam("drink") String name) {
Optional<Drink> drink = drinkService.getDrink(name);
if (drink.isPresent()) {
return Response.status(201).entity(drink.get().getName()).build();
} else {
return Response.status(201).entity("NOT_FOUND").build();
}
}
}
DrinkService.java:
public interface DrinkService {
Iterable<Drink> getDrinks();
Optional<Drink> getDrink(String name);
}
DrinkServiceImpl.java:
@Component
public class DrinkServiceImpl implements DrinkService {
private List<Drink> drinks = new ArrayList<Drink>() {{
add(new Drink("Coca Cola", 1886));
add(new Drink("Pepsi", 1903));
}};
public Iterable<Drink> getDrinks() {
return drinks;
}
public Optional<Drink> getDrink(String name) {
for (Drink drink : drinks) {
if (drink.getName().equalsIgnoreCase(name)) {
return Optional.of(drink);
}
}
return Optional.empty();
}
}
ApplicationConfig.java:
@Component
public class ApplicationConfig extends ResourceConfig {
public ApplicationConfig() {
register(DrinkEndpoint.class);
}
}
Drink.java:
public class Drink {
private String name;
private int since;
public Drink(String name, int since) {
this.name = name;
this.since = since;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSince() {
return since;
}
public void setSince(int since) {
this.since = since;
}
}
的pom.xml:
<project>
....
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.3.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
开始运行应用程序:
mvn spring-boot:run
然后在浏览器中打开:
答案 1 :(得分:0)
确保您返回正确的XML格式
示例:
@Produces(MediaType.APPLICATION_XML)
public String getCheapest() {
return "<abc>xyz</abc>";
}
如果BusinessLayer().getCheapest()
函数返回字符串,并且您使用了代码@Produces(MediaType.APPLICATION_XML)
,则只会显示一个空白页。
根据
返回的值,在 @Produces 标记中使用适当的 MediaTypeBusinessLayer().getCheapest()
函数