Spring Recreation

I am working with an example from https://spring.io/guides/gs/rest-service/ and I have changed the default class hello class to my class and when I call this in the web browser http: // localhost: 8080 / greeting I get a response: There was an unexpected error (type=Not Acceptable, status=406). Could not find acceptable representation

My controller:

package rest;

import java.util.concurrent.atomic.AtomicLong;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import database.*;

@RestController
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();
    private DBAccess dbaccess= new DBAccess();

    @RequestMapping("/greeting")
    public Customer greeting(@RequestParam(value="name", defaultValue="World") String name) {
        return new  Customer(1,"a","b");
    }
}

And client class:

package database;

public class Customer {
    private long id;
    private  String firstName, lastName;

    public Customer(){};
    public Customer(long id, String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }


    void setFirstName(String firstName){
        this.firstName=firstName;
    }

    void setLastName(String lastName){
        this.lastName=lastName;
    }


    @Override
    public String toString() {
        return String.format(
                "Customer[id=%d, firstName='%s', lastName='%s']",
                id, firstName, lastName);
    }
}

following the Spring MVC Rest / JSON service I added a portfolio pocket dependency, but it does not work ...

Please help me

According to the nebula comment. I do not have web.xml, I use the example from https://spring.io/guides/gs/rest-service/ and it is not.

Here is my Application.class

package rest;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan
@EnableAutoConfiguration
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
+4
source share
6

, , JsonMappingException.

, / , , , Customer.

+1

.

@RequestMapping(value = "/greeting", method = RequestMethod.GET, 
produces = "application/json; charset=utf-8")
@ResponseBody
public Customer greeting(@RequestParam(value="name", defaultValue="World") String name) {

, http://localhost:8080/greeting

0

@ResponseBody greeting,

 @RequestMapping("/greeting") 
 public @ResponseBody Customer greeting(@RequestParam(value="name", defaultValue="World") String name) {
     return new  Customer(1,"a","b"); 
 } 

. ( ) http.

0

, .

, JSON- pom.xml?

 <dependency>
      <groupId>com.jayway.jsonpath</groupId>
      <artifactId>json-path</artifactId>
      <scope>test</scope>
 </dependency>
0

Are you using spring boot? If so, your application class should be annotated with @SpringBootApplication. It integrates annotations @EnableWebMvc, @Configuration, @ComponentScanand @EnableAutoConfiguration. The stacktrace function would be useful to try to figure out what the problem is.

0
source

change your

 public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}

For

  public static void main(String[] args) {
        SpringApplication.run(GreetingController.class, args);
    }
-1
source

All Articles