2016-04-29 2 views
0

Я искал эту проблему целую вечность и просто не могу заставить ее работать. Это простое тестовое Java-приложение для предоставления услуги REST.java REST service - невозможно позвонить из браузера

У меня есть следующий 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 to 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 Status 404 -/test2/ctofservice

проверили много других вопросов и ответов, но я не» t seemto имеет любую из этих проблем - Любые идеи приветствуются !!

+0

Как называется файл войны, который вы развертываете? test2.war или test2-0.0.1-SNAPSHOT.war? Если я правильно помню, контекст по умолчанию для войны - это полное имя (все до .war, а не только имя проекта maven). Попробуйте http: // localhost: 8080/test2-0.0.1-SNAPSHOT/ctofservice – djmorton

+0

@ManoDestra Наличие аннотации Path в классе означает, что все методы в классе будут использовать это в качестве базы (а затем более конкретными добавляются дополнительные Аннотации пути к методам). Как это делается здесь, вполне приемлемо. – djmorton

+1

вы можете попробовать http: // localhost: 8080/test2/test2/ctofservice – yogidilip

ответ

1

Я думаю, что вы должны изменить файл 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> 

И это будет выход:

output

Я надеюсь, что я помог вам!

+0

Да, это решило проблему./test2 /, очевидно, не требуется. – oidsman

Смежные вопросы