I have a problem producing JSON in my application.
I'm trying a tutorial about Consuming Java Restful Web Service with AngularJS.
I've created a dynamic web project as my RESTful server and then added the following libraries:
asm-3.3.1.jar
jackson-core-asl-1.9.9.jar
jackson-jaxrs-1.9.9.jar
jackson-mapper-asl-1.9.9.jar
jackson-xc-1.9.9.jar
jersey-bundle-1.8.jar
jersey-bundle-1.9.jar
jersey-client-1.9.jar
jersey-core-1.9.jar
jersey-json-1.9.jar
jersey-media-json-jettison-2.4.jar
jersey-server-1.9.1.jar
jersey-servlet-1.12.jar
jettison-1.3.3.jar
jsr311-api-1.1.1.jar
Here is the service :
package ws;
import java.awt.PageAttributes.MediaType;
import java.util.*;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
import entities.Prospect;
#Path("prospect")
public class ProspectRestful {
#GET
#Path("findall")
#Produces(MediaType.APPLICATION_JSON)
public List<Prospect> findAll(){
List<Prospect> result = new ArrayList<Prospect>();
result.add(new Prospect(35, "Name 35", "last35"));
result.add(new Prospect(36, "Name 36", "last36"));
return result;
}
}
But I'm getting this error:
APPLICATION_JSON cannot be resolved or is not a field.
I've seen in another question that jersey-media-json-jettison-2.4.jar should be there so I added it but no sign.
I'm sure I'm not using a Maven project, in the same tutorial they didn't use Maven either.
Remove this
import java.awt.PageAttributes.MediaType;
and add this
import javax.ws.rs.core.MediaType;
In my case it is not working, so if anyone facing the error then try...
import org.springframework.http.MediaType;
It worked for me...
Related
I'm following this simple tutorial: https://www.mkyong.com/webservices/jax-ws/jax-ws-hello-world-example-document-style/?fbclid=IwAR0vxhYrj9MKy1Q28h6luFVJoSxDP4KWBOLEu_v_Ss4uQztmB-9JuYsS4RI and at step 3 it mentions that I should receive the error:
Wrapper class com.mkyong.ws.jaxws.GetHelloWorldAsString is not found.
Have you run APT to generate them?
However, I do not get such error(no error at all) and I'm worried that it is not working as expected.
My classes:
Interface:
package com.soap3sk.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import javax.jws.soap.SOAPBinding.Use;
// Service Endpoint Interface
#WebService
#SOAPBinding(style= Style.DOCUMENT, use= Use.LITERAL) // optional
public interface NoteEndpoint {
//#WebMethod ArrayList<ToDoNote> getNotes();
#WebMethod String getHelloWorldAsString(String name);
}
Implementation:
package com.soap3sk.ws;
import javax.jws.WebService;
#WebService(endpointInterface = "com.soap3sk.ws.NoteEndpoint")
public class NoteEndpointImpl implements NoteEndpoint {
#Override
public String getHelloWorldAsString(String name) {
return "Hello World JAX-WS " + name;
}
}
Publisher:
package com.soap3sk.endpoint;
import javax.xml.ws.Endpoint;
import com.soap3sk.ws.NoteEndpointImpl;
public class NoteEndpointPublisher {
public static void main (String[] args) {
Endpoint.publish("http://localhost:5000/ws/hello", new NoteEndpointImpl());
}
}
Project structure: https://imagizer.imageshack.com/img924/3514/BAuOcl.png
What I also noticed that those 2 .class files(asString and Response that are mentioned in the guide) are not generated anywhere as well. I'm using Eclipse and created a maven project with the quickstart archetype. Runnning it as a standard java application.
I can access the wsdl file going here: http://localhost:5000/ws/hello?wsdl and the I can see getHelloWorldAsString and getHelloWorldAsStringResponse there, but they are nowhere to be seen in my project and no error is thrown that they could not be found as mentioned in the guide that it should.
I also tried downloading the sample project and deleting the .java files that should be required, but it is stil the same(no error, not asking to create those classes).
I would be very grateful if someone could help. Thank you.
EDIT
I found a similiar question here: Java web service not giving error message Could someone explain his answer? Is the creation of those two classes not necessary?
you're trying to replicate a situation reported almost 10 years ago. Don't you want to try a newer tutorial like the following:
https://www.baeldung.com/jax-ws
https://spring.io/guides/gs/producing-web-service/
I've been trying to create a simple hello world application with Java and SpringBoot in IntelliJ IDEA, but I get this error and I don't know how to solve it.
I get the error at the return. Java doesn't seem to know how to resolve the of method that's in the public List<String> getHelloWorld method.
package com.myname.SpringApp;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
#RestController
public class HelloWorldController {
#RequestMapping("api/hello-world")
#GetMapping
public List<String> getHelloWorld(){
return List.of("Hello", "World");
}
}
The overloaded List.of methods were introduced in Java 9.
Since:
9
You must be compiling with an older version. Update to Java 9 or later. Here's how.
You can use Arrays.asList.
List.of will support java 9 and above
I am running a Glassfish server that is trying to connect to MongoDB. At first I created seperate projects for the server and MongoDB. So now I am trying to merge those projects but it appears anything I try to do it results in a faliure.
The current error I am getting is:
2018-07-05T19:54:36.249+0200|Severe: java.lang.NoClassDefFoundError: org/bson/conversions/Bson
I am well aware that the error happens in runtime and that the possible cause is my classpath.
Currently I copied all of my code from one project to another, added Maven dependencies and the following happens:
if I create a separate .java file for my MongoDB and run it in the same folder that the Glassfish server is, it works perfectly fine.
if I run the server and try to call methods from the other class (a little bit modified) the upper error appears
Simplified code example withouth error:
import org.bson.Document;
import org.bson.conversions.Bson;
import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoIterable;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Updates;
public class MyClass{
public static void main(String[]args){
String ip = "127.0.0.1";
int port = 27017;
MongoClient mongoClient = new MongoClient(ip,port);
/* Remaining code */
}
}
With error:
import org.bson.Document;
import org.bson.conversions.Bson;
import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoIterable;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Updates;
public class MyClass{
private MongoClient mongoClient;
public MyClass(String ip, int port){
mongoClient = new MongoClient(ip, port); // Error called here
}
/* Remaining code */
}
Called from the server.java file:
MyClass mc = new MyClass("127.0.0.1",27017);
I also tried to download all of the bson jar files separately and add them to the project but that had no effect...
The working solution for me was to delete the whole project and create it once more. Apparently there was a problem with Eclipse or I made a mistake before and forgot about it.
Good day folks,
I am an admittedly novice Java programmer but I take care to research docs and FAQ's to try to get past issues. This is a problem that I have not been able to overcome, however. I am using RestAssured (version 3.0.3, as pulled by Maven) and cannot get RestAssured to parse "text/plain" content (rather, I cannot get Java to compile the code to do so).
This compiles but gives an error:
import static io.restassured.RestAssured.*;
import static io.restassured.matcher.RestAssuredMatchers.*;
import static org.hamcrest.Matchers.*;
import static io.restassured.module.jsv.JsonSchemaValidator.*;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.testng.annotations.AfterClass;
public class TestNG2 {
/*
userName, passWord and server defined here as protected static Strings
*/
#Test
public void filter_Asset(){
given().
auth().basic(userName, passWord).
when().
get ("http://" + server +"/api/filter?type=$tAsset").
then().
statusCode(200).
body("count", greaterThan(0));
}
}
The error:
java.lang.IllegalStateException: Expected response body to be verified as JSON, HTML or XML but content-type 'text/plain' is not supported out of the box.
Try registering a custom parser using:
RestAssured.registerParser("text/plain", );
However, when I try to include the following line in the filter_Asset test:
RestAssured.registerParser("text/plain", Parser.JSON);
The code will not compile with the following complaint:
java.lang.Error: Unresolved compilation problems:
RestAssured cannot be resolved
Parser cannot be resolved to a variable
I receive similar complaints when I try to use the following declaration:
RestAssured.defaultParser = Parser.JSON;
For what it's worth, I am working on a Windows 7, 64-bit machine. Using Eclipse Neon.3 (4.6.3) and my JDK is 1.8_131
I've consulted the RestAssured usage and documentation pages, believe am importing the packages correctly, etc. Am I making a rookie error somewhere?
It was a rookie mistake!
In addition to statically importing the class methods, the compiler also required importing of the following classes:
import io.restassured.RestAssured;
import io.restassured.parsing.Parser;
After those declarations, I was able to register the default Parser in the filter_Asset test:
RestAssured.registerParser("text/plain", Parser.JSON);
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import org.apache.cxf.jaxrs.ext.search.SearchCondition;
import org.apache.cxf.jaxrs.ext.search.SearchContext;
import com.f2s.bean.Recentbean;
#Path("search")
public class SearchResource {
private List<Recentbean> ra;
#Path("book")
#GET
public List<Recentbean> findBooks(#Context SearchContext searchContext)
{
SearchCondition<Recentbean> condition = searchContext.getCondition(Recentbean.class);
return condition.findAll(ra);
}
error :- annotated with GET of resource, class com.f2s.restws.SearchResource, is not recognized as valid resource method.
com.sun.jersey.spi.inject.Errors$ErrorMessagesException
First of all, you "question" is actually not a question. You're just presenting an error that you get. No explanation, no expected behaviour, no question asked. Providing some more details would be helpful.
Secondly, have you had a look on the other questions dealing with the presented exception? Here there are a few:
Exception :com.sun.jersey.spi.inject.Errors$ErrorMessagesException
Jersey: com.sun.jersey.spi.inject.Errors$ErrorMessagesException
Errors$ErrorMessagesException when using Jersey in Java
Because of my reputation I couldn't add this as a comment...