java.lang.NoSuchMethodError on Jhipster - java

I send restTemplate.exchange() from spring-boot project:
RestTemplate restTemplate = new RestTemplate();
String URI = "http://localhost:8888/getResource";
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
ResponseEntity<List<PayScreenMenu>> screenMenus = restTemplate.exchange(URI, HttpMethod.GET, null, new ParameterizedTypeReference<List<PayScreenMenu>>() {});
to jhipster method:
#RequestMapping(value = "/getResource", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public #ResponseBody List<PayScreenMenu> getResource() {
....
return payScreenMenuList;
}
after the return jhipster method returned error:
java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.introspect.AnnotatedMember.getType()Lcom/fasterxml/jackson/databind/JavaType;
on pom.xml jhipster project added version jackson converter:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hibernate4</artifactId>
<version>2.6.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hppc</artifactId>
<version>2.6.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-json-org</artifactId>
<version>2.6.4</version>
</dependency>

The Resource class that the JHipster method is in might be annotated with something like:
#RestController
#RequestMapping("/api")
If that's the case, you need to change String URI = "http://localhost:8888/getResource"; to String URI = "http://localhost:8888/api/getResource";.

Related

DefaultDataBufferFactory cannot be cast to class NettyDataBufferFactory

I have a spring-boot project with spring-cloud gateway. When I make a http request, I am getting this error. I have been working on trying to solve this issue for days, but I could not manage to solve it.
java.lang.ClassCastException: class org.springframework.core.io.buffer.DefaultDataBufferFactory cannot be cast to class org.springframework.core.io.buffer.NettyDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory and org.springframework.core.io.buffer.NettyDataBufferFactory are in unnamed module of loader 'app')
These are versions which I am using for the apllication.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
This is my pom.xml for the service.
<properties>
<!-- Spring -->
<spring-cloud.version>Hoxton.SR10</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
This is how I am modifying gateway filter.
#Slf4j
public class ModifyHttpMethodGatewayFilter extends
AbstractGatewayFilterFactory<ModifyHttpMethodGatewayFilter.Config> {
public ModifyHttpMethodGatewayFilter() {
super(ModifyHttpMethodGatewayFilter.Config.class);
}
#Override
public GatewayFilter apply(ModifyHttpMethodGatewayFilter.Config config) {
return (exchange, chain) -> {
ServerHttpRequest.Builder builder = exchange.getRequest().mutate();
builder.method(config.getMethod());
ServerHttpRequest request = builder.build();
if (!config.getMethod().equals(HttpMethod.GET)) {
// Create request body if missing from a GET request
String bodyString = getRequestBody(request);
DataBuffer bodyDataBuffer = stringDataBuffer(bodyString);
HttpHeaders headers = request.getHeaders();
Flux<DataBuffer> bodyFlux = Flux.just(bodyDataBuffer);
request = new ServerHttpRequestDecorator(request) {
#Override
public Flux<DataBuffer> getBody() {
return bodyFlux;
}
#Override
public HttpHeaders getHeaders() {
return headers;
}
};
}
return chain.filter(exchange.mutate().request(request).build());
};
}
public static class Config {
#NotEmpty
protected HttpMethod method;
public HttpMethod getMethod() {
return method;
}
public Config setMethod(HttpMethod method) {
this.method = method;
return this;
}
}
}
stringDataBuffer method.
public static DataBuffer stringDataBuffer(String value) {
byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
NettyDataBufferFactory nettyDataBufferFactory = new NettyDataBufferFactory(ByteBufAllocator.DEFAULT);
DataBuffer buffer = nettyDataBufferFactory.allocateBuffer(bytes.length);
buffer.write(bytes);
return buffer;
}

ClientHandlerException: MIME media type, multipart/form-data, was not found

I am using Jersey client to hit a Spring MVC REST Controller for image uploading functionality. I am getting the following exception:
com.sun.jersey.api.client.ClientHandlerException: A message body writer for Java type, class org.springframework.web.multipart.commons.CommonsMultipartFile, and MIME media type, multipart/form-data, was not found
My Controller Method to POST the Image:
#RequestMapping(value = "/file/upload", method = RequestMethod.POST)
public String fileUpload(#RequestParam("fileUpload") MultipartFile file,
Model model, HttpServletRequest request, HttpServletResponse response)
{
try
{
ClientConfig config = new DefaultClientConfig();
com.sun.jersey.api.client.Client client = com.sun.jersey.api.client.Client.create(config);
WebResource webResource = client.resource("/save-image");
ClientResponse responseMsg = webResource
.type(MediaType.MULTIPART_FORM_DATA)
.post(ClientResponse.class, file);
}
catch (Exception e)
{
logger.error("Exception in fileUpload()", e);
return "error";
}
return "success";
}
My REST Controller method get the post data:
#ResponseBody
#Consumes(MediaType.MULTIPART_FORM_DATA)
#RequestMapping(value = "/save-image", method = RequestMethod.POST)
public String saveImage(#FormDataParam("file") MultipartFile file, ModelMap
model)
{
//Code to save the image
}
Is there a solution to this exception. I have tried according to the following stack solutions but I am still getting the same exception.
Jersey client exception: A message body writer was not found
Sending multiple files with Jersey: MessageBodyWriter not found for multipart/form-data
Have you added the dependencies for multipart?
<!-- Jersey client support -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<!-- Apache Commons FileUpload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>

Thymeleaf custom dialect doesn't work

