RestFul Java With JaxRs 404 Found - java

I am making small application. I think I made everything correctly but I got 404 error. Please be honest and see my code https://github.com/chalauri/Tsu_Junior and tell me if anything is incorrect. I deployed my application on tomcat. the module path is "/" and for example I got 404 http://localhost:8080/tsu/faculty/test from this url

One issue I see (but probably not the only one); It looks like you are trying to access a method in the FacultyResource class, but there is no method with a path of test or a path parameter in that class.
Try adding a method with path test
#Path("/test")
#GET
public String testMethod(){
return "Hello World";
}
Or add a method with a path param
#Path("/{name}")
#GET
public String testMethod(#PathParam("name") String name){
return "Hello " + name;
}

Related

cannot reach controller method in SpringBoot

I'm new to Spring Boot and I'm playing around with it trying to get a response from my backend.
The web server and application are running without errors but when I try to get "Hello World" by typing localhost:8080/api/hello I get a 404 not found.
This is the controller method
#RestController
#RequestMapping("/api")
public class EmployeeController {
#GetMapping("/hello")
public String greeting() {
return "Hello, World";
}
}
This is the response I get
the response
You've defined your api path as api/hello, but based on the screenshot of your request you are calling /hello.
use localhost:8080/api/hello ,
it will a solution for your problem

No response on a GET query to JAX-RS service

I have a service that has two classes and is very simple:
#ApplicationPath("/api")
public class HelloApplication extends Application {}
and
#Path("/hello-world")
public class HelloResource {
#GET
//#Produces("text/plain")
public Response hello() {
//return Response.ok().entity("This is JAX-RS").build();
return Response.ok().status(200).build();
//return "Hello, World!";
}
}
Commented lines are the ones that I tried to use. Every time I call "http://localhost:8080/api/hello-world" or "http://localhost:8080/api" the response is 404-Not Found. I use Tomcat 9.0.50.
What can be wrong with my usage of the classes or their calling from the outside?
The problem was - for some reason - in the usage of Tomcat. After moving to JBoss WildFly 24.0.0 the problem disappeared and the system started to work correctly.
If someone knows the reason of such an incompatibility - write a comment or an extra answer.

Creating and Running RESTful Web Service on GlassFish

I created a simple RESTful web service on the GlassFish server and run it according to this tutorial in the IntelliJ IDE. This runs fine based on the instruction provided. I have 2 additional questions,
a. The tutorial uses a service class provide below,
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
#Path("/helloworld")
public class HelloWorld {
#GET
#Produces("text/plain")
public String getClichedMessage() {
return "Hello World";
}
}
I can access that from the URL provided,
http://localhost:8080/AppointmentManager_war_exploded/helloworld
Afterward, I add a new class in the same directory,
#Path("/")
public class App {
#GET
#Produces("text/plain")
public String getMessage() {
return "Hello, Berlin";
}
}
I expected to see the message "Hello, Berlin" in the browser from the opening URL http://localhost:8080/AppointmentManager_war_exploded/, but, instead, I get the error provided,
HTTP Status 404 - Not Found
type Status report
messageNot Found
descriptionThe requested resource is not available.
GlassFish Server Open Source Edition 5.0
What is the issue here?
b. How do I change the part of URL AppointmentManager_war_exploded to something else, say, appointment etc? The artifact tab in the project setting is provided below,
I edited it, but, the change it not corresponded as expected.
I changed the project to maven build after the tutorial, but, the issue is not created for that. If someone interested, you can try too as it will take a minute to run.
Thank you.
First
I expected to see the message "Hello, Berlin" in the browser from the opening URL http://localhost:8080/AppointmentManager_war_exploded/, but, instead, I get the error provided
In MyApplication class that provided by tutorial you should also add your new class:
#ApplicationPath("/")
public class MyApplication extends Application{
#Override
public Set<Class<?>> getClasses() {
HashSet h = new HashSet<Class<?>>();
h.add(HelloWorld.class);
h.add(App.class); // Add your new class here
return h;
}
}
Then you will be able to see expected page on http://localhost:8080/AppointmentManager_war_exploded/
Second
How do I change the part of URL AppointmentManager_war_exploded to something else, say, appointment etc?
URL contains name of your artifact AppointmentManager_war_exploded. This artifact automatically copied to glassfish application directory. You can check glassfish\domains\domain1\applications\__internal.
Just change it just in project structure window here:
Update
Don't forget to change start URL in configuratin settings for app:

How to use ".." to pass URL parameters into Jersey?

I've encountered this URL RESTful query:
http://rest.ensembl.org/map/cdna/ENST00000288602/100..300?content-type=application/json
Where 100..300 are parameters that says: "from 100 to 300" (indexes). I can change those parameters as i'd like.
I want to have this kind of query parameter passing in my web-service as well. How can I annotate this in a Jersey API, and how do I get the parameter values?
You can use #Path annotation to map the 100..300 kind of URL.
For example, the below code works fine for me.
#Path("/Test")
#Component
public class TestRestfulService {
#GET
#Path("/100..200")
#Produces(MediaType.APPLICATION_JSON)
public String getText(){
return "Success";
}
}
Using the above code you can access the method by localhost:8080/test/100..200.
Hope this solves the problem!

Verifying the path of a web service resource

I am writing some unit tests for a web service written years ago. The root class has a path like:
#Path("v1/path/")
public class RootResource {
...
}
The methods inside the class have their respective path. One working path is:
#GET
#Path("orders/{order_num}.xml")
#Produces(MediaType.APPLICATION_XML)
public Response getXML() {
...
}
This is working fine at root_path/v1/path/orders/123123.xml.
However, there is another method:
#POST
#Path("orders/{order_numer}/status.xml")
#Consumes(MediaType.APPLICATION_XML)
#Produces(MediaType.APPLICATION_XML)
public Response getStatusXML() {
Logger.info(CALLER, "orderStatusXML", "XML Request received");
...
}
When I try to access this at root_path/v1/path/orders/123423/status.xml I get 404 in return. Even the first line with the logger is missing from the logs.
I am on Weblogic 12 and we used Jersey REST API for these web services.
I have tried a number of things to make sure the path listed in the test case is the correct one. Any hints/ideas on how to check for the correct path?
There is no reason for the root_path/v1/path/orders/123423/status.xml path to give a 404 unless something else is acting on that URL.
Things to try:
look into your web.xml file and see what URL pattern Jersey handles. That particular URL might be handled by some other servlet;
again look into the web.xml and see if you have any filters declared. What URLs do does filters intercept and what do the filters do to the request once intercepted?
this might not be the case but I'll add it anyway... is it a "404 - Not found" that you get back or is it actually a "405 - Method not allowed" that is returned? If you try to access the root_path/v1/path/orders/123423/status.xml URL with a GET, like from the browser, you get 405 because your method is annotated with #POST. Are you using a POST?
My bet is on a filter!

Categories

Resources