簡體   English   中英

Java REST服務-無法從瀏覽器調用

[英]java REST service - can't call from browser

我多年來一直在研究這個問題,但似乎無法使其正常工作。 它是提供REST服務的簡單測試Java應用程序。

我有以下web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns="http://java.sun.com/xml/ns/javaee"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

<display-name>test2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
  <param-name>com.sun.jersey.config.property.packages</param-name>
  <param-value>com.crunchify.restjersey</param-value>
</init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/test2/*</url-pattern>
</servlet-mapping>
</web-app>

和以下類設置:

package com.crunchify.restjersey;

/**
* @author Crunchify.com
*/

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

@Path("/ctofservice")
public class CtoFService {
@GET
@Produces("application/xml")
public String convertCtoF() {

    Double fahrenheit;
    Double celsius = 36.8;
    fahrenheit = ((celsius * 9) / 5) + 32;

    String result = "@Produces(\"application/xml\") Output: \n\nC to F Converter Output: \n\n" + fahrenheit;
    return "<ctofservice>" + "<celsius>" + celsius + "</celsius>" + "<ctofoutput>" + result + "</ctofoutput>" + "</ctofservice>";
}

@Path("{c}")
@GET
@Produces("application/xml")
public String convertCtoFfromInput(@PathParam("c") Double c) {
    Double fahrenheit;
    Double celsius = c;
    fahrenheit = ((celsius * 9) / 5) + 32;

    String result = "@Produces(\"application/xml\") Output: \n\nC to F Converter Output: \n\n" + fahrenheit;
    return "<ctofservice>" + "<celsius>" + celsius + "</celsius>" + "<ctofoutput>" + result + "</ctofoutput>" + "</ctofservice>";
}
}

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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test2</groupId>
<artifactId>test2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
 <sourceDirectory>src</sourceDirectory>
<plugins>
  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.3</version>
    <configuration>
      <source>1.6</source>
      <target>1.6</target>
    </configuration>
  </plugin>
  <plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.6</version>
    <configuration>
      <warSourceDirectory>WebContent</warSourceDirectory>
      <failOnMissingWebXml>false</failOnMissingWebXml>
    </configuration>
  </plugin>
</plugins>
</build>
<dependencies>
    <dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-bundle</artifactId>
    <version>1.19.1</version>
</dependency>
<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20151123</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-core</artifactId>
    <version>1.19.1</version>
</dependency>
<dependency>
    <groupId>asm</groupId>
    <artifactId>asm</artifactId>
    <version>3.3.1</version>
</dependency>
</dependencies>
</project>

並將OK部署到tomcat:-

INFO: Starting Servlet Engine: Apache Tomcat/8.0.33
Apr 29, 2016 6:43:08 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs.     Enable debug logging for this logger for a complete list of JARs that were    scanned but no TLDs were found in them. Skipping unneeded JARs during   scanning can improve startup time and JSP compilation time.
Apr 29, 2016 6:43:08 PM com.sun.jersey.api.core.PackagesResourceConfig  init
INFO: Scanning for root resource and provider classes in the packages:
com.crunchify.restjersey
Apr 29, 2016 6:43:08 PM com.sun.jersey.api.core.ScanningResourceConfig   logClasses
INFO: Root resource classes found:
class com.crunchify.restjersey.CtoFService
class com.crunchify.restjersey.FtoCService
Apr 29, 2016 6:43:08 PM com.sun.jersey.api.core.ScanningResourceConfig     init
INFO: No provider classes found.
Apr 29, 2016 6:43:08 PM    com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.19.1 03/11/2016    02:42 PM'
Apr 29, 2016 6:43:09 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Apr 29, 2016 6:43:09 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Apr 29, 2016 6:43:09 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 2719 ms

但是在瀏覽器中調用時,如下所示:

http://localhost:8080/test2/ctofservice

我剛收到HTTP狀態404-/ test2 / ctofservice

已經檢查了很多其他問題和答案,但是我似乎沒有任何這些問題-歡迎任何想法!

我認為您需要將web.xml文件更改為此:

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns="http://java.sun.com/xml/ns/javaee"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

<display-name>test2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
  <param-name>com.sun.jersey.config.property.packages</param-name>
  <param-value>com.crunchify.restjersey</param-value>
</init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

這將是輸出:

輸出

希望我能對您有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM