Multiple Path Params in Jersey - java

I'm building my first rest api webservice and now i want to get two different path params, but it is not working!
here's the code that is causing the error:
#PUT
#Path("/{projectid}/updateproject/{profileid}")
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public Projects updateProject(
#PathParam("projectid") int projectid,
#PathParam("profileid") int profileid,
Projects pProject,
Profiles pProfile) {
pProject.setProjektid(projectid);
pProfile.setPersonalnummer(profileid);
return service.addProfileInProject(pProject, pProfile);
}
And this is the error i get:
org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
It also says something with:
on resource class ... contains multiple parameters with no annotation. Unable to resolve the injection source.
Please help me with this issue!
Thanks! <3

Related

Spring MVC - Using #PathVariable doesn't find the JSP

I am using the #PathVariable annotation to provide an ID to my controller. When I do this, it finds the mapping and runs the code within the controller method, but when it comes to load the JSP it cannot find it and I get the error below.
HTTP ERROR 404
Problem accessing
/Timesheets/viewtimesheet/WEB-INF/pages/viewtimesheet.jsp. Reason:
Not Found
In the log I get the following error:
SEVERE: PWC6117: File "C:\dev\Projects\DominoTimesheets\WebContent\viewtimesheet\WEB-INF\pages\viewtimesheets.jsp" not found
If I take out the #PathVariable then it runs fine and loads the page as expected (but I need to have the timesheetId to runs against the database). The path above has viewtimesheet before the WEB-INF which is causing the issues. It seems that it does not like the mapping of /viewtimesheet/{timesheetID} Can someone shed any light on this?
#RequestMapping(value="/viewtimesheet/{timesheetID}", method = RequestMethod.GET)
public String viewTimesheet(#PathVariable int timesheetID, ModelMap model,
final RedirectAttributes redirectAttributes, HttpSession session) {
Timesheets timesheetResult = timesheetsService.getTimesheet(timesheetID);
model.addAttribute("mondayDate", timesheetResult.getDate_WC());
model.addAttribute("viewTimesheetModel", timesheetResult);
return "viewtimesheets";
}
I have fixed the issue. The InternalResourceViewResolver bean in the dispatcher-servlet required a slash before the prefix value.
<property name="prefix" value="/WEB-INF/pages/"/>
This loaded the page as expected without the extra viewtimesheet in the url:
SEVERE: PWC6117: File
"C:\dev\Projects\DominoTimesheets\WebContent\viewtimesheet\WEB-INF\pages\viewtimesheets.jsp"
not found

returning string instead or returning the ModelView is not working in spring MVC

i have tried to write an upload option in my web application following mentioned is my code
#RequestMapping( value="/group/import/upload", method=RequestMethod.GET )
public String GroupImportInfo( Model model )
{
System.out.println("in controller");
return "upload-File-import";
}
i have a upload-File-import.jsp file, but when i try to access that page am getting an exception saying..
type Status report
message /corept/WEB-INF/views/upload-File-import.jsp
description The requested resource (/corept/WEB-INF/views/upload-File-import.jsp) is not available.
JBoss Web/7.0.13.Final
Thanks for helping.
It seems the ViewResolver is unable to locate the view. Hence, I suggest you to check configuration of the ViewResolver bean's prefix property(in the spring-servlet-config.xml) if it is matching the path of the requested view.
Apart from that I dont see any issue with your code.

Using #BeanParameter with Jersey

I am creating an restfull webservice using JAX-RS, I've started developing using Wildfly 8.2 and JEE7 and I was able to achieve this:
endpoint search method:
#GET
#Path("/consultar")
public Response consultar(
#QueryParam("offset") #DefaultValue(value = "0") Integer offSet,
#QueryParam("limit") #DefaultValue(value = "10") Integer limit,
#NotNull #BeanParam EmpresaDTO filtro) {
return super.consultar(offSet, limit, filtro);
}
endpoint abstraction search method:
#Override
public Response consultar(Integer offSet, Integer limit, #NotNull Object filtro) {
T filtroMaterializado = mapper.map(filtro, getClassType());
Example example = getExampleGenerator().generate(filtroMaterializado);
List<T> lista = getRepository().listar(offSet, limit, example);
return getOkResponse(lista);
}
Thats was working until I had the requisite of migrating to Tomcat, then I pick Jersey as my JAX-RS implementation. Now I get an big big error stacktrace, followerd by this warning at server startup:
WARNING: A HTTP GET method, public javax.ws.rs.core.Response br.com.logtec.delivery.resource.AbstractResource.consultar(java.lang.Integer,java.lang.Integer,java.lang.Object), should not consume any entity.
I've googled and I've found this: Using #Consume with GET request in Jersey Rest
But I rather stick with the javax api default annotation #BeanParam, furthermore theres no such annotation #InjectParam into jersey-container-servlet dependency.
So what I ask is, is there a way of using #BeanParam at #GET method? If not, how can I include #InjectParam without including the hole glassfish-embedded-web dependency?
Nevermind, I figured it out. The problem was that my Resource interface abstract methods were annotated by #GET, #POST... I ripped them out and it was solved. Thanks anyway

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!

JAX-RS #Path annotation with regular expression doesn't match expected URL

I want the URL for my API to have following format for users resource:
http://hostname/webappname/something/something/serviceName/orgs/{id}/users
However, I get a 404 error as the URL mapping fails. But if I have following format, I get the response.
http://hostname/webappname/{id}/something/something/serviceName/orgs/{id}/users
This is my #Path annotation:
#Path("/orgs/{oid: [0-9]+}/users")
But if I have the following #Path annotation:
#Path("/orgs")
With URL's
http://hostname/webappname/{id}/something/something/serviceName/orgs
http://hostname/webappname/something/something/serviceName/orgs
I always get a response back. Can someone tell how should I provide #Path annotation for my resource. Or where am I going wrong?
Users Resource class looks like this:
#Path("/orgs/{oid: [0-9]+}/users")
#Component
public class UserResource extends Resource {
}
Orgs Resource class looks like this:
#Path("/orgs")
#Component
public class OrgResource extends Resource {
}
You should use JAX-RS subresources. In your case, UserResource is a subresource of OrgResource.
I figured out the problem. It had nothing to do with the my #Path annotation. While creating this project I added some old code dependencies. One of then was unsharding filter whose job was to strip of shard present in the URL before passing it to the next filter. It was removing the {oid} from the requested path and hence there was a URL mapping error. I faced this issue only when I changed the URL format for my project.

Categories

Resources