Can someone explain why this URL returns 404?
http://localhost:8080/HelloWorld-1.0-SNAPSHOT/rest/hat
How should I be passing this so the parameter hat
is passed in and see Hello hat!
on the output
HelloService.java
package com.sentiment360.helloworld;
public class HelloService {
String createHelloMessage(String name) {
return "Hello " + name + "!";
}
}
HelloWorld.java
package com.sentiment360.helloworld;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
/**
* A simple REST service which is able to say hello to someone using HelloService Please take a look at the web.xml where JAX-RS
* is enabled
*
* @author gbrey@redhat.com
*
*/
@Path("/")
public class HelloWorld {
@Inject
HelloService helloService;
@GET
@Path("/json")
@Produces({ "application/json" })
public String getHelloWorldJSON() {
return "{\"result\":\"" + helloService.createHelloMessage("World") + "\"}";
}
@GET
@Path("/xml")
@Produces({ "application/xml" })
public String getHelloWorldXML() {
return "<xml><result>" + helloService.createHelloMessage("World") + "</result></xml>";
}
}
JAXActivator.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.sentiment360.helloworld;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
/**
* JAXActivator is an arbitrary name, what is important is that javax.ws.rs.core.Application is extended
* and the @ApplicationPath annotation is used with a "rest" path. Without this the rest routes linked to
* from index.html would not be found.
*/
@ApplicationPath("rest")
public class JAXActivator extends Application {
// Left empty intentionally
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
My directory structure:
You should change your endpoints like this:
@GET
@Path("/rest/{name}")
@Consumes("application/json")
@Produces("application/json")
public String getHelloWorldJSON(@PathParam("name") String name) {
return "{\"result\":\"" + helloService.createHelloMessage(name) + "\"}";
}
@GET
@Path("/rest/{name}")
@Consumes("application/xml")
@Produces("application/xml")
public String getHelloWorldJSON(@PathParam("name") String name) {
return "<xml><result>" + helloService.createHelloMessage(name) + "</result></xml>";
}
The @Consumes is the right way to change from json and xml input. The @Path("rest/{name}") is doing the binding of this endpoint to /rest/something, and the @PathParam("name") is binding the variable path value to the variable name
This will work if your server is correctly deployed in http://localhost:8080/HelloWorld-1.0-SNAPSHOT
The url you are using does not match your configuration.
Given your configuration you have 2 possible resources to request:
which basically differ in the produced contenttype.
Edit: Ok, i will post necessary changes so that you have exactly the output you asked for given the exact query you wanted to use:
public class HelloWorld {
@Inject
HelloService helloService;
@GET
@Path("/{helloSuffix}")
public String getHello(@PathParam("helloSuffix") String helloSuffix) {
return helloService.createHelloMessage(helloSuffix);
}
}
JAXActivator.java ...
@ApplicationPath("HelloWorld-1.0-SNAPSHOT/rest")
public class JAXActivator extends Application {
}
Firebase Cloud Functions: PubSub, "res.on is not a function"
TypeError: Cannot read properties of undefined (reading 'createMessageComponentCollector')
How to load tree/indexes fromyaml file in java? I want to grap all this values from
This question already has an answer here:
How do I add a list in swagger-api notes section for an end point? something like this