My custom dialect with processor doesn't parse any value and I don't know why. In generated view there is nothing in place where ${content} should be and after changing tag to th:text it appears. I'm using Spring Boot v1.5.9.RELEASE, Spring v4.3.13.RELEASE
pom.xml dependencies (it's submodule)
<properties>
<h2.version>1.4.194</h2.version>
<java-version>1.8</java-version>
<org.thymeleaf-version>3.0.9.RELEASE</org.thymeleaf-version>
<org.thymeleaf.extras-version>3.0.0.RELEASE</org.thymeleaf.extras-version>
<thymeleaf-layout-dialect.version>2.1.2</thymeleaf-layout-dialect.version>
</properties>
<dependencies>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>${org.thymeleaf-version}</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>${org.thymeleaf-version}</version>
</dependency>
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
<version>${thymeleaf-layout-dialect.version}</version>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
<version>${org.thymeleaf.extras-version}</version>
</dependency>
<!--WebJars-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<!--database-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
<version>${h2.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-java8</artifactId>
<version>${hibernate.version}</version>
</dependency>
</dependencies>
LineSeparatorProcessor.java
public class LineSeparatorProcessor extends AbstractAttributeTagProcessor {
private static final String ATTR_NAME = "lstext";
private static final int PRECEDENCE = 10000;
public LineSeparatorProcessor(final String dialectPrefix) {
super(
TemplateMode.HTML,
dialectPrefix,
null,
false,
ATTR_NAME,
true,
PRECEDENCE,
true);
}
protected void doProcess(
final ITemplateContext context, final IProcessableElementTag tag,
final AttributeName attributeName, final String attributeValue,
final IElementTagStructureHandler structureHandler) {
final IEngineConfiguration configuration = context.getConfiguration();
final IStandardExpressionParser parser =
StandardExpressions.getExpressionParser(configuration);
final IStandardExpression expression = parser.parseExpression(context, attributeValue);
final String value = (String) expression.execute(context);
structureHandler.setBody(
HtmlEscape.escapeHtml5Xml(value).replace(System.getProperty("line.separator"), "<br />"),
false);
}
}
MyDialect.java
public class MyDialect extends AbstractProcessorDialect {
public MyDialect() {
super(
"MyDialect",
"mydialect",
13000);
}
public Set<IProcessor> getProcessors(final String dialectPrefix){
final Set<IProcessor> processors = new HashSet<>();
processors.add( new LineSeparatorProcessor(dialectPrefix) );
return processors;
}
}
ThymeleafConfiguration.java
#Configuration
public class ThymleafConfiguration {
#Bean
public MyDialect myDialect() {
return new MyDialect();
}
}
view.html
<span mydialect:lstext="${content}" ></span>
You need to add the dialect to the instance of the TemplateEngine. For example:
#Bean
public SpringTemplateEngine templateEngine(){
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setEnableSpringELCompiler(true);
templateEngine.setTemplateResolver(templateResolver());
templateEngine.addDialect(new MyDialect());
return templateEngine;
}
You can find this documented in the Say Hello! Extending Thymeleaf in 5 minutes guide.

request MediaType "application/json" but received "text/html"

I have the following get request to a RESTful web service:
List<ShoppingEntry> entries = client.target("http://localhost:8080/MMServer/webresources/shopping/getEntries")
.request(MediaType.APPLICATION_JSON)
.get()
.readEntity(new GenericType<List<ShoppingEntry>>() {});
and the corresponding resource
#GET
#Path("getEntries")
#Produces(MediaType.APPLICATION_JSON)
public List<ShoppingEntry> getShoppingBundle() {
List<ShoppingEntry> shoppingEntries = new ArrayList<>();
List<Shopping> shoppingList = super.findAll();
ShoppingEntry entry;
for (Shopping s: shoppingList) {
entry = new ShoppingEntry();
entry.setName(s.getIdperson().getFirstname());
entry.setDate(s.getDate());
entry.setAmount(s.getAmount());
entry.setStore(s.getStore());
shoppingEntries.add(entry);
}
return shoppingEntries;
}
The problem: Sometimes the program runs without error and everything is alright. But most of the time i get the following exception:
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=text/html, type=interface java.util.List, genericType=java.util.List.
which says: "type=text/html" what should be the cause. How can this happen?
My dependencies are:
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.26</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.26</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>2.26</version>
</dependency>
</dependencies>

Unable to find message body reader with content type application/json in Jersey Test Framework

I have selected Jersey Test Framework to implement unit test cases for REST services.But i am getting following issue once i ran the test.
Note: I even add the resteasy-jackson-provider into pom file but couldn't help.
Here is the .pom file dependency
<!-- jersey security dependency -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
</dependency>
<!-- jersey test framework dependency -->
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-jetty</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>2.3.4.Final</version>
</dependency>
<!--junit Dependency-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
MockServices.Java
#Path("/hello")
public class MockServices {
#GET
#Produces(MediaType.APPLICATION_JSON)
#Path("/world")
public DateVO getHello() {
DateVO j=new DateVO ();
j.setActive(true);
return j;
}
}
MockServicesTest.Java
public class MockServicesTest extends JerseyTest {
#Override
protected Application configure() {
return new ResourceConfig(MockServices.class);
}
#Test
public void test() {
Response hello = target("/hello/world").request().get();
System.out.println(hello.readEntity(String.class));//throw an above exception
}
}
Please let me know how can i overcome this problem.
Override your provider method like this
#Override
protected Application configure() {
ResourceConfig config =new ResourceConfig(MockServices.class).register(JacksonFeature.class).register("Your ContextResolver<ObjectMapper> implementation class");
return config;
}
I had to use explicitly Jersey client implementation to invoke the REST end points.
#Test
public void test() {
final Client client = new JerseyClientBuilder().build();
WebTarget target = client.target("http://localhost:9998");
final Response response =
target.path("/hello/world").request().get();
final String json = response.readEntity(String.class);
}
Reference

Categories

Resources